Gets the sequence of SQL statements that need to be performed in order
to bring the given class mappings in-synch with the relational schema.
public getUpdateSchemaSql ( array $classes, $saveMode = false ) : array | ||
$classes | array | The classes to consider. |
return | array | The sequence of SQL statements. |
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { // Defining if update is complete or not (--complete not defined means $saveMode = true) $saveMode = $input->getOption('complete') !== true; if ($input->getOption('dump-sql') === true) { $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { if ($input->getOption('force') === true) { $output->write('Updating database schema...' . PHP_EOL); $schemaTool->updateSchema($metadatas, $saveMode); $output->write('Database schema updated successfully!' . PHP_EOL); } else { $output->write('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL); $output->write('Use the incremental update to detect changes during development and use' . PHP_EOL); $output->write('this SQL DDL to manually update your database in production.' . PHP_EOL . PHP_EOL); $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); if (count($sqls)) { $output->write('Schema-Tool would execute ' . count($sqls) . ' queries to update the database.' . PHP_EOL); $output->write('Please run the operation with --force to execute these queries or use --dump-sql to see them.' . PHP_EOL); } else { $output->write('Nothing to update. The database is in sync with the current entity metadata.' . PHP_EOL); } } } }
public function execute() { $d = $this->factory()->appDoctrine(); $this->app->dataStorage->clearStorage('CyantreeDoctrineModule/Proxies'); $this->app->cacheStorage->clearStorage('CyantreeDoctrineModule'); $tool = new SchemaTool($d); $queries = $tool->getUpdateSchemaSql($d->getMetadataFactory()->getAllMetadata()); if ($queries) { $hash = ''; foreach ($queries as $query) { $hash = md5($hash . $query); } $code = substr($hash, 0, 8); if ($transferredCode = $this->request->args->get('code')) { if ($transferredCode == $code) { $tool->updateSchema($d->getMetadataFactory()->getAllMetadata()); $this->result->showSuccess('Schema has been updated.'); return; } else { $this->result->showError('Passed invalid code.'); } } $this->result->showWarning('Schema is not up to date. The following queries would be executed:'); foreach ($queries as $query) { $this->result->showInfo($query); } $this->result->showWarning("Execute with -code={$code} to update the database schema."); } else { $this->result->showSuccess('Schema is up to date.'); } }
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { // Defining if update is complete or not (--complete not defined means $saveMode = true) $saveMode = $input->getOption('complete') !== true; $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); if (0 == count($sqls)) { $output->writeln('Nothing to update - your database is already in sync with the current entity metadata.'); return; } $dumpSql = true === $input->getOption('dump-sql'); $force = true === $input->getOption('force'); if ($dumpSql && $force) { throw new \InvalidArgumentException('You can pass either the --dump-sql or the --force option (but not both simultaneously).'); } if ($dumpSql) { $output->writeln(implode(';' . PHP_EOL, $sqls)); } else { if ($force) { $output->writeln('Updating database schema...'); $schemaTool->updateSchema($metadatas, $saveMode); $output->writeln(sprintf('Database schema updated successfully! "<info>%s</info>" queries were executed', count($sqls))); } else { $output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.'); $output->writeln(' Use the incremental update to detect changes during development and use'); $output->writeln(' the SQL DDL provided to manually update your database in production.'); $output->writeln(''); $output->writeln(sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls))); $output->writeln('Please run the operation by passing one of the following options:'); $output->writeln(sprintf(' <info>%s --force</info> to execute the command', $this->getName())); $output->writeln(sprintf(' <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName())); } } }
public function updateAction() { $console = $this->getServiceLocator()->get('console'); // Make sure that we are running in a console and the user has not tricked our // application into running this action from a public web server. $request = $this->getRequest(); if (!$request instanceof ConsoleRequest) { throw new RuntimeException('You can only use this action from a console.'); } $classes = array(); $metas = $this->getAuditObjectManager()->getMetadataFactory()->getAllMetadata(); foreach ($metas as $meta) { $classes[] = $meta; } $schemaTool = new SchemaTool($this->getAuditObjectManager()); try { $result = $schemaTool->getUpdateSchemaSql($classes, false); } catch (\Exception $e) { $console->write($e->getCode() . ": " . $e->getMessage()); $console->write("\nExiting now"); return; } foreach ($result as $sql) { $console->write($sql . ";\n"); } }
/** * Runs command * * @param InputInterface $input * @param OutputInterface $output * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $output->writeln($this->getDescription()); /** @var EntityManager $em */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); /** @var ConfigManager $configManager */ $configManager = $this->getContainer()->get('oro_entity_config.config_manager'); $metadata = array_filter($em->getMetadataFactory()->getAllMetadata(), function ($doctrineMetadata) use($configManager) { /** @var ClassMetadataInfo $doctrineMetadata */ return $this->isExtendEntity($doctrineMetadata->getReflectionClass()->getName(), $configManager); }); $schemaTool = new SchemaTool($em); $sqls = $schemaTool->getUpdateSchemaSql($metadata, true); if (0 === count($sqls)) { $output->writeln('Nothing to update - a database is already in sync with the current entity metadata.'); } else { if ($input->getOption('dry-run')) { $output->writeln(implode(';' . PHP_EOL, $sqls) . ';'); } else { $output->writeln('Updating database schema...'); $schemaTool->updateSchema($metadata, true); $output->writeln(sprintf('Database schema updated successfully! "<info>%s</info>" queries were executed', count($sqls))); } } if (!$input->getOption('dry-run')) { /** @var EnumSynchronizer $enumSynchronizer */ $enumSynchronizer = $this->getContainer()->get('oro_entity_extend.enum_synchronizer'); $enumSynchronizer->sync(); } }
/** * main method * * @return void */ public function main() { static $em; if ($em === null) { $wd = getcwd(); $zf = $this->project->getProperty('zf'); $application = (require $zf); if (!$application instanceof Application) { throw new BuildException(sprintf('zf bootstrap file "%s" should return an instance of Zend\\Mvc\\Application', $zf)); } chdir($wd); $em = $application->getServiceManager()->get($this->em); } $metadatas = $em->getMetadataFactory()->getAllMetadata(); if (!empty($metadatas)) { $tool = new SchemaTool($em); $sqls = $tool->getUpdateSchemaSql($metadatas, false); if (0 === count($sqls)) { $this->log('Nothing to update - your database is already in sync with the current entity metadata.'); } else { $this->log('Updating database schema...'); $tool->updateSchema($metadatas, false); $this->log(sprintf('Database schema updated successfully! %s queries were executed', count($sqls))); } } else { $this->log('No metadata classes to process'); } }
public function setUp() { $this->_oldEntityManager = \SoliantEntityAudit\Module::getModuleOptions()->getEntityManager(); $this->_oldAuditedClassNames = \SoliantEntityAudit\Module::getModuleOptions()->getAuditedClassNames(); $this->_oldJoinClasses = \SoliantEntityAudit\Module::getModuleOptions()->resetJoinClasses(); $isDevMode = false; $config = Setup::createConfiguration($isDevMode, null, null); $chain = new DriverChain(); // Use ZFC User for authentication tests $chain->addDriver(new XmlDriver(__DIR__ . '/../../../vendor/zf-commons/zfc-user-doctrine-orm/config/xml/zfcuser'), 'ZfcUser\\Entity'); $chain->addDriver(new XmlDriver(__DIR__ . '/../../../vendor/zf-commons/zfc-user-doctrine-orm/config/xml/zfcuserdoctrineorm'), 'ZfcUserDoctrineORM\\Entity'); $chain->addDriver(new StaticPHPDriver(__DIR__ . "/../Models"), 'SoliantEntityAuditTest\\Models\\LogRevision'); $chain->addDriver(new AuditDriver('.'), 'SoliantEntityAudit\\Entity'); $config->setMetadataDriverImpl($chain); // Replace entity manager $moduleOptions = \SoliantEntityAudit\Module::getModuleOptions(); $conn = array('driver' => 'pdo_sqlite', 'memory' => true); $moduleOptions->setAuditedClassNames(array('SoliantEntityAuditTest\\Models\\LogRevision\\Album' => array(), 'SoliantEntityAuditTest\\Models\\LogRevision\\Performer' => array(), 'SoliantEntityAuditTest\\Models\\LogRevision\\Song' => array(), 'SoliantEntityAuditTest\\Models\\LogRevision\\SingleCoverArt' => array())); $entityManager = EntityManager::create($conn, $config); $moduleOptions->setEntityManager($entityManager); $schemaTool = new SchemaTool($entityManager); // Add auditing listener $entityManager->getEventManager()->addEventSubscriber(new LogRevision()); $sql = $schemaTool->getUpdateSchemaSql($entityManager->getMetadataFactory()->getAllMetadata()); #print_r($sql);die(); $schemaTool->createSchema($entityManager->getMetadataFactory()->getAllMetadata()); $this->_em = $entityManager; }
private function getUpdateBundleSqlSchema() { $sql = array(); foreach ($this->_application->getBundles() as $bundle) { $classes = $this->_getBundleSchema($bundle); $sql = array_merge($sql, $this->_schemaTool->getUpdateSchemaSql($classes, true)); } return $sql; }
/** * Execute the console command. * * @return void */ public function fire() { $this->info('Checking if database needs updating....'); $clean = $this->option('clean'); $sql = $this->tool->getUpdateSchemaSql($this->metadata->getAllMetadata(), !$clean); if (empty($sql)) { $this->info('No updates found.'); return; } if ($this->option('sql')) { $this->info('Outputting update query:'); $this->info(implode(';' . PHP_EOL, $sql)); } else { $this->info('Updating database schema....'); $this->tool->updateSchema($this->metadata->getAllMetadata(), !$clean); $this->info('Schema has been updated!'); } }
/** * @return string[] */ protected function getUpdateQueries() { $cache = $this->entityManager->getConfiguration()->getMetadataCacheImpl(); if ($cache instanceof ClearableCache) { $cache->deleteAll(); } $schemaTool = new SchemaTool($this->entityManager); $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata(); $queries = $schemaTool->getUpdateSchemaSql($metadata, TRUE); return $queries; }
/** * @group DDC-1657 */ public function testUpdateSchemaWithPostgreSQLSchema() { $classes = array($this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1657Screen'), $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1657Avatar')); $tool = new SchemaTool($this->_em); $tool->createSchema($classes); $sql = $tool->getUpdateSchemaSql($classes); $sql = array_filter($sql, function ($sql) { return strpos($sql, "DROP SEQUENCE stonewood.") === 0; }); $this->assertCount(0, $sql, implode("\n", $sql)); }
public function fire() { $this->info('Checking if database needs updating....'); $sql = $this->tool->getUpdateSchemaSql($this->metadata->getAllMetadata(), $this->option('clean')); if (empty($sql)) { $this->info('No updates found.'); return; } if ($this->option('sql')) { $this->info('Outputting update query:'); $this->info(implode(';' . PHP_EOL, $sql) . ';'); } else { if ($this->option('commit')) { $this->info('Updating database schema....'); $this->tool->updateSchema($this->metadata->getAllMetadata()); $this->info('Schema has been updated!'); } else { $this->comment("Warning: this command can cause data loss. Run with --sql or --commit."); } } }
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { // Defining if update is complete or not (--complete not defined means $saveMode = true) $saveMode = $input->getOption('complete') === true; if ($input->getOption('dump-sql') === true) { $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { $output->write('Updating database schema...' . PHP_EOL); $schemaTool->updateSchema($metadatas, $saveMode); $output->write('Database schema updated successfully!' . PHP_EOL); } }
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { // Defining if update is complete or not (--complete not defined means $saveMode = true) $saveMode = $input->getOption('complete') !== true; if ($input->getOption('dump-sql') === true) { $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { if ($input->getOption('force') === true) { $output->write('Updating database schema...' . PHP_EOL); $schemaTool->updateSchema($metadatas, $saveMode); $output->write('Database schema updated successfully!' . PHP_EOL); } else { $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); if (count($sqls)) { $output->write('Schema-Tool would execute ' . count($sqls) . ' queries to update the database.' . PHP_EOL); $output->write('Please run the operation with --force to execute these queries or use --dump-sql to see them.' . PHP_EOL); } else { $output->write('Nothing to update. The database is in sync with the current entity metadata.' . PHP_EOL); } } } }
private function checkThatNewConstraintsCanBeApplied(EntityManager $em) { $tool = new SchemaTool($em); $metas = $em->getMetadataFactory()->getAllMetadata(); // Bad ordering of Indexes changes in Doctrine DBAL-2.5 $sqls = $tool->getUpdateSchemaSql($metas, true); foreach ($sqls as $sql) { if ('DROP INDEX usr_id ON Sessions' === $sql) { $em->getConnection()->executeQuery($sql); } } // End patching DBAL-2.5 $tool->updateSchema($metas, true); }
public function installEntity(array $entity, $force = false) { if ($force) { $this->uninstallEntity($entity); } $metadata = $this->getMetadata($entity); $all_metadata = $this->getAllMetadata($entity); $tool = new SchemaTool($this->_em); $queries = array_merge($tool->getCreateSchemaSql($metadata), array_filter($tool->getUpdateSchemaSql($all_metadata), function ($query) { if (strpos(strtoupper($query), 'DROP') === false) { return $query; } }) ?: array()); return $this->persist($queries ?: array()); }
/** * Update database schema. * * @param boolean $delete Enable DELETEĀ and DROP statements * * @return boolean */ public function updateSchema($delete = false) { $this->clearMetadata(); $tool = new SchemaTool($this->entityManager); $meta = $this->entityManager->getMetadataFactory()->getAllMetadata(); $sql = $tool->getUpdateSchemaSql($meta, true); $deletions = []; foreach ($sql as $statement) { if (substr($statement, 0, 6) == 'DELETE' || strpos($statement, 'DROP')) { $deletions[] = $statement; } else { $this->entityManager->getConnection()->exec($statement); } } if (true === $delete) { foreach ($deletions as $statement) { $this->entityManager->getConnection()->exec($statement); } } return true; }
/** * @see Oro\Bundle\EntityExtendBundle\Command\UpdateSchemaCommand::execute */ public function testSchema() { $this->overrideRemoveNamespacedAssets(); $this->overrideSchemaDiff(); /** @var ManagerRegistry $registry */ $registry = $this->getContainer()->get('doctrine'); $ignoredQueries = [DatabasePlatformInterface::DATABASE_MYSQL => ['ALTER TABLE oro_search_index_text ADD CONSTRAINT FK_A0243539126F525E FOREIGN KEY (item_id) ' . 'REFERENCES oro_search_item (id)', 'ALTER TABLE oro_audit_field CHANGE old_datetimetz old_datetimetz DATETIME DEFAULT NULL, ' . 'CHANGE new_datetimetz new_datetimetz DATETIME DEFAULT NULL']]; /** @var EntityManager $em */ foreach ($registry->getManagers() as $em) { $schemaTool = new SchemaTool($em); $allMetadata = $em->getMetadataFactory()->getAllMetadata(); $queries = $schemaTool->getUpdateSchemaSql($allMetadata, true); $platform = $em->getConnection()->getDatabasePlatform()->getName(); if (array_key_exists($platform, $ignoredQueries)) { $queries = array_filter($queries, function ($query) use($ignoredQueries, $platform) { return !in_array($query, $ignoredQueries[$platform], true); }); } $this->assertEmpty($queries, implode("\n", $queries)); } }
/** * Execute the console command. * * @param ManagerRegistry $registry */ public function fire(ManagerRegistry $registry) { if (!$this->option('sql') && (!$this->laravel->environment('local') && !$this->option('force'))) { $this->error('ATTENTION: This operation should not be executed in a production environment.'); $this->error('Use the incremental update to detect changes during development and use'); $this->error('the SQL DDL provided to manually update your database in production.'); } $names = $this->option('em') ? [$this->option('em')] : $registry->getManagerNames(); foreach ($names as $name) { $em = $registry->getManager($name); $tool = new SchemaTool($em); $this->comment(''); $this->message('Checking if database connected to <info>' . $name . '</info> entity manager needs updating...', 'blue'); // Check if there are updates available $sql = $tool->getUpdateSchemaSql($em->getMetadataFactory()->getAllMetadata(), !$this->option('clean')); if (0 === count($sql)) { return $this->error('Nothing to update - your database is already in sync with the current entity metadata.'); } if ($this->option('sql')) { $this->comment(' ' . implode('; ' . PHP_EOL, $sql)); } else { if ($this->laravel->environment('local') || $this->option('force')) { $this->message('Updating database schema...', 'blue'); $tool->updateSchema($em->getMetadataFactory()->getAllMetadata(), !$this->option('clean')); $pluralization = 1 === count($sql) ? 'query was' : 'queries were'; $this->info(sprintf('Database schema updated successfully! "<info>%s</info>" %s executed', count($sql), $pluralization)); } else { $this->message(sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sql))); } } } if (!$this->option('sql') && (!$this->laravel->environment('local') && !$this->option('force'))) { $this->info(''); $this->message('Please run the operation by passing one - or both - of the following options:'); $this->comment(sprintf(' <info>php artisan %s --force</info> to execute the command', $this->getName())); $this->comment(sprintf(' <info>php artisan %s --sql</info> to dump the SQL statements to the screen', $this->getName())); } }
/** * {@inheritdoc} */ public function getUpdateSchemaSql(array $classes, $saveMode = false) { if (false === $saveMode) { return parent::getUpdateSchemaSql($classes, $saveMode); } $schemaDiff = $this->getSchemaDiff($classes); // clean removed indexes(except those which were created by EXTEND, detected by prefix) and removed columns // enable super save mode foreach ($schemaDiff->changedTables as $table) { // does not matter how they were created, extend mechanism does not allow column/association deletion $table->removedColumns = $table->removedForeignKeys = []; $table->removedIndexes = array_filter($table->removedIndexes, function ($idx) { $idxName = null; if ($idx instanceof Index) { $idxName = $idx->getName(); } elseif (is_string($idx)) { $idxName = $idx; } return strpos($idxName, ExtendDbIdentifierNameGenerator::CUSTOM_INDEX_PREFIX) === 0; }); } return $schemaDiff->toSaveSql($this->platform); }
public function generateSchemaSql($return) { $entityManager = EntityHelper::getEntityManager(); $cacheDriver = $entityManager->getConfiguration()->getMetadataCacheImpl(); if ($cacheDriver && !$cacheDriver instanceof ApcCache) { $cacheDriver->deleteAll(); } // force "disconnected" generation of entities $reload = false; EntityGeneration::generate(null, $reload); if ($reload) { $this->reload(); } $metadatas = $entityManager->getMetadataFactory()->getAllMetadata(); $tool = new SchemaTool($entityManager); $queries = $tool->getUpdateSchemaSql($metadatas); $filter = array_map('preg_quote', $GLOBALS['DOCTRINE_MANAGED_TABLE']); $filter = implode('|', $filter); $filter = sprintf('~^(CREATE|ALTER|DROP) TABLE (%s)~', $filter); $queries = array_filter($queries, function ($query) use($filter) { return preg_match($filter, $query); }); return $this->appendQueries($return, $queries); }
/** * Runs command * * @param InputInterface $input * @param OutputInterface $output * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $output->writeln($this->getDescription()); /** @var EntityManager $em */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $metadata = array_filter($em->getMetadataFactory()->getAllMetadata(), function ($doctrineMetadata) { /** @var ClassMetadataInfo $doctrineMetadata */ return strpos($doctrineMetadata->getReflectionClass()->getName(), self::TEST_ENTITY_NAMESPACE) === 0; }); $schemaTool = new SchemaTool($em); $sqls = $schemaTool->getUpdateSchemaSql($metadata, true); if (0 === count($sqls)) { $output->writeln('Nothing to update - a database is already in sync with the current entity metadata.'); } else { if ($input->getOption('dry-run')) { $output->writeln(implode(';' . PHP_EOL, $sqls) . ';'); } else { $output->writeln('Updating database schema...'); $schemaTool->updateSchema($metadata, true); $output->writeln(sprintf('Database schema updated successfully! "<info>%s</info>" queries were executed', count($sqls))); } } $this->loadEntityConfigData($em); }
/** * Check if the Database Schema is in sync with the current metadata state. * * @return bool */ public function schemaInSyncWithMetadata() { $schemaTool = new SchemaTool($this->em); $allMetadata = $this->em->getMetadataFactory()->getAllMetadata(); return count($schemaTool->getUpdateSchemaSql($allMetadata, true)) == 0; }
public static function updateSchema($force = NULL, $eol = "<br />", \Doctrine\ORM\EntityManager $em = NULL) { $em = $em ?: self::em(); if (extension_loaded('apc')) { // damit das update auch durchgeht apc_clear_cache(); } $tool = new SchemaTool($em); $classes = $em->getMetadataFactory()->getAllMetadata(); $log = NULL; foreach ($tool->getUpdateSchemaSql($classes, TRUE) as $sql) { $log .= $sql . ';' . $eol; } if ($force == self::FORCE) { $tool->updateSchema($classes, TRUE); } return $log; }
/** * @inheritdoc */ public function run() { $arguments = $this->getArguments(); $printer = $this->getPrinter(); $isCreate = isset($arguments['create']) && $arguments['create']; $isDrop = isset($arguments['drop']) && $arguments['drop']; $isUpdate = isset($arguments['update']) && $arguments['update']; $isCompleteUpdate = isset($arguments['complete-update']) && $arguments['complete-update']; $em = $this->getConfiguration()->getAttribute('em'); $cmf = $em->getMetadataFactory(); $classes = $cmf->getAllMetadata(); if (empty($classes)) { $printer->writeln('No classes to process.', 'INFO'); return; } $tool = new SchemaTool($em); if ($isDrop) { if (isset($arguments['dump-sql'])) { foreach ($tool->getDropSchemaSql($classes) as $sql) { $printer->writeln($sql . ";"); } } else { $printer->writeln('Dropping database schema...', 'INFO'); $tool->dropSchema($classes); $printer->writeln('Database schema dropped successfully.', 'INFO'); } } if ($isCreate) { if (isset($arguments['dump-sql'])) { foreach ($tool->getCreateSchemaSql($classes) as $sql) { $printer->writeln($sql . ";"); } } else { $printer->writeln('Creating database schema...', 'INFO'); $tool->createSchema($classes); $printer->writeln('Database schema created successfully.', 'INFO'); } } if ($isUpdate || $isCompleteUpdate) { $saveMode = $isUpdate ? true : false; if (isset($arguments['dump-sql'])) { foreach ($tool->getUpdateSchemaSql($classes, $saveMode) as $sql) { $printer->writeln($sql . ";"); } } else { $printer->writeln('Updating database schema...', 'INFO'); $tool->updateSchema($classes, $saveMode); $printer->writeln('Database schema updated successfully.', 'INFO'); } } }
/** * Creates SQL requests to update your bundle entities and executes them against application database * if force is equal to true. * * @param BundleInterface $bundle * @param boolean $force * @return array */ private function updateEntitiesSchema(BundleInterface $bundle, $force = false) { if (!is_dir($entityDir = $this->getBundleEntityDir($bundle))) { return []; } $bundleEntityManager = $bundle->getEntityManager(); $bundleEntityManager->getConfiguration()->getMetadataDriverImpl()->addPaths([$entityDir]); $metadata = $bundleEntityManager->getMetadataFactory()->getAllMetadata(); $schemaTool = new SchemaTool($bundleEntityManager); $sqls = $schemaTool->getUpdateSchemaSql($metadata, true); if (true === $force) { $schemaTool->updateSchema($metadata, true); } return $sqls; }
/** * Update database. * * @static * @access public * @return View * @since 1.2.0-dev * @version 1.2.0-dev */ private static function makeUpdateNoExec() { $entityManager = DB::getEntityManager(); $tool = new ORM\Tools\SchemaTool($entityManager); $classes = []; // get list of Model classes foreach (DB::getModelsNames() as $sClass) { $classes[] = $entityManager->getClassMetadata($sClass); } // make schemas update try { $sql = $tool->getUpdateSchemaSql($classes); /* @var $sql array */ $tool->updateSchema($classes); $output = View::factory('db_update/backend/update_output')->bind('aSQL', $sql)->renderAndMinify(); } catch (\Exception $e) { $output = __('Error') . ': ' . $e->getMessage(); } // return output return $output; }
/** * Updates the DB schema using Doctrine's SchemaTool. The $safeMode flag is passed * to SchemaTool unchanged. * * @param boolean $safeMode * @param string $outputPathAndFilename A file to write SQL to, instead of executing it * @return string */ public function updateSchema($safeMode = true, $outputPathAndFilename = null) { $schemaTool = new SchemaTool($this->entityManager); $allMetaData = $this->entityManager->getMetadataFactory()->getAllMetadata(); if ($outputPathAndFilename === null) { $schemaTool->updateSchema($allMetaData, $safeMode); } else { $updateSchemaSqlStatements = $schemaTool->getUpdateSchemaSql($allMetaData, $safeMode); file_put_contents($outputPathAndFilename, implode(PHP_EOL, $updateSchemaSqlStatements)); } }
/** * Get update queries. * * @return string[] */ protected function getUpdateQueries() { $schema = new SchemaTool($this->em); $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); // Insure the name of altered tables are quoted according to the platform $this->em->getEventManager()->addEventListener(Events::onSchemaAlterTable, $this); $sqls = $schema->getUpdateSchemaSql($metadatas, true); return $sqls; }
/** * Get update queries. * * @return string[] */ protected function getUpdateQueries() { $schema = new SchemaTool($this->em); $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); $sqls = $schema->getUpdateSchemaSql($metadatas, true); return $sqls; }