public function readDatabase() { $this->database = new Database(); $this->database->setSchema('migration'); $this->database->setPlatform($this->platform); $this->parser->parse($this->database); }
public function testTableInheritsSchema() { $database = new Database(); $database->setPlatform(new SchemaPlatform()); $database->setSchema("Foo"); $table = new Table("Bar"); $database->addTable($table); $this->assertTrue($database->hasTable("Foo.Bar")); $this->assertFalse($database->hasTable("Bar")); $database = new Database(); $database->setPlatform(new NoSchemaPlatform()); $database->addTable($table); $this->assertFalse($database->hasTable("Foo.Bar")); $this->assertTrue($database->hasTable("Bar")); }
/** * @dataProvider parseDataProvider */ public function testParse($columnDDL, $expectedColumnPhpName, $expectedColumnDefaultType, $expectedColumnDefaultValue, $expectedSize, $expectedScale) { $this->con->query("create table foo ( {$columnDDL} );"); $parser = new PgsqlSchemaParser($this->con); $parser->setGeneratorConfig(new QuickGeneratorConfig()); $database = new Database(); $database->setSchema('public'); $database->setPlatform(new DefaultPlatform()); // make sure our DDL insert produced exactly the SQL we inserted $this->assertGreaterThanOrEqual(1, $parser->parse($database), 'We parsed at least one table.'); $table = $database->getTable('foo'); $columns = $table->getColumns(); $this->assertEquals(1, count($columns)); // check out our rev-eng column info $defaultValue = $columns[0]->getDefaultValue(); $this->assertEquals($expectedColumnPhpName, $columns[0]->getPhpName()); $this->assertEquals($expectedColumnDefaultType, $defaultValue->getType()); $this->assertEquals($expectedColumnDefaultValue, $defaultValue->getValue()); $this->assertEquals($expectedSize, $columns[0]->getSize()); $this->assertEquals($expectedScale, $columns[0]->getScale()); }
/** * @return Database */ public function readConnectedDatabase() { $this->getDatabase(); $database = new Database(); $database->setSchema($this->database->getSchema()); $database->setName($this->database->getName()); $database->setPlatform($this->getPlatform()); $this->getParser()->parse($database); return $database; }
$table3->addColumns([$column31, $column32]); $table4 = new Table('blog_tag'); $table4->setDescription('The list of tags'); $table4->setNamespace('Blog'); $table4->setPackage('Acme.Blog'); $table4->addColumns([$column41, $column42]); $table5 = new Table('blog_post_tag'); $table5->setNamespace('Blog'); $table5->setPackage('Acme.Blog'); $table5->setCrossRef(); $table5->addColumns([$column51, $column52]); $table5->addForeignKeys([$fkPostTag, $fkTagPost]); $table6 = new Table('cms_page'); $table6->setPhpName('Page'); $table6->setNamespace('Cms'); $table6->setBaseClass('Acme\\Model\\PublicationActiveRecord'); $table6->setPackage('Acme.Cms'); $table6->addColumns([$column61, $column62, $column63, $column64]); $table6->addIndex($pageContentFulltextIdx); $table6->addVendorInfo(new VendorInfo('mysql', ['Engine' => 'MyISAM'])); /* Database */ $database = new Database('acme_blog', new MysqlPlatform()); $database->setSchema('acme'); $database->setTablePrefix('acme_'); $database->setNamespace('Acme\\Model'); $database->setBaseClass('Acme\\Model\\ActiveRecord'); $database->setPackage('Acme'); $database->setHeavyIndexing(); $database->addVendorInfo(new VendorInfo('mysql', ['Engine' => 'InnoDB', 'Charset' => 'utf8'])); $database->addTables([$table1, $table2, $table3, $table4, $table5, $table6]); return $database;
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $configOptions = []; if ($this->hasInputOption('connection', $input)) { foreach ($input->getOption('connection') as $conn) { $configOptions += $this->connectionToProperties($conn); } } if ($this->hasInputOption('migration-table', $input)) { $configOptions['propel']['migrations']['tableName'] = $input->getOption('migration-table'); } if ($this->hasInputOption('schema-dir', $input)) { $configOptions['propel']['paths']['schemaDir'] = $input->getOption('schema-dir'); } if ($this->hasInputOption('output-dir', $input)) { $configOptions['propel']['paths']['migrationDir'] = $input->getOption('output-dir'); } $generatorConfig = $this->getGeneratorConfig($configOptions, $input); $this->createDirectory($generatorConfig->getSection('paths')['migrationDir']); $manager = new MigrationManager(); $manager->setGeneratorConfig($generatorConfig); $manager->setSchemas($this->getSchemas($generatorConfig->getSection('paths')['schemaDir'], $input->getOption('recursive'))); $connections = []; $optionConnections = $input->getOption('connection'); if (!$optionConnections) { $connections = $generatorConfig->getBuildConnections(); } else { foreach ($optionConnections as $connection) { list($name, $dsn, $infos) = $this->parseConnection($connection); $connections[$name] = array_merge(['dsn' => $dsn], $infos); } } $manager->setConnections($connections); $manager->setMigrationTable($generatorConfig->getConfigProperty('migrations.tableName')); $manager->setWorkingDirectory($generatorConfig->getSection('paths')['migrationDir']); if ($manager->hasPendingMigrations()) { throw new RuntimeException('Uncommitted migrations have been found ; you should either execute or delete them before rerunning the \'diff\' task'); } $totalNbTables = 0; $reversedSchema = new Schema(); foreach ($manager->getDatabases() as $appDatabase) { $name = $appDatabase->getName(); if (!($params = @$connections[$name])) { $output->writeln(sprintf('<info>No connection configured for database "%s"</info>', $name)); } if ($input->getOption('verbose')) { $output->writeln(sprintf('Connecting to database "%s" using DSN "%s"', $name, $params['dsn'])); } $conn = $manager->getAdapterConnection($name); $platform = $generatorConfig->getConfiguredPlatform($conn, $name); if (!$platform->supportsMigrations()) { $output->writeln(sprintf('Skipping database "%s" since vendor "%s" does not support migrations', $name, $platform->getDatabaseType())); continue; } $additionalTables = []; foreach ($appDatabase->getTables() as $table) { if ($table->getSchema() && $table->getSchema() != $appDatabase->getSchema()) { $additionalTables[] = $table; } } if ($input->getOption('disable-identifier-quoting')) { $platform->setIdentifierQuoting(false); } $database = new Database($name); $database->setPlatform($platform); $database->setSchema($appDatabase->getSchema()); $database->setDefaultIdMethod(IdMethod::NATIVE); $parser = $generatorConfig->getConfiguredSchemaParser($conn, $name); $nbTables = $parser->parse($database, $additionalTables); $reversedSchema->addDatabase($database); $totalNbTables += $nbTables; if ($input->getOption('verbose')) { $output->writeln(sprintf('%d tables found in database "%s"', $nbTables, $name), Output::VERBOSITY_VERBOSE); } } if ($totalNbTables) { $output->writeln(sprintf('%d tables found in all databases.', $totalNbTables)); } else { $output->writeln('No table found in all databases'); } // comparing models $output->writeln('Comparing models...'); $tableRenaming = $input->getOption('table-renaming'); $migrationsUp = []; $migrationsDown = []; $removeTable = !$input->getOption('skip-removed-table'); $excludedTables = $input->getOption('skip-tables'); foreach ($reversedSchema->getDatabases() as $database) { $name = $database->getName(); if ($input->getOption('verbose')) { $output->writeln(sprintf('Comparing database "%s"', $name)); } if (!($appDataDatabase = $manager->getDatabase($name))) { $output->writeln(sprintf('<error>Database "%s" does not exist in schema.xml. Skipped.</error>', $name)); continue; } $configManager = new ConfigurationManager(); $excludedTables = array_merge((array) $excludedTables, (array) $configManager->getSection('exclude_tables')); $databaseDiff = DatabaseComparator::computeDiff($database, $appDataDatabase, false, $tableRenaming, $removeTable, $excludedTables); if (!$databaseDiff) { if ($input->getOption('verbose')) { $output->writeln(sprintf('Same XML and database structures for datasource "%s" - no diff to generate', $name)); } continue; } $output->writeln(sprintf('Structure of database was modified in datasource "%s": %s', $name, $databaseDiff->getDescription())); foreach ($databaseDiff->getPossibleRenamedTables() as $fromTableName => $toTableName) { $output->writeln(sprintf('<info>Possible table renaming detected: "%s" to "%s". It will be deleted and recreated. Use --table-renaming to only rename it.</info>', $fromTableName, $toTableName)); } $conn = $manager->getAdapterConnection($name); $platform = $generatorConfig->getConfiguredPlatform($conn, $name); if ($input->getOption('disable-identifier-quoting')) { $platform->setIdentifierQuoting(false); } $migrationsUp[$name] = $platform->getModifyDatabaseDDL($databaseDiff); $migrationsDown[$name] = $platform->getModifyDatabaseDDL($databaseDiff->getReverseDiff()); } if (!$migrationsUp) { $output->writeln('Same XML and database structures for all datasource - no diff to generate'); return; } $timestamp = time(); $migrationFileName = $manager->getMigrationFileName($timestamp); $migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp, $input->getOption('comment')); $file = $generatorConfig->getSection('paths')['migrationDir'] . DIRECTORY_SEPARATOR . $migrationFileName; file_put_contents($file, $migrationClassBody); $output->writeln(sprintf('"%s" file successfully created.', $file)); if (null !== ($editorCmd = $input->getOption('editor'))) { $output->writeln(sprintf('Using "%s" as text editor', $editorCmd)); shell_exec($editorCmd . ' ' . escapeshellarg($file)); } else { $output->writeln('Please review the generated SQL statements, and add data migration code if necessary.'); $output->writeln('Once the migration class is valid, call the "migrate" task to execute it.'); } }
public function testAutoNamespaceToDatabaseSchemaName() { $yamlConf = <<<EOF propel: database: connections: mysource: adapter: mysql classname: Propel\\Runtime\\Connection\\DebugPDO dsn: mysql:host=localhost;dbname=mydb user: root password: generator: schema: autoNamespace: true EOF; $configFilename = sys_get_temp_dir() . '/propel.yml'; $filesystem = new Filesystem(); $filesystem->dumpFile($configFilename, $yamlConf); $schema = 'TestSchema'; $config = new GeneratorConfig($configFilename); $platform = new MysqlPlatform(); $parentSchema = new Schema($platform); $parentSchema->setGeneratorConfig($config); $db = new Database(); $db->setPlatform($platform); $db->setParentSchema($parentSchema); $db->setSchema($schema); $this->assertEquals($schema, $db->getNamespace()); }