Neos\Flow\Persistence\Doctrine\Service::generateMigration PHP Метод

generateMigration() публичный Метод

If $diffAgainstCurrent is TRUE, it generates a migration file with the diff between current DB structure and the found mapping metadata. Only include tables/sequences matching the $filterExpression regexp when diffing models and existing schema. Otherwise an empty migration skeleton is generated.
public generateMigration ( boolean $diffAgainstCurrent = true, string $filterExpression = null ) : string
$diffAgainstCurrent boolean
$filterExpression string
Результат string Path to the new file
    public function generateMigration($diffAgainstCurrent = true, $filterExpression = null)
    {
        $configuration = $this->getMigrationConfiguration();
        $up = null;
        $down = null;
        if ($diffAgainstCurrent === true) {
            /** @var \Doctrine\DBAL\Connection $connection */
            $connection = $this->entityManager->getConnection();
            if ($filterExpression) {
                $connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
            }
            $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
            if (empty($metadata)) {
                return ['No mapping information to process.', null];
            }
            $tool = new SchemaTool($this->entityManager);
            $fromSchema = $connection->getSchemaManager()->createSchema();
            $toSchema = $tool->getSchemaFromMetadata($metadata);
            if ($filterExpression) {
                foreach ($toSchema->getTables() as $table) {
                    $tableName = $table->getName();
                    if (!preg_match($filterExpression, $this->resolveTableName($tableName))) {
                        $toSchema->dropTable($tableName);
                    }
                }
                foreach ($toSchema->getSequences() as $sequence) {
                    $sequenceName = $sequence->getName();
                    if (!preg_match($filterExpression, $this->resolveTableName($sequenceName))) {
                        $toSchema->dropSequence($sequenceName);
                    }
                }
            }
            $platform = $connection->getDatabasePlatform();
            $up = $this->buildCodeFromSql($configuration, $fromSchema->getMigrateToSql($toSchema, $platform));
            $down = $this->buildCodeFromSql($configuration, $fromSchema->getMigrateFromSql($toSchema, $platform));
            if (!$up && !$down) {
                return ['No changes detected in your mapping information.', null];
            }
        }
        return ['Generated new migration class!', $this->writeMigrationClassToFile($configuration, $up, $down)];
    }

Usage Example

Пример #1
0
 /**
  * Generate a new migration
  *
  * If $diffAgainstCurrent is TRUE (the default), it generates a migration file
  * with the diff between current DB structure and the found mapping metadata.
  *
  * Otherwise an empty migration skeleton is generated.
  *
  * Only includes tables/sequences matching the $filterExpression regexp when
  * diffing models and existing schema. Include delimiters in the expression!
  * The use of
  *
  *  --filter-expression '/^acme_com/'
  *
  * would only create a migration touching tables starting with "acme_com".
  *
  * Note: A filter-expression will overrule any filter configured through the
  * Neos.Flow.persistence.doctrine.migrations.ignoredTables setting
  *
  * @param boolean $diffAgainstCurrent Whether to base the migration on the current schema structure
  * @param string $filterExpression Only include tables/sequences matching the filter expression regexp
  * @return void
  * @see neos.flow:doctrine:migrate
  * @see neos.flow:doctrine:migrationstatus
  * @see neos.flow:doctrine:migrationexecute
  * @see neos.flow:doctrine:migrationversion
  */
 public function migrationGenerateCommand($diffAgainstCurrent = true, $filterExpression = null)
 {
     // "driver" is used only for Doctrine, thus we (mis-)use it here
     // additionally, when no host is set, skip this step, assuming no DB is needed
     if (!$this->isDatabaseConfigured()) {
         $this->outputLine('Doctrine migration generation has been SKIPPED, the driver and host backend options are not set in /Configuration/Settings.yaml.');
         $this->quit(1);
     }
     // use default filter expression from settings
     if ($filterExpression === null) {
         $ignoredTables = array_keys(array_filter($this->settings['doctrine']['migrations']['ignoredTables']));
         if ($ignoredTables !== array()) {
             $filterExpression = sprintf('/^(?!%s$).*$/xs', implode('$|', $ignoredTables));
         }
     }
     list($status, $migrationClassPathAndFilename) = $this->doctrineService->generateMigration($diffAgainstCurrent, $filterExpression);
     $this->outputLine('<info>%s</info>', [$status]);
     $this->outputLine();
     if ($migrationClassPathAndFilename) {
         $choices = ['Don\'t Move'];
         $packages = [null];
         /** @var Package $package */
         foreach ($this->packageManager->getAvailablePackages() as $package) {
             $type = $package->getComposerManifest('type');
             if ($type === null || strpos($type, 'typo3-') !== 0 && strpos($type, 'neos-') !== 0) {
                 continue;
             }
             $choices[] = $package->getPackageKey();
             $packages[] = $package;
         }
         $selectedPackageIndex = (int) $this->output->select('Do you want to move the migration to one of these packages?', $choices, 0);
         $this->outputLine();
         if ($selectedPackageIndex !== 0) {
             /** @var Package $selectedPackage */
             $selectedPackage = $packages[$selectedPackageIndex];
             $targetPathAndFilename = Files::concatenatePaths([$selectedPackage->getPackagePath(), 'Migrations', $this->doctrineService->getDatabasePlatformName(), basename($migrationClassPathAndFilename)]);
             Files::createDirectoryRecursively(dirname($targetPathAndFilename));
             rename($migrationClassPathAndFilename, $targetPathAndFilename);
             $this->outputLine('The migration was moved to: <comment>%s</comment>', [substr($targetPathAndFilename, strlen(FLOW_PATH_PACKAGES))]);
             $this->outputLine();
             $this->outputLine('Next Steps:');
         } else {
             $this->outputLine('Next Steps:');
             $this->outputLine(sprintf('- Move <comment>%s</comment> to YourPackage/<comment>Migrations/%s/</comment>', $migrationClassPathAndFilename, $this->doctrineService->getDatabasePlatformName()));
         }
         $this->outputLine('- Review and adjust the generated migration.');
         $this->outputLine('- (optional) execute the migration using <comment>%s doctrine:migrate</comment>', [$this->getFlowInvocationString()]);
     }
 }