migrations/Version20231227131150.php line 1

Open in your IDE?
  1. <?php
    
    declare (strict_types = 1);
    
    namespace DoctrineMigrations;
    
    use Doctrine\DBAL\Schema\Schema;
    use Doctrine\Migrations\AbstractMigration;
    
    /**
     * Auto-generated Migration: Please modify to your needs!
     */
    final class Version20231227131150 extends AbstractMigration
    {
        public function getDescription(): string
        {
            return '';
        }
    
        public function up(Schema $schema): void
        {
            $connection = $this->connection;
    
            //cheking if record already exist of user
            $existingRecordOfUser = $connection->fetchAssociative('SELECT * FROM `user` WHERE `username` = :username', ['username' => 'super_admin']);
            
            //if yes
            if ($existingRecordOfUser) {
                $userId = $existingRecordOfUser['id'];
            }
    
              //cheking if record already exist of role
            $existingRecordOfRole = $connection->fetchAssociative('SELECT * FROM `roles` WHERE `role_slug` = :role_slug', ['role_slug' => 'super_admin']);
            if ($existingRecordOfRole) {
                $roleId = $existingRecordOfRole['id'];
            }
    
             //if yes
            $existingRecordOfUserRole = $connection->fetchAssociative('SELECT * FROM `user_role` WHERE `user_id` = :user_id AND `role_id` = :role_id', ['user_id' => $userId, 'role_id' => $roleId]);
    
            // 2. Insert into user_role if the user exists
            if (!$existingRecordOfUserRole) {
    
                // Insert into user_role
                $this->addSql('INSERT INTO `user_role` (`user_id`, `role_id`) VALUES (:user_id, :role_id)', ['user_id' => $userId, 'role_id' => $roleId]);
    
            }
    
        }
    
        public function down(Schema $schema): void
        {
    
        }
    }