diff --git a/Classes/Command/DiffCommand.php b/Classes/Command/DiffCommand.php deleted file mode 100644 index dd7e1d8..0000000 --- a/Classes/Command/DiffCommand.php +++ /dev/null @@ -1,43 +0,0 @@ -doctrineService = $doctrineService ?? GeneralUtility::makeInstance(DoctrineService::class); - parent::__construct(); - } - - protected function configure(): void - { - parent::configure(); - - $this->addOption( - 'connection', - null, - InputOption::VALUE_OPTIONAL, - 'The DB connection to use.' - ); - } - - public function initialize(InputInterface $input, OutputInterface $output): void - { - $connectionName = $input->getOption('connection') ?? 'Default'; - $this->configuration = $this->doctrineService->getMigrationConfiguration($connectionName); - - parent::initialize($input, $output); - } -} diff --git a/Classes/Command/ExecuteCommand.php b/Classes/Command/ExecuteCommand.php deleted file mode 100644 index 8258a14..0000000 --- a/Classes/Command/ExecuteCommand.php +++ /dev/null @@ -1,46 +0,0 @@ -doctrineService = $doctrineService ?? GeneralUtility::makeInstance(DoctrineService::class); - parent::__construct(); - } - - protected function configure(): void - { - parent::configure(); - - $this->addOption( - 'connection', - null, - InputOption::VALUE_OPTIONAL, - 'The DB connection to use.' - ); - } - - public function initialize(InputInterface $input, OutputInterface $output): void - { - $dryRun = (bool)$input->getOption('dry-run'); - $this->doctrineService->setDryRun($dryRun); - - $connectionName = $input->getOption('connection') ?? 'Default'; - $this->configuration = $this->doctrineService->getMigrationConfiguration($connectionName); - - parent::initialize($input, $output); - } -} diff --git a/Classes/Command/MigrateCommand.php b/Classes/Command/MigrateCommand.php deleted file mode 100644 index 3b12447..0000000 --- a/Classes/Command/MigrateCommand.php +++ /dev/null @@ -1,46 +0,0 @@ -doctrineService = $doctrineService ?? GeneralUtility::makeInstance(DoctrineService::class); - parent::__construct(); - } - - protected function configure(): void - { - parent::configure(); - - $this->addOption( - 'connection', - null, - InputOption::VALUE_OPTIONAL, - 'The DB connection to use.' - ); - } - - public function initialize(InputInterface $input, OutputInterface $output): void - { - $dryRun = (bool)$input->getOption('dry-run'); - $this->doctrineService->setDryRun($dryRun); - - $connectionName = $input->getOption('connection') ?? 'Default'; - $this->configuration = $this->doctrineService->getMigrationConfiguration($connectionName); - - parent::initialize($input, $output); - } -} diff --git a/Classes/Command/RollupCommand.php b/Classes/Command/RollupCommand.php deleted file mode 100644 index 1428de6..0000000 --- a/Classes/Command/RollupCommand.php +++ /dev/null @@ -1,43 +0,0 @@ -doctrineService = $doctrineService ?? GeneralUtility::makeInstance(DoctrineService::class); - parent::__construct(); - } - - protected function configure(): void - { - parent::configure(); - - $this->addOption( - 'connection', - null, - InputOption::VALUE_OPTIONAL, - 'The DB connection to use.' - ); - } - - public function initialize(InputInterface $input, OutputInterface $output): void - { - $connectionName = $input->getOption('connection') ?? 'Default'; - $this->configuration = $this->doctrineService->getMigrationConfiguration($connectionName); - - parent::initialize($input, $output); - } -} diff --git a/Classes/Command/StatusCommand.php b/Classes/Command/StatusCommand.php deleted file mode 100644 index fc1fbed..0000000 --- a/Classes/Command/StatusCommand.php +++ /dev/null @@ -1,43 +0,0 @@ -doctrineService = $doctrineService ?? GeneralUtility::makeInstance(DoctrineService::class); - parent::__construct(); - } - - protected function configure(): void - { - parent::configure(); - - $this->addOption( - 'connection', - null, - InputOption::VALUE_OPTIONAL, - 'The DB connection to use.' - ); - } - - public function initialize(InputInterface $input, OutputInterface $output): void - { - $connectionName = $input->getOption('connection') ?? 'Default'; - $this->configuration = $this->doctrineService->getMigrationConfiguration($connectionName); - - parent::initialize($input, $output); - } -} diff --git a/Classes/Command/UpToDateCommand.php b/Classes/Command/UpToDateCommand.php deleted file mode 100644 index d7632e6..0000000 --- a/Classes/Command/UpToDateCommand.php +++ /dev/null @@ -1,43 +0,0 @@ -doctrineService = $doctrineService ?? GeneralUtility::makeInstance(DoctrineService::class); - parent::__construct(); - } - - protected function configure(): void - { - parent::configure(); - - $this->addOption( - 'connection', - null, - InputOption::VALUE_OPTIONAL, - 'The DB connection to use.' - ); - } - - public function initialize(InputInterface $input, OutputInterface $output): void - { - $connectionName = $input->getOption('connection') ?? 'Default'; - $this->configuration = $this->doctrineService->getMigrationConfiguration($connectionName); - - parent::initialize($input, $output); - } -} diff --git a/Classes/Command/VersionCommand.php b/Classes/Command/VersionCommand.php deleted file mode 100644 index cd423ca..0000000 --- a/Classes/Command/VersionCommand.php +++ /dev/null @@ -1,43 +0,0 @@ -doctrineService = $doctrineService ?? GeneralUtility::makeInstance(DoctrineService::class); - parent::__construct(); - } - - protected function configure(): void - { - parent::configure(); - - $this->addOption( - 'connection', - null, - InputOption::VALUE_OPTIONAL, - 'The DB connection to use.' - ); - } - - public function initialize(InputInterface $input, OutputInterface $output): void - { - $connectionName = $input->getOption('connection') ?? 'Default'; - $this->configuration = $this->doctrineService->getMigrationConfiguration($connectionName); - - parent::initialize($input, $output); - } -} diff --git a/Classes/Migration/AbstractDataHandlerMigration.php b/Classes/Migration/AbstractDataHandlerMigration.php index 4b26349..673143b 100644 --- a/Classes/Migration/AbstractDataHandlerMigration.php +++ b/Classes/Migration/AbstractDataHandlerMigration.php @@ -4,20 +4,15 @@ namespace KayStrobach\Migrations\Migration; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; -use Doctrine\Migrations\Version\Version; use KayStrobach\Migrations\DataHandling\DryRunDataHandler; use KayStrobach\Migrations\Service\DoctrineMigrationCoordinator; -use KayStrobach\Migrations\Service\DoctrineService; +use Psr\Log\LoggerInterface; use TYPO3\CMS\Core\Core\Bootstrap; -use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\DataHandling\DataHandler; -use TYPO3\CMS\Core\Log\LogManager; -use TYPO3\CMS\Core\Package\PackageManager; -use TYPO3\CMS\Core\Service\DependencyOrderingService; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Core\Utility\VersionNumberUtility; /** * Base class for migrations using the TYPO3 DataHandler @@ -40,31 +35,12 @@ abstract class AbstractDataHandlerMigration extends AbstractMigration private DoctrineMigrationCoordinator $doctrineMigrationCoordinator; - private DoctrineService $doctrineService; - - public function __construct(Version $version) + public function __construct(Connection $connection, LoggerInterface $logger) { - parent::__construct($version); + parent::__construct($connection, $logger); $this->doctrineMigrationCoordinator = new DoctrineMigrationCoordinator(); $this->doctrineMigrationCoordinator->setCurrentVersion(static::class); - - if (VersionNumberUtility::convertVersionNumberToInteger(VersionNumberUtility::getCurrentTypo3Version()) < 100000) { - $dependencyOrderingService = GeneralUtility::makeInstance(DependencyOrderingService::class); - $packageManager = GeneralUtility::makeInstance(PackageManager::class, $dependencyOrderingService); - } else { - $packageManager = GeneralUtility::makeInstance(PackageManager::class); - } - - // todo: refactor this with proper dependency injection if possible - /** @var DoctrineService $doctrineService */ - $doctrineService = GeneralUtility::makeInstance( - DoctrineService::class, - $packageManager, - GeneralUtility::makeInstance(ConnectionPool::class), - GeneralUtility::makeInstance(LogManager::class), - ); - $this->doctrineService = $doctrineService; } /** @@ -101,7 +77,7 @@ public function preUp(Schema $schema): void $this->getMigrationCoordinator()->setCurrentVersion($version); - Bootstrap::initializeBackendAuthentication(true); + Bootstrap::initializeBackendAuthentication(); } public function postUp(Schema $schema): void @@ -113,7 +89,7 @@ public function postUp(Schema $schema): void public function preDown(Schema $schema): void { - Bootstrap::initializeBackendAuthentication(true); + Bootstrap::initializeBackendAuthentication(); } /** @@ -124,7 +100,9 @@ public function preDown(Schema $schema): void */ protected function getDataHandler(array $dataMap = [], array $commandMap = []): DataHandler { - $dataHandlerClass = $this->doctrineService->isDryRun() ? DryRunDataHandler::class : DataHandler::class; + // @todo Find a way to pass the information about --dry-run from the Command to here: + //$dataHandlerClass = $isDryRun ? DryRunDataHandler::class : DataHandler::class; + $dataHandlerClass = DataHandler::class; /** @var DataHandler $dataHandler */ $dataHandler = GeneralUtility::makeInstance($dataHandlerClass); diff --git a/Classes/Service/DoctrineService.php b/Classes/Service/DoctrineService.php deleted file mode 100644 index ccf9b56..0000000 --- a/Classes/Service/DoctrineService.php +++ /dev/null @@ -1,183 +0,0 @@ -packageManager = $packageManager ?? $this->getPackageManager(); - $this->connectionPool = $connectionPool ?? GeneralUtility::makeInstance(ConnectionPool::class); - $logManager = $logManager ?? GeneralUtility::makeInstance(LogManager::class); - $this->setLogger($logManager->getLogger(static::class)); - } - - /** - * Return the configuration needed for Migrations. - */ - public function getMigrationConfiguration(string $connectionName, string $filterForPackageKey = null): Configuration - { - $connection = $this->connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); - - $configuration = new Configuration( - $connection, - $this->getOutputWriter() - ); - - $configuration->setMigrationsNamespace('KayStrobach\Migrations\Persistence\Doctrine\Migrations'); - $publicPath = Environment::getPublicPath(); - - GeneralUtility::mkdir_deep($publicPath . '/fileadmin/Migrations'); - $configuration->setMigrationsDirectory($publicPath . '/fileadmin/Migrations'); - - $configuration->setMigrationsTableName(self::MIGRATION_TABLE_NAME); - - $configuration->createMigrationTable(); - - $databasePlatformName = $connection->getDatabasePlatform()->getName(); - foreach ($this->packageManager->getActivePackages() as $package) { - if ($filterForPackageKey !== null && $package->getPackageKey() !== $filterForPackageKey) { - continue; - } - - [$namespace, $path] = $this->getPackageMigrationNamespaceAndDirectory($package); - - if ($namespace === null || $path === null) { - $this->debug(sprintf('Package %s does not contain any migrations', $package->getPackageKey())); - continue; - } - - $plattformPath = $path . ucfirst($databasePlatformName) . '/'; - - if (is_dir($path)) { - $configuration->setMigrationsNamespace($namespace); - - $configuration->registerMigrationsFromDirectory($path); - } - - if (is_dir($plattformPath)) { - $namespace .= ucfirst($databasePlatformName); - $configuration->setMigrationsNamespace($namespace); - $configuration->registerMigrationsFromDirectory($plattformPath); - if ($filterForPackageKey !== null) { - $configuration->setMigrationsDirectory($plattformPath); - } - } elseif ($connectionName === 'Default') { - $configuration->setMigrationsNamespace($namespace); - $configuration->registerMigrationsFromDirectory($path); - - if ($filterForPackageKey !== null) { - $configuration->setMigrationsDirectory($path); - } - } - } - return $configuration; - } - - private function getOutputWriter(): OutputWriter - { - $this->output = []; - return new OutputWriter( - function (string $message) { - $outputFormatter = new OutputFormatter(true); - echo $outputFormatter->format($message); - $this->debug($message); - $this->output[] = $message; - } - ); - } - - /** - * @return array{0: string, 1: string} [The namespace, the full package directory] - */ - private function getPackageMigrationNamespaceAndDirectory(PackageInterface $package): array - { - $autoloadComposerDefinition = $package->getValueFromComposerManifest('autoload'); - - if (($autoloadComposerDefinition->{'psr-4'} ?? null) instanceof \stdClass) { - $psr4Namespaces = get_object_vars($autoloadComposerDefinition->{'psr-4'}); - foreach ($psr4Namespaces as $namespace => $dir) { - if (strpos($namespace, '\\Migrations\\')) { - $fullDir = $package->getPackagePath() . $dir . '/'; - - return [$namespace, $fullDir]; - } - } - } - - return [null, null]; - } - - public function setDryRun(bool $dryRun): void - { - $this->dryRun = $dryRun; - } - - public function isDryRun(): bool - { - return $this->dryRun; - } - - public function getLogger(): ?LoggerInterface - { - return $this->logger; - } - - public function debug(string $message): void - { - if ($this->getLogger()) { - $this->getLogger()->debug($message); - } - } - - protected function getPackageManager(): PackageManager - { - if (VersionNumberUtility::convertVersionNumberToInteger(VersionNumberUtility::getCurrentTypo3Version()) < 100000) { - $dependencyOrderingService = GeneralUtility::makeInstance(DependencyOrderingService::class); - $packageManager = GeneralUtility::makeInstance(PackageManager::class, $dependencyOrderingService); - } else { - $packageManager = GeneralUtility::makeInstance(PackageManager::class); - } - - return $packageManager; - } -} diff --git a/Classes/Tester/DoctrineCommandRunner.php b/Classes/Tester/DoctrineCommandRunner.php index 1bbe5ae..cf766d7 100644 --- a/Classes/Tester/DoctrineCommandRunner.php +++ b/Classes/Tester/DoctrineCommandRunner.php @@ -4,7 +4,7 @@ namespace KayStrobach\Migrations\Tester; -use KayStrobach\Migrations\Command\MigrateCommand; +use Doctrine\Migrations\Tools\Console\Command\MigrateCommand; use Psr\Container\ContainerInterface; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\QuestionHelper; diff --git a/Classes/Typo3ConfigurationLoader.php b/Classes/Typo3ConfigurationLoader.php new file mode 100644 index 0000000..e0d8220 --- /dev/null +++ b/Classes/Typo3ConfigurationLoader.php @@ -0,0 +1,93 @@ +packageManager = $packageManager; + $this->connectionPool = $connectionPool; + $this->setLogger($logManager->getLogger()); + } + + public function getConfiguration(): Configuration + { + $configuration = new Configuration(); + + GeneralUtility::mkdir_deep(Environment::getVarPath() . '/migrations'); + $configuration->addMigrationsDirectory( + 'KayStrobach\Migrations\Persistence\Doctrine\Migrations', + Environment::getVarPath() . '/migrations' + ); + + // Store in our own doctrine_migrationstatus table + $metadataStorageConfiguration = new TableMetadataStorageConfiguration(); + $metadataStorageConfiguration->setTableName(self::MIGRATION_TABLE_NAME); + $configuration->setMetadataStorageConfiguration($metadataStorageConfiguration); + + $connection = $this->connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); + $databasePlatformName = $connection->getDatabasePlatform()->getName(); + + foreach ($this->packageManager->getActivePackages() as $package) { + [$namespace, $path] = $this->getPackageMigrationNamespaceAndDirectory($package); + + if ($namespace === null || $path === null) { + $this->logger?->debug(sprintf('Package %s does not contain any migrations', $package->getPackageKey())); + continue; + } + + $plattformPath = $path . ucfirst($databasePlatformName) . '/'; + + if (is_dir($plattformPath)) { + $this->logger?->debug(sprintf('Adding migrations for Package %s', $package->getPackageKey())); + $namespace .= ucfirst($databasePlatformName); + $configuration->addMigrationsDirectory($namespace, $plattformPath); + } + } + return $configuration; + } + + /** + * @return array{0: string|null, 1: string|null} [The namespace, the full package directory] + */ + private function getPackageMigrationNamespaceAndDirectory(PackageInterface $package): array + { + $autoloadComposerDefinition = $package->getValueFromComposerManifest('autoload'); + + if (($autoloadComposerDefinition->{'psr-4'} ?? null) instanceof \stdClass) { + $psr4Namespaces = get_object_vars($autoloadComposerDefinition->{'psr-4'}); + foreach ($psr4Namespaces as $namespace => $dir) { + if (strpos($namespace, '\\Migrations\\')) { + $fullDir = $package->getPackagePath() . $dir . '/'; + + return [$namespace, $fullDir]; + } + } + } + + return [null, null]; + } +} diff --git a/Classes/Typo3DependencyFactory.php b/Classes/Typo3DependencyFactory.php new file mode 100644 index 0000000..b9318b9 --- /dev/null +++ b/Classes/Typo3DependencyFactory.php @@ -0,0 +1,31 @@ +getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); + $connectionLoader = new ExistingConnection($connection); + + return DependencyFactory::fromConnection( + $configurationLoader, + $connectionLoader, + $logManager->getLogger() + ); + } +} diff --git a/Classes/UpgradeWizard/StatusTableUpgradeWizard.php b/Classes/UpgradeWizard/StatusTableUpgradeWizard.php new file mode 100644 index 0000000..254341e --- /dev/null +++ b/Classes/UpgradeWizard/StatusTableUpgradeWizard.php @@ -0,0 +1,144 @@ + + * @throws \Doctrine\DBAL\Exception + */ + private function getVersionMapping(): array + { + $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); + $connection = $connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); + $databasePlatformName = $connection->getDatabasePlatform()->getName(); + + $packageManager = GeneralUtility::makeInstance(PackageManager::class); + $mapping = []; + foreach ($packageManager->getActivePackages() as $package) { + $autoloadComposerDefinition = $package->getValueFromComposerManifest('autoload'); + if (! ($autoloadComposerDefinition->{'psr-4'} ?? null) instanceof \stdClass) { + continue; + } + $psr4Namespaces = get_object_vars($autoloadComposerDefinition->{'psr-4'}); + foreach ($psr4Namespaces as $namespace => $dir) { + if (strpos($namespace, '\\Migrations')) { + $fullDir = $package->getPackagePath() . $dir . ucfirst($databasePlatformName); + $files = glob($fullDir . '/Version*.php'); + if (! is_array($files)) { + continue; + } + foreach ($files as $file) { + $className = rtrim($namespace, '\\') . '\\' . ucfirst($databasePlatformName) + . '\\' . str_replace('.php', '', basename($file)); + $legacyNumber = preg_replace('/[^0-9]/', '', basename($file)); + $mapping[$legacyNumber] = $className; + } + } + } + } + return $mapping; + } + + private function getQueryBuilder(): QueryBuilder + { + $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->tableName); + return $connection->createQueryBuilder(); + } + + /** + * Loop through all available migration classes and update the status table accordingly + * + * @inheritDoc + */ + public function executeUpdate(): bool + { + $queryBuilder = $this->getQueryBuilder(); + foreach ($this->getVersionMapping() as $version => $className) { + $queryBuilder->update($this->tableName) + ->set('version', $className, true, Connection::PARAM_STR) + ->where( + $queryBuilder->expr()->eq( + 'version', + $queryBuilder->createNamedParameter($version, Connection::PARAM_STR) + ) + ); + $queryBuilder->execute(); + } + + return true; + } + + /** + * Check if there is any version stored in the status table which does not contain a "\" + * + * @inheritDoc + */ + public function updateNecessary(): bool + { + $queryBuilder = $this->getQueryBuilder(); + $count = (int)$queryBuilder->count('*') + ->from($this->tableName) + ->where($queryBuilder->expr()->notLike( + 'version', + $queryBuilder->createNamedParameter('%\\\%', Connection::PARAM_STR) + )) + ->executeQuery() + ->fetchOne(); + return $count > 0; + } + + /** + * @inheritDoc + */ + public function getPrerequisites(): array + { + return [DatabaseUpdatedPrerequisite::class]; + } +} diff --git a/Configuration/Commands.php b/Configuration/Commands.php deleted file mode 100644 index fdcab8c..0000000 --- a/Configuration/Commands.php +++ /dev/null @@ -1,32 +0,0 @@ - [ - 'class' => \KayStrobach\Migrations\Command\DiffCommand::class, - 'schedulable' => false, - ], - 'migrations:execute' => [ - 'class' => \KayStrobach\Migrations\Command\ExecuteCommand::class, - 'schedulable' => false, - ], - 'migrations:migrate' => [ - 'class' => \KayStrobach\Migrations\Command\MigrateCommand::class, - 'schedulable' => false, - ], - 'migrations:rollup' => [ - 'class' => \KayStrobach\Migrations\Command\RollupCommand::class, - 'schedulable' => false, - ], - 'migrations:uptodate' => [ - 'class' => \KayStrobach\Migrations\Command\UpToDateCommand::class, - 'schedulable' => false, - ], - 'migrations:status' => [ - 'class' => \KayStrobach\Migrations\Command\StatusCommand::class, - 'schedulable' => false, - ], - 'migrations:version' => [ - 'class' => \KayStrobach\Migrations\Command\VersionCommand::class, - 'schedulable' => false, - ], -]; diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index c8ec0f2..0e30b8c 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -10,51 +10,106 @@ services: arguments: - '@Psr\Container\ContainerInterface' - KayStrobach\Migrations\Command\DiffCommand: + KayStrobach\Migrations\Typo3ConfigurationLoader: arguments: - - '@KayStrobach\Migrations\Service\DoctrineService' + - '@TYPO3\CMS\Core\Package\PackageManager' + - '@TYPO3\CMS\Core\Database\ConnectionPool' + - '@TYPO3\CMS\Core\Log\LogManager' + + KayStrobach\Migrations\Typo3DependencyFactory: + factory: [null, 'create'] + arguments: + - '@TYPO3\CMS\Core\Log\LogManager' + - '@TYPO3\CMS\Core\Database\ConnectionPool' + - '@KayStrobach\Migrations\Typo3ConfigurationLoader' + + Doctrine\Migrations\Tools\Console\Command\CurrentCommand: + arguments: + - '@KayStrobach\Migrations\Typo3DependencyFactory' + tags: + - name: 'console.command' + command: 'migrations:current' + schedulable: false + Doctrine\Migrations\Tools\Console\Command\DiffCommand: + arguments: + - '@KayStrobach\Migrations\Typo3DependencyFactory' tags: - name: 'console.command' command: 'migrations:diff' schedulable: false - KayStrobach\Migrations\Command\ExecuteCommand: + Doctrine\Migrations\Tools\Console\Command\DumpSchemaCommand: arguments: - - '@KayStrobach\Migrations\Service\DoctrineService' + - '@KayStrobach\Migrations\Typo3DependencyFactory' + tags: + - name: 'console.command' + command: 'migrations:dump-schema' + schedulable: false + Doctrine\Migrations\Tools\Console\Command\ExecuteCommand: + arguments: + - '@KayStrobach\Migrations\Typo3DependencyFactory' tags: - name: 'console.command' command: 'migrations:execute' schedulable: false - KayStrobach\Migrations\Command\MigrateCommand: + Doctrine\Migrations\Tools\Console\Command\GenerateCommand: arguments: - - '@KayStrobach\Migrations\Service\DoctrineService' + - '@KayStrobach\Migrations\Typo3DependencyFactory' + tags: + - name: 'console.command' + command: 'migrations:generate' + schedulable: false + Doctrine\Migrations\Tools\Console\Command\LatestCommand: + arguments: + - '@KayStrobach\Migrations\Typo3DependencyFactory' + tags: + - name: 'console.command' + command: 'migrations:latest' + schedulable: false + Doctrine\Migrations\Tools\Console\Command\ListCommand: + arguments: + - '@KayStrobach\Migrations\Typo3DependencyFactory' + tags: + - name: 'console.command' + command: 'migrations:list' + schedulable: false + Doctrine\Migrations\Tools\Console\Command\MigrateCommand: + arguments: + - '@KayStrobach\Migrations\Typo3DependencyFactory' tags: - name: 'console.command' command: 'migrations:migrate' schedulable: false - KayStrobach\Migrations\Command\RollupCommand: + Doctrine\Migrations\Tools\Console\Command\RollupCommand: arguments: - - '@KayStrobach\Migrations\Service\DoctrineService' + - '@KayStrobach\Migrations\Typo3DependencyFactory' tags: - name: 'console.command' command: 'migrations:rollup' schedulable: false - KayStrobach\Migrations\Command\UpToDateCommand: + Doctrine\Migrations\Tools\Console\Command\StatusCommand: arguments: - - '@KayStrobach\Migrations\Service\DoctrineService' + - '@KayStrobach\Migrations\Typo3DependencyFactory' tags: - name: 'console.command' - command: 'migrations:uptodate' + command: 'migrations:status' schedulable: false - KayStrobach\Migrations\Command\StatusCommand: + Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand: arguments: - - '@KayStrobach\Migrations\Service\DoctrineService' + - '@KayStrobach\Migrations\Typo3DependencyFactory' tags: - name: 'console.command' - command: 'migrations:status' + command: 'migrations:sync-metadata-storage' + schedulable: false + Doctrine\Migrations\Tools\Console\Command\UpToDateCommand: + arguments: + - '@KayStrobach\Migrations\Typo3DependencyFactory' + tags: + - name: 'console.command' + command: 'migrations:up-to-date' schedulable: false - KayStrobach\Migrations\Command\VersionCommand: + Doctrine\Migrations\Tools\Console\Command\VersionCommand: arguments: - - '@KayStrobach\Migrations\Service\DoctrineService' + - '@KayStrobach\Migrations\Typo3DependencyFactory' tags: - name: 'console.command' command: 'migrations:version' diff --git a/README.md b/README.md index c9651c9..515e858 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Migrate with doctrine migrations -__It's strongly recommend to use the "typo3" CLI binary!__ +This extension uses `doctrine/migrations` to migrate the database tables. + +__You should use the "typo3" CLI binary or "typo3cms" console and not `bin/doctrine-migrations`!__ To get the status of your migrations you can run: @@ -10,28 +12,18 @@ To execute all pending migrations you can run: /typo3 migrations:migrate -This will give you an output like this: +# Upgrade from TYPO3 9/10 to 11/12 +Moving to TYPO3 11, this extensions switched from doctrine/migration 2.x to 3.x for compatibility +with PHP 8. + +After running the Database Compare migrations, don't forget to run the Upgrade Wizards which will +migrate the table `migration_statustable`: ``` - == Configuration - >> Name: Doctrine Database Migrations - >> Database Driver: pdo_mysql - >> Database Name: myproject - >> Configuration Source: manually configured - >> Version Table Name: doctrine_migrationstatus - >> Migrations Namespace: KayStrobach\Migrations\Persistence\Doctrine\Migrations - >> Migrations Target Directory: /var/www/my-project//fileadmin/Migrations - >> Current Version: 0 - >> Latest Version: 2014-07-14 18:44:53 (20140714184453) - >> Executed Migrations: 0 - >> Available Migrations: 1 - >> New Migrations: 1 - - == Migration Versions - >> 2014-07-14 18:44:53 (20140714184453) migrations not migrated +bin/typo3 upgrade:run migration_statustable ``` -This extension uses `doctrine/migrations` to migrate the database tables. +Replace the `@namespace` setting with the namespace of your extension. # Own migration diff --git a/Tests/Functional/Command/MigrateCommandTest.php b/Tests/Functional/Command/MigrateCommandTest.php index 02143e0..f3aabaf 100644 --- a/Tests/Functional/Command/MigrateCommandTest.php +++ b/Tests/Functional/Command/MigrateCommandTest.php @@ -4,8 +4,8 @@ namespace KayStrobach\Migrations\Tests\Functional\Command; -use KayStrobach\Migrations\Command\MigrateCommand; -use KayStrobach\Migrations\Service\DoctrineService; +use Doctrine\Migrations\Tools\Console\Command\MigrateCommand; +use KayStrobach\Migrations\Typo3ConfigurationLoader; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Tester\CommandTester; @@ -13,7 +13,7 @@ use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; /** - * @covers \KayStrobach\Migrations\Command\MigrateCommand + * @covers \Doctrine\Migrations\Tools\Console\Command\MigrateCommand */ class MigrateCommandTest extends FunctionalTestCase { @@ -35,12 +35,13 @@ public function migrationCommandExecutesMigrationsDefinedInExtensionWhenMigratio $commandTester->assertCommandIsSuccessful(); + /** @var \TYPO3\CMS\Core\Database\Connection $connection */ $connection = $this->get(ConnectionPool::class) - ->getConnectionForTable(DoctrineService::MIGRATION_TABLE_NAME); - $result = $connection->select(['*'], DoctrineService::MIGRATION_TABLE_NAME)->fetchAllAssociative(); + ->getConnectionForTable(Typo3ConfigurationLoader::MIGRATION_TABLE_NAME); + $result = $connection->select(['*'], Typo3ConfigurationLoader::MIGRATION_TABLE_NAME)->fetchAllAssociative(); self::assertCount(1, $result); - self::assertSame('20230804102700', $result[0]['version']); + self::assertSame('KayStrobach\\Migrations\\TestFixtures\\Migrations\\Mysql\\Version20230804102700', $result[0]['version']); $result = $connection->select(['*'], 'pages', ['uid' => 1])->fetchAllAssociative(); self::assertSame('My test page', $result[0]['title']); diff --git a/Tests/Functional/Migration/AbstractDataHandlerMigrationTest.php b/Tests/Functional/Migration/AbstractDataHandlerMigrationTest.php index 8fc455a..5e1e256 100644 --- a/Tests/Functional/Migration/AbstractDataHandlerMigrationTest.php +++ b/Tests/Functional/Migration/AbstractDataHandlerMigrationTest.php @@ -4,8 +4,8 @@ namespace KayStrobach\Migrations\Tests\Functional\Migration; -use KayStrobach\Migrations\Service\DoctrineService; use KayStrobach\Migrations\Tester\DoctrineCommandRunner; +use KayStrobach\Migrations\Typo3ConfigurationLoader; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication; use TYPO3\CMS\Core\Core\Bootstrap; @@ -30,12 +30,13 @@ public function dataHandlerMigrationRunsDataHandler(): void $GLOBALS['BE_USER']->workspace = 0; $this->get(DoctrineCommandRunner::class)->executeMigrateCommand(); + /** @var \TYPO3\CMS\Core\Database\Connection $connection */ $connection = $this->get(ConnectionPool::class) - ->getConnectionForTable(DoctrineService::MIGRATION_TABLE_NAME); - $result = $connection->select(['*'], DoctrineService::MIGRATION_TABLE_NAME)->fetchAllAssociative(); + ->getConnectionForTable(Typo3ConfigurationLoader::MIGRATION_TABLE_NAME); + $result = $connection->select(['*'], Typo3ConfigurationLoader::MIGRATION_TABLE_NAME)->fetchAllAssociative(); self::assertCount(1, $result, 'No or more than one migration was executed'); - self::assertSame('20230804162200', $result[0]['version']); + self::assertSame('KayStrobach\\Migrations\\TestFixtures\\Migrations\\Mysql\\Version20230804162200', $result[0]['version']); $result = BackendUtility::getRecord('pages', 1); self::assertIsArray($result); diff --git a/composer.json b/composer.json index 6412371..c22322e 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "symfony/console": "^5.4 || ^6.2", "symfony/filesystem": "^5.4 || ^6.2", "typo3/coding-standards": "^0.5.4", - "typo3/testing-framework": "^7.0 || ^8.0" + "typo3/testing-framework": "^7.0.4 || ^8.0" }, "autoload": { "psr-4": { @@ -64,7 +64,7 @@ "scripts": { "cs:check": "php-cs-fixer fix --config .php-cs-fixer.php -v --dry-run --diff", "cs:fix": "php-cs-fixer fix --config .php-cs-fixer.php -v --diff", - "php:static": "phpstan analyze --no-interaction", + "php:static": "phpstan analyze --no-interaction --memory-limit=1G", "test:php:functional": "phpunit -c ./Build/FunctionalTests.xml" } } diff --git a/ext_localconf.php b/ext_localconf.php index 7be3ada..91b078f 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -2,3 +2,6 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = \KayStrobach\Migrations\Hooks\DataHandlerVersionAddHook::class; + +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['migration_statustable'] + = \KayStrobach\Migrations\UpgradeWizard\StatusTableUpgradeWizard::class; diff --git a/ext_tables.sql b/ext_tables.sql index ba37757..9ac4188 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -10,6 +10,7 @@ CREATE TABLE tt_content CREATE TABLE doctrine_migrationstatus ( - version VARCHAR(14) NOT NULL PRIMARY KEY, - executed_at DATETIME NOT NULL + version VARCHAR(191) NOT NULL PRIMARY KEY, + executed_at DATETIME DEFAULT NULL, + execution_time INT(11) DEFAULT NULL, ); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 5c84ae8..e69de29 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,16 +0,0 @@ -parameters: - ignoreErrors: - - - message: "#^Method KayStrobach\\\\Migrations\\\\Service\\\\DoctrineService\\:\\:getPackageMigrationNamespaceAndDirectory\\(\\) should return array\\{string, string\\} but returns array\\{null, null\\}\\.$#" - count: 1 - path: Classes/Service/DoctrineService.php - - - - message: "#^Result of \\|\\| is always false\\.$#" - count: 1 - path: Classes/Service/DoctrineService.php - - - - message: "#^Strict comparison using \\=\\=\\= between string and null will always evaluate to false\\.$#" - count: 2 - path: Classes/Service/DoctrineService.php