updated code for web installation
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -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('<error>Invalid database configuration.</error>');
|
||||
|
||||
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('<info>Fresh database detected. Creating schema...</info>');
|
||||
|
||||
// Create schema
|
||||
$this->runCommand('doctrine:schema:create', ['--no-interaction' => true], $output);
|
||||
|
||||
// Load fixtures
|
||||
$output->writeln('<info>Loading default fixtures...</info>');
|
||||
$this->runCommand('doctrine:fixtures:load', ['--no-interaction' => true, '--quiet' => true], $output);
|
||||
|
||||
$output->writeln('<info>Fresh installation complete.</info>');
|
||||
|
||||
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('<info>Database migrated to latest version.</info>');
|
||||
} else {
|
||||
$output->writeln('<info>Database is already up to date.</info>');
|
||||
}
|
||||
} catch (NoChangesDetected $e) {
|
||||
// Do nothing ...
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('<error>Error during migration: ' . $e->getMessage() . '</error>');
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\DataFixtures;
|
||||
|
||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
|
||||
class AppFixtures extends Fixture
|
||||
{
|
||||
public function load(ObjectManager $manager)
|
||||
{
|
||||
// Add fixtures here...
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user