diff --git a/CHANGELOG-1.1.md b/CHANGELOG-1.1.md index 59a0e637..7c32867f 100644 --- a/CHANGELOG-1.1.md +++ b/CHANGELOG-1.1.md @@ -5,10 +5,12 @@ This changelog references any relevant changes introduced in 1.1 minor versions. * 1.1.8 * Updates - - Docker file updates. - - Added portuguese translation lang. + - Dockerfile updates. + - Added Portuguese translation language. + - Updated code related to Doctrine Migrations package update. * Issue Fixes: + - Issue #805 - Resolved an issue encountered during migration execution - Check removed for docker running env. - Enabling permission for the uvdesk.yaml file for saving email setting in case not having read/write permission. diff --git a/src/Console/Wizard/MigrateDatabase.php b/src/Console/Wizard/MigrateDatabase.php index 0b2dbdeb..641bbc6b 100644 --- a/src/Console/Wizard/MigrateDatabase.php +++ b/src/Console/Wizard/MigrateDatabase.php @@ -5,19 +5,17 @@ namespace App\Console\Wizard; use Doctrine\DBAL\DBALException; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\ArrayInput as ConsoleOptions; use Symfony\Component\Console\Output\BufferedOutput; +use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\Console\Input\ArrayInput as ConsoleOptions; -use Doctrine\Migrations\Generator\Exception\NoChangesDetected; class MigrateDatabase extends Command { private $container; private $entityManager; - private $questionHelper; public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager) { @@ -31,127 +29,115 @@ class MigrateDatabase extends Command { $this ->setName('uvdesk_wizard:database:migrate') - ->setDescription('Migrate your database to the latest schema version.') - ->setHidden(true) - ; + ->setDescription('Migrate or initialize database depending on schema status.') + ->setHidden(true); } protected function execute(InputInterface $input, OutputInterface $output) { - // Check 1: Verify database connection - $database = $this->entityManager->getConnection()->getDatabase(); + if (!$this->isDatabaseConfigurationValid()) { + $output->writeln('Invalid database configuration.'); - if (false == $this->isDatabaseConfigurationValid()) { - return; + return Command::FAILURE; } - // Get the current database migration version + $connection = $this->entityManager->getConnection(); + $schemaManager = method_exists($connection, 'createSchemaManager') + ? $connection->createSchemaManager() // DBAL 3 + : $connection->getSchemaManager(); // DBAL 2 + + $tables = $schemaManager->listTableNames(); + + if (empty($tables)) { + // ✅ FRESH INSTALL - Generate schema & load fixtures + $output->writeln('Fresh database detected. Creating schema...'); + + // Create schema + $this->runCommand('doctrine:schema:create', ['--no-interaction' => true], $output); + + // Load fixtures + $output->writeln('Loading default fixtures...'); + $this->runCommand('doctrine:fixtures:load', ['--no-interaction' => true, '--quiet' => true], $output); + + $output->writeln('Fresh installation complete.'); + + return Command::SUCCESS; + } + + // ✅ EXISTING DB - Proceed with normal migration flow + $this->runCommand('doctrine:migrations:sync-metadata-storage', ['--quiet' => true], new NullOutput()); + try { - $currentMigrationVersion = $this->getLatestMigrationVersion(new BufferedOutput()); + $currentVersion = $this->getLatestMigrationVersion(new BufferedOutput()); } catch (\Exception $e) { - // Fresh setup. No initial migration version defined. - $currentMigrationVersion = 0; + $currentVersion = '0'; } + // Create migration version if needed $this->versionMigrations($output); - // Compare the current database migration version against database - // and create a new migration version accordingly. try { - $latestMigrationVersion = $this + $latestVersion = $this ->compareMigrations($output) ->getLatestMigrationVersion(new BufferedOutput()); - if ( - ( - '0' != $currentMigrationVersion - && $currentMigrationVersion != $latestMigrationVersion - ) - || ($currentMigrationVersion != $latestMigrationVersion) - ) { + if ($currentVersion !== $latestVersion) { $this->migrateDatabaseToLatestVersion(new NullOutput()); + $output->writeln('Database migrated to latest version.'); + } else { + $output->writeln('Database is already up to date.'); } - } catch (NoChangesDetected $e) { - // Do nothing ... + } catch (\Exception $e) { + $output->writeln('Error during migration: ' . $e->getMessage() . ''); } return Command::SUCCESS; } - private function versionMigrations(OutputInterface $consoleOutput) + private function versionMigrations(OutputInterface $output) { - $command = $this->getApplication()->find('doctrine:migrations:version'); - ($consoleOptions = new ConsoleOptions([ - 'command' => 'migrations:version', - '--add' => true, - '--all' => true, + $this->runCommand('doctrine:migrations:version', [ + '--add' => true, + '--all' => true, '--quiet' => true - ]))->setInteractive(false); - - // Execute command - $command->run($consoleOptions, $consoleOutput); + ], $output); + } + private function compareMigrations(OutputInterface $output) + { + $this->runCommand('doctrine:migrations:diff', ['--quiet' => true], new NullOutput()); + $this->runCommand('doctrine:migrations:status', ['--quiet' => true], new NullOutput()); return $this; } - private function compareMigrations(OutputInterface $consoleOutput) + private function getLatestMigrationVersion(OutputInterface $output) { - $compareMigrationsCommand = $this->getApplication()->find('doctrine:migrations:diff'); - $compareMigrationsCommandOptions = new ConsoleOptions([ - 'command' => 'migrations:diff', - '--quiet' => true - ]); - - $viewMigrationStatusCommand = $this->getApplication()->find('doctrine:migrations:status'); - $viewMigrationStatusCommandOptions = new ConsoleOptions([ - 'command' => 'migrations:status', - '--quiet' => true - ]); - - // Execute command - $compareMigrationsCommand->run($compareMigrationsCommandOptions, new NullOutput()); - $viewMigrationStatusCommand->run($viewMigrationStatusCommandOptions, new NullOutput()); - - return $this; + $cmd = $this->getApplication()->find('doctrine:migrations:latest'); + $cmd->mergeApplicationDefinition(); + $cmd->run(new ConsoleOptions(['command' => 'migrations:latest']), $output); + return trim($output->fetch()); } - private function getLatestMigrationVersion(OutputInterface $bufferedOutput) + private function migrateDatabaseToLatestVersion(OutputInterface $output) { - $command = $this->getApplication()->find('doctrine:migrations:latest'); - $commandOptions = new ConsoleOptions([ - 'command' => 'migrations:latest' - ]); - - // To avoid issues through same instance - $command->mergeApplicationDefinition(); - $command = clone $command; - - // Execute command - $command->run($commandOptions, $bufferedOutput); - - return trim($bufferedOutput->fetch()); + $this->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true], $output); } - private function migrateDatabaseToLatestVersion(OutputInterface $consoleOutput) + private function runCommand(string $commandName, array $options, OutputInterface $output) { - $command = $this->getApplication()->find('doctrine:migrations:migrate'); - ($commandOptions = new ConsoleOptions([ - 'command' => 'migrations:migrate', - ]))->setInteractive(false); - - // Execute Command - $command->run($commandOptions, $consoleOutput); - - return $this; + $command = $this->getApplication()->find($commandName); + $input = new ConsoleOptions(array_merge(['command' => $commandName], $options)); + $input->setInteractive(false); + $command->run($input, $output); } private function isDatabaseConfigurationValid() { - $databaseConnection = $this->entityManager->getConnection(); + $connection = $this->entityManager->getConnection(); - if (false === $databaseConnection->isConnected()) { + if (!$connection->isConnected()) { try { - $databaseConnection->connect(); + $connection->connect(); } catch (DBALException $e) { return false; } diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php deleted file mode 100644 index 980093ca..00000000 --- a/src/DataFixtures/AppFixtures.php +++ /dev/null @@ -1,14 +0,0 @@ -