Gets the list of DDL statements that are required to create the database schema for
the given list of ClassMetadata instances.
public getCreateSchemaSql ( array $classes ) : array | ||
$classes | array | |
return | array | $sql The SQL statements needed to create the schema for the classes. |
/** * Execute the console command. * * @return void */ public function fire() { if ($this->option('sql')) { $this->info('Outputting create query:'); $sql = $this->tool->getCreateSchemaSql($this->metadata->getAllMetadata()); $this->info(implode(';' . PHP_EOL, $sql)); } else { $this->info('Creating database schema...'); $this->tool->createSchema($this->metadata->getAllMetadata()); $this->info('Schema has been created!'); } }
public function updateEntity(array $entity) { $metadata = $this->getMetadata($entity); $tool = new SchemaTool($this->_em); $queries = $tool->getCreateSchemaSql($metadata); return $this->persist($queries ?: array()); }
/** * @group DBAL-204 */ public function testGetCreateSchemaSql4() { $classes = array($this->_em->getClassMetadata(__NAMESPACE__ . '\\MysqlSchemaNamespacedEntity')); $tool = new SchemaTool($this->_em); $sql = $tool->getCreateSchemaSql($classes); $this->assertEquals(0, count($sql)); }
public function fire() { if ($this->option('sql')) { $this->info('Outputting create query:' . PHP_EOL); $sql = $this->tool->getCreateSchemaSql($this->metadata->getAllMetadata()); $this->info(implode(';' . PHP_EOL, $sql) . ';'); } else { if ($this->option('commit')) { $this->info('Creating database schema...'); $this->tool->createSchema($this->metadata->getAllMetadata()); $this->info('Schema has been created!'); } else { $this->comment("Warning: this command can cause data loss. Run with --sql or --commit."); } } }
public function testGetCreateSchemaSql3() { $classes = array($this->_em->getClassMetadata('Doctrine\\Tests\\Models\\Generic\\BooleanModel')); $tool = new SchemaTool($this->_em); $sql = $tool->getCreateSchemaSql($classes); $this->assertEquals(1, count($sql)); $this->assertEquals("CREATE TABLE boolean_model (id INT AUTO_INCREMENT NOT NULL, booleanField TINYINT(1) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[0]); }
private function getBundleSqlSchema() { $sql = array(); foreach ($this->_application->getBundles() as $bundle) { $classes = $this->_getBundleSchema($bundle); $sql = array_merge($sql, $this->_schemaTool->getCreateSchemaSql($classes)); } return $sql; }
public function testGetCreateSchemaSql3() { $classes = array($this->_em->getClassMetadata('Doctrine\\Tests\\Models\\Generic\\BooleanModel')); $tool = new SchemaTool($this->_em); $sql = $tool->getCreateSchemaSql($classes); $this->assertEquals(2, count($sql)); $this->assertEquals("CREATE TABLE boolean_model (id INT NOT NULL, booleanField BOOLEAN NOT NULL, PRIMARY KEY(id))", $sql[0]); $this->assertEquals("CREATE SEQUENCE boolean_model_id_seq INCREMENT BY 10 MINVALUE 1 START 1", $sql[1]); }
public function testGetCreateSchemaSql() { $driver = new \Doctrine\Tests\Mocks\DriverMock(); $conn = new \Doctrine\Tests\Mocks\ConnectionMock(array(), $driver); $conn->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform()); $em = $this->_getTestEntityManager($conn); $classes = array($em->getClassMetadata('Doctrine\\Tests\\Models\\CMS\\CmsAddress'), $em->getClassMetadata('Doctrine\\Tests\\Models\\CMS\\CmsUser'), $em->getClassMetadata('Doctrine\\Tests\\Models\\CMS\\CmsPhonenumber')); $exporter = new SchemaTool($em); $sql = $exporter->getCreateSchemaSql($classes); $this->assertEquals(count($sql), 8); }
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { if ($input->getOption('dump-sql') === true) { $sqls = $schemaTool->getCreateSchemaSql($metadatas); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { $output->write('Creating database schema...' . PHP_EOL); $schemaTool->createSchema($metadatas); $output->write('Database schema created successfully!' . PHP_EOL); } }
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { $output->write('ATTENTION: This operation should not be executed in an production enviroment.' . PHP_EOL . PHP_EOL); if ($input->getOption('dump-sql') === true) { $sqls = $schemaTool->getCreateSchemaSql($metadatas); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { $output->write('Creating database schema...' . PHP_EOL); $schemaTool->createSchema($metadatas); $output->write('Database schema created successfully!' . PHP_EOL); } }
/** * Generate SQL for creating schema * * @return string */ public function generate() { AnnotationRegistry::registerFile(__DIR__ . '/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); $driver = new AnnotationDriver(new CachedReader(new AnnotationReader(), new ArrayCache()), array(__DIR__ . '/../library/Xi/Filelib/Backend/Adapter/DoctrineOrm/Entity')); $config = new Configuration(); $config->setMetadataDriverImpl($driver); $config->setProxyDir(ROOT_TESTS . '/data/temp'); $config->setProxyNamespace('Proxies'); $em = EntityManager::create($this->connectionOptions, $config); $st = new SchemaTool($em); $metadata = $st->getCreateSchemaSql($em->getMetadataFactory()->getAllMetadata()); return join(";\n", $metadata) . ";\n"; }
/** * Install plugin schema based on Doctrine metadata * * @param array $metadata * @param MauticFactory $factory * * @throws \Doctrine\DBAL\ConnectionException * @throws \Doctrine\ORM\ORMException * @throws \Exception */ public static function installPluginSchema(array $metadata, MauticFactory $factory) { $db = $factory->getDatabase(); $schemaTool = new SchemaTool($factory->getEntityManager()); $installQueries = $schemaTool->getCreateSchemaSql($metadata); $db->beginTransaction(); try { foreach ($installQueries as $q) { $db->query($q); } $db->commit(); } catch (\Exception $e) { $db->rollback(); throw $e; } }
/** * {@inheritdoc} */ protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { if ($input->getOption('dump-sql')) { $sqls = $schemaTool->getCreateSchemaSql($metadatas); $output->writeln(implode(';' . PHP_EOL, $sqls) . ';'); } else { $output->writeln('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL); $output->writeln('Creating database schema...'); $schemaTool->createSchema($metadatas); $output->writeln('Database schema created successfully!'); } // Insert fake data to database if ($input->getOption('fakedata')) { $this->_insertFakeData($output); } return 0; }
/** * Execute the console command. * * @param ManagerRegistry $registry */ public function fire(ManagerRegistry $registry) { $names = $this->option('em') ? [$this->option('em')] : $registry->getManagerNames(); if (!$this->option('sql')) { $this->error('ATTENTION: This operation should not be executed in a production environment.'); } foreach ($names as $name) { $em = $registry->getManager($name); $tool = new SchemaTool($em); $this->message('Creating database schema for <info>' . $name . '</info> entity manager...', 'blue'); if ($this->option('sql')) { $sql = $tool->getCreateSchemaSql($em->getMetadataFactory()->getAllMetadata()); $this->comment(' ' . implode('; ' . PHP_EOL, $sql)); } else { $tool->createSchema($em->getMetadataFactory()->getAllMetadata()); } } $this->info('Database schema created successfully!'); }
protected function _doRun(HelperSet $helperSet) { if ($this->installSqlFile === null) { throw new \BuildException("Schema SQL file is not set."); } if (!is_dir(dirname($this->installSqlFile))) { throw new \BuildException("Schema SQL directory is not a valid directory."); } if (!is_writable(dirname($this->installSqlFile))) { throw new \BuildException("Schema SQL directory is not writable."); } $emHelper = $helperSet->get('em'); $em = $emHelper->getEntityManager(); $schemaTool = new SchemaTool($em); $cmf = $em->getMetadataFactory(); $classes = $cmf->getAllMetadata(); $sql = $schemaTool->getCreateSchemaSql($classes); $code = sprintf("<?php\n\nreturn %s;\n", var_export($sql, true)); file_put_contents($this->installSqlFile, $code); $this->log("Wrote schema SQL to file {$this->installSqlFile}"); }
/** * Install the database. * * @param string $email * @param bool $verbose */ public function installDatabase($email, $verbose) { $em = $this->getEntityManager(); $tool = new SchemaTool($em); if ($verbose) { print_r("Installing database.\n"); } $classes = $this->getEntityMetaData($em); // Rebuild the schema if ($verbose) { print_r("Drop existing schema if exists.\n"); } $tool->dropSchema($classes); if ($verbose) { print_r("Rebuild the schema.\nRunning:\n"); foreach ($tool->getCreateSchemaSql($classes) as $statement) { print_r($statement . "\n"); } } $tool->createSchema($classes); $config = $this->getServiceLocator()->get('Config'); $setUp = $config['jaztec_acl']['setUp']; $roleSetUp = $setUp['roles']; /* @var \JaztecAcl\Entity\Acl\Role[] $roles */ $roles = []; // Setup roles. foreach ($roleSetUp as $setUpConfig) { $role = new Role($setUpConfig['name'], null, $setUpConfig['sort']); $this->validateRoleParent($setUpConfig, $roles, $role); $em->persist($role); $roles[] = $role; } $em->flush(); // Add a global user. if ($verbose) { print_r("Create an administrative user with email '{$email}'.\n"); } $this->addAdminUser($email); return "Database installed\n"; }
/** * {@inheritdoc} */ protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { $extra = new Extra\MysqlExtra(); if ($input->getOption('dump-sql')) { $sqls = $schemaTool->getCreateSchemaSql($metadatas); $output->writeln(implode(';' . PHP_EOL, $sqls) . ';'); $ddls = $extra->getExtrasDDLWithDelimiter(); $output->writeln($ddls); } else { $conn = $this->getConnection(); $output->writeln('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL); $output->writeln('Creating database schema...'); $schemaTool->createSchema($metadatas); $output->writeln('Recreating database extras...'); $metadatas = $extra->getExtrasDDL(); foreach ($metadatas as $key => $ddl) { $output->writeln("Executing: {$key}"); $ret = $conn->exec($ddl); } $output->writeln('Database schema and extras created successfully!'); } return 0; }
protected function getArrayOfSqlLines(SchemaTool $schemaTool, $classes) { return $schemaTool->getCreateSchemaSql($classes); }
/** * Creates SQL requests to create 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 createEntitiesSchema(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->getCreateSchemaSql($metadata); if (true === $force) { $schemaTool->createSchema($metadata); } return $sqls; }
/** * Creates the needed DB schema using Doctrine's SchemaTool. If tables already * exist, this will throw an exception. * * @param string $outputPathAndFilename A file to write SQL to, instead of executing it * @return string */ public function createSchema($outputPathAndFilename = null) { $schemaTool = new SchemaTool($this->entityManager); $allMetaData = $this->entityManager->getMetadataFactory()->getAllMetadata(); if ($outputPathAndFilename === null) { $schemaTool->createSchema($allMetaData); } else { $createSchemaSqlStatements = $schemaTool->getCreateSchemaSql($allMetaData); file_put_contents($outputPathAndFilename, implode(PHP_EOL, $createSchemaSqlStatements)); } }
/** * Ensure type hstore is added when the table is created! */ public function testSchema() { $sql = $this->tool->getCreateSchemaSql($this->classes); $this->assertEquals("CREATE TABLE test (id SERIAL NOT NULL, title VARCHAR(255) NOT NULL, attributes hstore NOT NULL, PRIMARY KEY(id))", $sql[0]); }
/** * @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'); } } }
public static function createSchema($force = NULL) { $em = self::em(); $tool = new SchemaTool($em); $classes = $em->getMetadataFactory()->getAllMetadata(); foreach ($tool->getCreateSchemaSql($classes, TRUE) as $sql) { print $sql . ';<br />'; } if ($force == self::FORCE) { $tool->createSchema($classes, TRUE); } }