Example #1
0
 public function testValidateReturnsFalseWhenTwoTablesHaveSamePhpName()
 {
     $table1 = new Table('foo');
     $table2 = new Table('bar');
     $table2->setPhpName('Foo');
     $database = new Database();
     $database->addTable($table1);
     $database->addTable($table2);
     $appData = new AppData();
     $appData->addDatabase($database);
     $validator = new SchemaValidator($appData);
     $this->assertFalse($validator->validate());
     $this->assertContains('Table "bar" declares a phpName already used in another table', $validator->getErrors());
 }
 /**
  *
  */
 public function parse(Database $database)
 {
     $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo');
     $sql = 'SHOW FULL TABLES';
     if ($schema = $database->getSchema()) {
         $sql .= ' FROM ' . $database->getPlatform()->quoteIdentifier($schema);
     }
     $dataFetcher = $this->dbh->query($sql);
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     foreach ($dataFetcher as $row) {
         $name = $row[0];
         $type = $row[1];
         if ($name == $this->getMigrationTable() || $type !== 'BASE TABLE') {
             continue;
         }
         $table = new Table($name);
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         $tables[] = $table;
     }
     // Now populate only columns.
     foreach ($tables as $table) {
         $this->addColumns($table);
     }
     // Now add indices and constraints.
     foreach ($tables as $table) {
         $this->addForeignKeys($table);
         $this->addIndexes($table);
         $this->addPrimaryKey($table);
         $this->addTableVendorInfo($table);
     }
     return count($tables);
 }
 public function parse(Database $database, Task $task = null)
 {
     $dataFetcher = $this->dbh->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'");
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     foreach ($dataFetcher as $row) {
         $name = $this->cleanDelimitedIdentifiers($row[0]);
         if ($name === $this->getMigrationTable()) {
             continue;
         }
         $table = new Table($name);
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         $tables[] = $table;
     }
     // Now populate only columns.
     foreach ($tables as $table) {
         $this->addColumns($table);
     }
     // Now add indexes and constraints.
     foreach ($tables as $table) {
         $this->addForeignKeys($table);
         $this->addIndexes($table);
         $this->addPrimaryKey($table);
     }
     return count($tables);
 }
Example #4
0
 /**
  *
  */
 public function parse(Database $database)
 {
     $dataFetcher = $this->dbh->query("SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' ORDER BY name;");
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     foreach ($dataFetcher as $row) {
         $name = $row[0];
         if ($name === $this->getMigrationTable()) {
             continue;
         }
         $table = new Table($name);
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         $tables[] = $table;
     }
     // Now populate only columns.
     foreach ($tables as $table) {
         $this->addColumns($table);
     }
     // Now add indexes and constraints.
     foreach ($tables as $table) {
         $this->addIndexes($table);
     }
     return count($tables);
 }
 public function testTableNamespaceAndDbNamespace()
 {
     $d = new Database('fooDb');
     $d->setNamespace('Baz');
     $t = new Table('fooTable');
     $t->setNamespace('Foo\\Bar');
     $d->addTable($t);
     $builder = new TestableOMBuilder2($t);
     $this->assertEquals('Baz\\Foo\\Bar', $builder->getNamespace(), 'Builder namespace is composed from the database and table namespaces when both are set');
 }
 /**
  * Parses a database schema.
  *
  * @param Database $database
  * @return integer
  */
 public function parse(Database $database)
 {
     $stmt = null;
     $searchPath = '?';
     $params = [$database->getSchema()];
     if (!$database->getSchema()) {
         $stmt = $this->dbh->query('SHOW search_path');
         $searchPathString = $stmt->fetchColumn();
         $params = [];
         $searchPath = explode(',', $searchPathString);
         foreach ($searchPath as &$path) {
             $params[] = $path;
             $path = '?';
         }
         $searchPath = implode(', ', $searchPath);
     }
     $stmt = $this->dbh->prepare("SELECT c.oid,\n            c.relname, n.nspname\n            FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)\n            WHERE c.relkind = 'r'\n            AND n.nspname NOT IN ('information_schema','pg_catalog')\n            AND n.nspname NOT LIKE 'pg_temp%'\n            AND n.nspname NOT LIKE 'pg_toast%'\n            AND n.nspname IN ({$searchPath})\n            ORDER BY relname");
     $stmt->execute($params);
     $tableWraps = array();
     // First load the tables (important that this happen before filling out details of tables)
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $name = $row['relname'];
         $namespaceName = $row['nspname'];
         if ($name == $this->getMigrationTable()) {
             continue;
         }
         $oid = $row['oid'];
         $table = new Table($name);
         if ('public' !== $namespaceName) {
             $table->setSchema($namespaceName);
         }
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         // Create a wrapper to hold these tables and their associated OID
         $wrap = new \stdClass();
         $wrap->table = $table;
         $wrap->oid = $oid;
         $tableWraps[] = $wrap;
     }
     // Now populate only columns.
     foreach ($tableWraps as $wrap) {
         $this->addColumns($wrap->table, $wrap->oid);
     }
     // Now add indexes and constraints.
     foreach ($tableWraps as $wrap) {
         $this->addForeignKeys($wrap->table, $wrap->oid);
         $this->addIndexes($wrap->table, $wrap->oid);
         $this->addPrimaryKey($wrap->table, $wrap->oid);
     }
     $this->addSequences($database);
     return count($tableWraps);
 }
 /**
  *
  */
 public function parse(Database $database, Task $task = null)
 {
     $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo');
     $stmt = $this->dbh->query("SHOW FULL TABLES");
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     if ($task) {
         $task->log("Reverse Engineering Tables", Project::MSG_VERBOSE);
     }
     while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
         $name = $row[0];
         $type = $row[1];
         if ($name == $this->getMigrationTable() || $type != "BASE TABLE") {
             continue;
         }
         if ($task) {
             $task->log("  Adding table '" . $name . "'", Project::MSG_VERBOSE);
         }
         $table = new Table($name);
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         $tables[] = $table;
     }
     // Now populate only columns.
     if ($task) {
         $task->log("Reverse Engineering Columns", Project::MSG_VERBOSE);
     }
     foreach ($tables as $table) {
         if ($task) {
             $task->log("  Adding columns for table '" . $table->getName() . "'", Project::MSG_VERBOSE);
         }
         $this->addColumns($table);
     }
     // Now add indices and constraints.
     if ($task) {
         $task->log("Reverse Engineering Indices And Constraints", Project::MSG_VERBOSE);
     }
     foreach ($tables as $table) {
         if ($task) {
             $task->log("  Adding indices and constraints for table '" . $table->getName() . "'", Project::MSG_VERBOSE);
         }
         $this->addForeignKeys($table);
         $this->addIndexes($table);
         $this->addPrimaryKey($table);
         if ($this->addVendorInfo) {
             $this->addTableVendorInfo($table);
         }
     }
     return count($tables);
 }
Example #8
0
 /**
  *
  */
 public function parse(Database $database)
 {
     $stmt = $this->dbh->query('SELECT version() as ver');
     $nativeVersion = $stmt->fetchColumn();
     if (!$nativeVersion) {
         throw new EngineException('Failed to get database version');
     }
     $arrVersion = sscanf($nativeVersion, '%*s %d.%d');
     $version = sprintf('%d.%d', $arrVersion[0], $arrVersion[1]);
     // Clean up
     $stmt = null;
     $stmt = $this->dbh->query("SELECT c.oid,\n            c.relname, n.nspname\n            FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)\n            WHERE c.relkind = 'r'\n            AND n.nspname NOT IN ('information_schema','pg_catalog')\n            AND n.nspname NOT LIKE 'pg_temp%'\n            AND n.nspname NOT LIKE 'pg_toast%'\n            ORDER BY relname");
     $tableWraps = array();
     // First load the tables (important that this happen before filling out details of tables)
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $name = $row['relname'];
         $namespacename = $row['nspname'];
         if ($name == $this->getMigrationTable()) {
             continue;
         }
         $oid = $row['oid'];
         $table = new Table($name);
         if ($namespacename != 'public') {
             $table->setSchema($namespacename);
         }
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         // Create a wrapper to hold these tables and their associated OID
         $wrap = new \stdClass();
         $wrap->table = $table;
         $wrap->oid = $oid;
         $tableWraps[] = $wrap;
     }
     // Now populate only columns.
     foreach ($tableWraps as $wrap) {
         $this->addColumns($wrap->table, $wrap->oid, $version);
     }
     // Now add indexes and constraints.
     foreach ($tableWraps as $wrap) {
         $this->addForeignKeys($wrap->table, $wrap->oid, $version);
         $this->addIndexes($wrap->table, $wrap->oid, $version);
         $this->addPrimaryKey($wrap->table, $wrap->oid, $version);
     }
     // @TODO - Handle Sequences ...
     return count($tableWraps);
 }
 /**
  *
  */
 public function parse(Database $database)
 {
     $dataFetcher = $this->dbh->query("\n        SELECT name\n        FROM sqlite_master\n        WHERE type='table'\n        UNION ALL\n        SELECT name\n        FROM sqlite_temp_master\n        WHERE type='table'\n        ORDER BY name;");
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     foreach ($dataFetcher as $row) {
         $name = $row[0];
         $commonName = '';
         if ('sqlite_' == substr($name, 0, 7)) {
             continue;
         }
         if ($database->getSchema()) {
             if (false !== ($pos = strpos($name, '§'))) {
                 if ($database->getSchema()) {
                     if ($database->getSchema() !== substr($name, 0, $pos)) {
                         continue;
                     } else {
                         $commonName = substr($name, $pos + 2);
                         //2 because the delimiter § uses in UTF8 one byte more.
                     }
                 }
             } else {
                 continue;
             }
         }
         if ($name === $this->getMigrationTable()) {
             continue;
         }
         $table = new Table($commonName ?: $name);
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         $tables[] = $table;
     }
     // Now populate only columns.
     foreach ($tables as $table) {
         $this->addColumns($table);
     }
     // Now add indexes and constraints.
     foreach ($tables as $table) {
         $this->addIndexes($table);
         $this->addForeignKeys($table);
     }
     return count($tables);
 }
 /**
  * Searches for tables in the database. Maybe we want to search also the views.
  * @param Database $database The Database model class to add tables to.
  * @param Table[]  $additionalTables
  */
 public function parse(Database $database, array $additionalTables = array())
 {
     $tables = array();
     $stmt = $this->dbh->query("SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE = 'TABLE'");
     $seqPattern = $this->getGeneratorConfig()->get()['database']['adapters']['oracleAutoincrementSequencePattern'];
     // First load the tables (important that this happen before filling out details of tables)
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         if (false !== strpos($row['OBJECT_NAME'], '$')) {
             // this is an Oracle internal table or materialized view - prune
             continue;
         }
         if (strtoupper($row['OBJECT_NAME']) === strtoupper($this->getMigrationTable())) {
             continue;
         }
         $table = new Table($row['OBJECT_NAME']);
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         // Add columns, primary keys and indexes.
         $this->addColumns($table);
         $this->addPrimaryKey($table);
         $this->addIndexes($table);
         $pkColumns = $table->getPrimaryKey();
         if (1 === count($pkColumns) && $seqPattern) {
             $seqName = str_replace('${table}', $table->getName(), $seqPattern);
             $seqName = strtoupper($seqName);
             $stmt2 = $this->dbh->query("SELECT * FROM USER_SEQUENCES WHERE SEQUENCE_NAME = '" . $seqName . "'");
             $hasSeq = $stmt2->fetch(\PDO::FETCH_ASSOC);
             if ($hasSeq) {
                 $pkColumns[0]->setAutoIncrement(true);
                 $idMethodParameter = new IdMethodParameter();
                 $idMethodParameter->setValue($seqName);
                 $table->addIdMethodParameter($idMethodParameter);
             }
         }
         $tables[] = $table;
     }
     foreach ($tables as $table) {
         $this->addForeignKeys($table);
     }
     return count($tables);
 }
 protected function parseTables(Database $database, Table $filterTable = null)
 {
     $sql = "\n        SELECT name\n        FROM sqlite_master\n        WHERE type='table'\n        %filter%\n        UNION ALL\n        SELECT name\n        FROM sqlite_temp_master\n        WHERE type='table'\n        %filter%\n        ORDER BY name;";
     $filter = '';
     if ($filterTable) {
         if ($schema = $filterTable->getSchema()) {
             $filter = sprintf(" AND name LIKE '%s§%%'", $schema);
         }
         $filter .= sprintf(" AND (name = '%s' OR name LIKE '%%§%1\$s')", $filterTable->getCommonName());
     } else {
         if ($schema = $database->getSchema()) {
             $filter = sprintf(" AND name LIKE '%s§%%'", $schema);
         }
     }
     $sql = str_replace('%filter%', $filter, $sql);
     $dataFetcher = $this->dbh->query($sql);
     // First load the tables (important that this happen before filling out details of tables)
     foreach ($dataFetcher as $row) {
         $tableName = $row[0];
         $tableSchema = '';
         if ('sqlite_' == substr($tableName, 0, 7)) {
             continue;
         }
         if (false !== ($pos = strpos($tableName, '§'))) {
             $tableSchema = substr($tableName, 0, $pos);
             $tableName = substr($tableName, $pos + 2);
         }
         $table = new Table($tableName);
         if ($filterTable && $filterTable->getSchema()) {
             $table->setSchema($filterTable->getSchema());
         } else {
             if (!$database->getSchema() && $tableSchema) {
                 //we have no schema to filter, but this belongs to one, so next
                 continue;
             }
         }
         if ($tableName === $this->getMigrationTable()) {
             continue;
         }
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
     }
 }
 public function testTablePrefix()
 {
     $database = new Database();
     $database->loadMapping(array('name' => 'bookstore', 'defaultIdMethod' => 'native', 'defaultPhpNamingMethod' => 'underscore', 'tablePrefix' => 'acme_', 'defaultStringFormat' => 'XML'));
     $table = new Table();
     $database->addTable($table);
     $table->loadMapping(array('name' => 'books'));
     $this->assertEquals('Books', $table->getPhpName());
     $this->assertEquals('acme_books', $table->getCommonName());
 }
 public function testExcludedTablesWithRenaming()
 {
     $dc = new DatabaseComparator();
     $this->assertCount(0, $dc->getExcludedTables());
     $dc->setExcludedTables(array('foo'));
     $this->assertCount(1, $dc->getExcludedTables());
     $d1 = new Database();
     $d2 = new Database();
     $t2 = new Table('Bar');
     $d2->addTable($t2);
     $diff = DatabaseComparator::computeDiff($d1, $d2, false, true, true, array('Bar'));
     $this->assertFalse($diff);
     $diff = DatabaseComparator::computeDiff($d1, $d2, false, true, true, array('Baz'));
     $this->assertInstanceOf('Propel\\Generator\\Model\\Diff\\DatabaseDiff', $diff);
     $d1 = new Database();
     $t1 = new Table('Foo');
     $d1->addTable($t1);
     $d2 = new Database();
     $t2 = new Table('Bar');
     $d2->addTable($t2);
     $diff = DatabaseComparator::computeDiff($d1, $d2, false, true, true, array('Bar', 'Foo'));
     $this->assertFalse($diff);
     $diff = DatabaseComparator::computeDiff($d1, $d2, false, true, true, array('Foo'));
     $this->assertInstanceOf('Propel\\Generator\\Model\\Diff\\DatabaseDiff', $diff);
     $diff = DatabaseComparator::computeDiff($d1, $d2, false, true, true, array('Bar'));
     $this->assertInstanceOf('Propel\\Generator\\Model\\Diff\\DatabaseDiff', $diff);
     $d1 = new Database();
     $t1 = new Table('Foo');
     $c1 = new Column('col1');
     $t1->addColumn($c1);
     $d1->addTable($t1);
     $d2 = new Database();
     $t2 = new Table('Foo');
     $d2->addTable($t2);
     $diff = DatabaseComparator::computeDiff($d1, $d2, false, true, true, array('Bar', 'Foo'));
     $this->assertFalse($diff);
     $diff = DatabaseComparator::computeDiff($d1, $d2, false, true, true, array('Bar'));
     $this->assertInstanceOf('Propel\\Generator\\Model\\Diff\\DatabaseDiff', $diff);
 }
 public function testCompareSeveralTableDifferences()
 {
     $d1 = new Database();
     $t1 = new Table('Foo_Table');
     $c1 = new Column('Foo');
     $c1->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $c1->getDomain()->replaceScale(2);
     $c1->getDomain()->replaceSize(3);
     $c1->setNotNull(true);
     $c1->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
     $t1->addColumn($c1);
     $d1->addTable($t1);
     $t2 = new Table('Bar');
     $c2 = new Column('Bar_Column');
     $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $t2->addColumn($c2);
     $d1->addTable($t2);
     $t11 = new Table('Baz');
     $d1->addTable($t11);
     $d2 = new Database();
     $t3 = new Table('Foo_Table');
     $c3 = new Column('Foo1');
     $c3->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $c3->getDomain()->replaceScale(2);
     $c3->getDomain()->replaceSize(3);
     $c3->setNotNull(true);
     $c3->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
     $t3->addColumn($c3);
     $d2->addTable($t3);
     $t4 = new Table('Bar2');
     $c4 = new Column('Bar_Column');
     $c4->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $t4->addColumn($c4);
     $d2->addTable($t4);
     $t5 = new Table('Biz');
     $c5 = new Column('Biz_Column');
     $c5->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $t5->addColumn($c5);
     $d2->addTable($t5);
     // Foo_Table was modified, Bar was renamed, Baz was removed, Biz was added
     $dc = new DatabaseComparator();
     $dc->setFromDatabase($d1);
     $dc->setToDatabase($d2);
     $nbDiffs = $dc->compareTables();
     $databaseDiff = $dc->getDatabaseDiff();
     $this->assertEquals(4, $nbDiffs);
     $this->assertEquals(array('Bar' => 'Bar2'), $databaseDiff->getRenamedTables());
     $this->assertEquals(array('Biz' => $t5), $databaseDiff->getAddedTables());
     $this->assertEquals(array('Baz' => $t11), $databaseDiff->getRemovedTables());
     $tableDiff = TableComparator::computeDiff($t1, $t3);
     $this->assertEquals(array('Foo_Table' => $tableDiff), $databaseDiff->getModifiedTables());
 }
 protected function parseTables(&$tableWraps, Database $database, Table $filterTable = null)
 {
     $stmt = null;
     $params = [];
     $sql = "\n          SELECT c.oid, c.relname, n.nspname\n          FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)\n          WHERE c.relkind = 'r'\n            AND n.nspname NOT IN ('information_schema','pg_catalog')\n            AND n.nspname NOT LIKE 'pg_temp%'\n            AND n.nspname NOT LIKE 'pg_toast%'";
     if ($filterTable) {
         if ($schema = $filterTable->getSchema()) {
             $sql .= ' AND n.nspname = ?';
             $params[] = $schema;
         }
         $sql .= ' AND c.relname = ?';
         $params[] = $filterTable->getCommonName();
     } else {
         if (!$database->getSchema()) {
             $stmt = $this->dbh->query('SELECT current_schemas(false)');
             $searchPathString = substr($stmt->fetchColumn(), 1, -1);
             $params = [];
             $searchPath = explode(',', $searchPathString);
             foreach ($searchPath as &$path) {
                 $params[] = $path;
                 $path = '?';
             }
             $searchPath = implode(', ', $searchPath);
             $sql .= "\n            AND n.nspname IN ({$searchPath})";
         } elseif ($database->getSchema()) {
             $sql .= "\n            AND n.nspname = ?";
             $params[] = $database->getSchema();
         }
     }
     $sql .= "\n          ORDER BY relname";
     $stmt = $this->dbh->prepare($sql);
     $stmt->execute($params);
     // First load the tables (important that this happen before filling out details of tables)
     while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
         $name = $row['relname'];
         $namespaceName = $row['nspname'];
         if ($name == $this->getMigrationTable()) {
             continue;
         }
         $oid = $row['oid'];
         $table = new Table($name);
         if ('public' !== $namespaceName) {
             $table->setSchema($namespaceName);
         }
         $table->setIdMethod($database->getDefaultIdMethod());
         $database->addTable($table);
         // Create a wrapper to hold these tables and their associated OID
         $wrap = new \stdClass();
         $wrap->table = $table;
         $wrap->oid = $oid;
         $tableWraps[] = $wrap;
     }
 }
 protected function parseTables(Database $database, $filterTable = null)
 {
     $sql = 'SHOW FULL TABLES';
     if ($filterTable) {
         if ($schema = $filterTable->getSchema()) {
             $sql .= ' FROM ' . $database->getPlatform()->doQuoting($schema);
         }
         $sql .= sprintf(" LIKE '%s'", $filterTable->getCommonName());
     } else {
         if ($schema = $database->getSchema()) {
             $sql .= ' FROM ' . $database->getPlatform()->doQuoting($schema);
         }
     }
     $dataFetcher = $this->dbh->query($sql);
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     foreach ($dataFetcher as $row) {
         $name = $row[0];
         $type = $row[1];
         if ($name == $this->getMigrationTable() || $type !== 'BASE TABLE') {
             continue;
         }
         $table = new Table($name);
         $table->setIdMethod($database->getDefaultIdMethod());
         if ($filterTable && $filterTable->getSchema()) {
             $table->setSchema($filterTable->getSchema());
         }
         $database->addTable($table);
         $tables[] = $table;
     }
 }
 public function testCompareModifiedFks()
 {
     $db1 = new Database();
     $db1->setPlatform($this->platform);
     $c1 = new Column('Foo');
     $c2 = new Column('Bar');
     $fk1 = new ForeignKey();
     $fk1->addReference($c1, $c2);
     $t1 = new Table('Baz');
     $t1->addForeignKey($fk1);
     $db1->addTable($t1);
     $t1->doNaming();
     $db2 = new Database();
     $db2->setPlatform($this->platform);
     $c3 = new Column('Foo');
     $c4 = new Column('Bar2');
     $fk2 = new ForeignKey();
     $fk2->addReference($c3, $c4);
     $t2 = new Table('Baz');
     $t2->addForeignKey($fk2);
     $db2->addTable($t2);
     $t2->doNaming();
     $tc = new TableComparator();
     $tc->setFromTable($t1);
     $tc->setToTable($t2);
     $nbDiffs = $tc->compareForeignKeys();
     $tableDiff = $tc->getTableDiff();
     $this->assertEquals(1, $nbDiffs);
     $this->assertEquals(1, count($tableDiff->getModifiedFks()));
     $this->assertEquals(array('Baz_FK_1' => array($fk1, $fk2)), $tableDiff->getModifiedFks());
 }
Example #18
0
 public function testAddTableWithSameNameOnDifferentSchema()
 {
     $db = new Database();
     $db->setPlatform(new PgsqlPlatform());
     $t1 = new Table('t1');
     $db->addTable($t1);
     $this->assertEquals('t1', $t1->getName());
     $t1b = new Table('t1');
     $t1b->setSchema('bis');
     $db->addTable($t1b);
     $this->assertEquals('bis.t1', $t1b->getName());
 }
 public function createMigrationTable($datasource)
 {
     $platform = $this->getPlatform($datasource);
     // modelize the table
     $database = new Database($datasource);
     $database->setPlatform($platform);
     $table = new Table($this->getMigrationTable());
     $database->addTable($table);
     $column = new Column('version');
     $column->getDomain()->copy($platform->getDomainForType('INTEGER'));
     $column->setDefaultValue(0);
     $table->addColumn($column);
     // insert the table into the database
     $statements = $platform->getAddTableDDL($table);
     $conn = $this->getAdapterConnection($datasource);
     $res = SqlParser::executeString($statements, $conn);
     if (!$res) {
         throw new \Exception(sprintf('Unable to create migration table in datasource "%s"', $datasource));
     }
 }
Example #20
0
 public function testValidateReturnsTrueWhenTwoTablesHaveSamePhpNameInDifferentNamespaces()
 {
     $column1 = new Column('id');
     $column1->setPrimaryKey(true);
     $table1 = new Table('foo');
     $table1->addColumn($column1);
     $table1->setNamespace('Foo');
     $column2 = new Column('id');
     $column2->setPrimaryKey(true);
     $table2 = new Table('bar');
     $table2->addColumn($column2);
     $table2->setPhpName('Foo');
     $table2->setNamespace('Bar');
     $database = new Database();
     $database->addTable($table1);
     $database->addTable($table2);
     $schema = new Schema();
     $schema->addDatabase($database);
     $validator = new SchemaValidator($schema);
     $this->assertTrue($validator->validate());
 }
 public function providerForTestGetForeignKeysDDL()
 {
     $db = new Database();
     $db->setIdentifierQuoting(true);
     $table1 = new Table('foo');
     $db->addTable($table1);
     $column1 = new Column('bar_id');
     $column1->getDomain()->copy(new Domain('FOOTYPE'));
     $table1->addColumn($column1);
     $table2 = new Table('bar');
     $db->addTable($table2);
     $column2 = new Column('id');
     $column2->getDomain()->copy(new Domain('BARTYPE'));
     $table2->addColumn($column2);
     $fk = new ForeignKey('foo_bar_fk');
     $fk->setForeignTableCommonName('bar');
     $fk->addReference($column1, $column2);
     $fk->setOnDelete('CASCADE');
     $table1->addForeignKey($fk);
     $column3 = new Column('baz_id');
     $column3->getDomain()->copy(new Domain('BAZTYPE'));
     $table1->addColumn($column3);
     $table3 = new Table('baz');
     $db->addTable($table3);
     $column4 = new Column('id');
     $column4->getDomain()->copy(new Domain('BAZTYPE'));
     $table3->addColumn($column4);
     $fk = new ForeignKey('foo_baz_fk');
     $fk->setForeignTableCommonName('baz');
     $fk->addReference($column3, $column4);
     $fk->setOnDelete('SETNULL');
     $table1->addForeignKey($fk);
     return array(array($table1));
 }
Example #22
0
 public function testHasPlatform()
 {
     $column = new Column();
     $this->assertFalse($column->hasPlatform());
     $table = new Table();
     $table->addColumn($column);
     $this->assertFalse($column->hasPlatform());
     $database = new Database();
     $database->addTable($table);
     $this->assertFalse($column->hasPlatform());
     $platform = new DefaultPlatform();
     $database->setPlatform($platform);
     $this->assertTrue($column->hasPlatform());
 }
Example #23
0
 public function startElement($parser, $name, $attributes)
 {
     $parentTag = $this->peekCurrentSchemaTag();
     if (false === $parentTag) {
         switch ($name) {
             case 'database':
                 if ($this->isExternalSchema()) {
                     $this->currentPackage = isset($attributes['package']) ? $attributes['package'] : null;
                     if (null === $this->currentPackage) {
                         $this->currentPackage = $this->defaultPackage;
                     }
                 } else {
                     $this->currDB = $this->schema->addDatabase($attributes);
                 }
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('database' === $parentTag) {
         switch ($name) {
             case 'external-schema':
                 $xmlFile = isset($attributes['filename']) ? $attributes['filename'] : null;
                 // 'referenceOnly' attribute is valid in the main schema XML file only,
                 // and it's ignored in the nested external-schemas
                 if (!$this->isExternalSchema()) {
                     $isForRefOnly = isset($attributes['referenceOnly']) ? $attributes['referenceOnly'] : null;
                     $this->isForReferenceOnly = null !== $isForRefOnly ? 'true' === strtolower($isForRefOnly) : true;
                     // defaults to TRUE
                 }
                 if ('/' !== $xmlFile[0]) {
                     $xmlFile = realpath(dirname($this->currentXmlFile) . DIRECTORY_SEPARATOR . $xmlFile);
                     if (!file_exists($xmlFile)) {
                         throw new SchemaException(sprintf('Unknown include external "%s"', $xmlFile));
                     }
                 }
                 $this->parseFile($xmlFile);
                 break;
             case 'domain':
                 $this->currDB->addDomain($attributes);
                 break;
             case 'table':
                 if (!isset($attributes['schema']) && $this->currDB->getSchema() && $this->currDB->getPlatform()->supportsSchemas() && false === strpos($attributes['name'], $this->currDB->getPlatform()->getSchemaDelimiter())) {
                     $attributes['schema'] = $this->currDB->getSchema();
                 }
                 $this->currTable = $this->currDB->addTable($attributes);
                 if ($this->isExternalSchema()) {
                     $this->currTable->setForReferenceOnly($this->isForReferenceOnly);
                     $this->currTable->setPackage($this->currentPackage);
                 }
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currDB->addVendorInfo($attributes);
                 break;
             case 'behavior':
                 $this->currBehavior = $this->currDB->addBehavior($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('table' === $parentTag) {
         switch ($name) {
             case 'column':
                 $this->currColumn = $this->currTable->addColumn($attributes);
                 break;
             case 'foreign-key':
                 $this->currFK = $this->currTable->addForeignKey($attributes);
                 break;
             case 'index':
                 $this->currIndex = new Index();
                 $this->currIndex->setTable($this->currTable);
                 $this->currIndex->loadMapping($attributes);
                 break;
             case 'unique':
                 $this->currUnique = new Unique();
                 $this->currUnique->setTable($this->currTable);
                 $this->currUnique->loadMapping($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currTable->addVendorInfo($attributes);
                 break;
             case 'id-method-parameter':
                 $this->currTable->addIdMethodParameter($attributes);
                 break;
             case 'behavior':
                 $this->currBehavior = $this->currTable->addBehavior($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('column' === $parentTag) {
         switch ($name) {
             case 'inheritance':
                 $this->currColumn->addInheritance($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currColumn->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('foreign-key' === $parentTag) {
         switch ($name) {
             case 'reference':
                 $this->currFK->addReference($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currUnique->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('index' === $parentTag) {
         switch ($name) {
             case 'index-column':
                 $this->currIndex->addColumn($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currIndex->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('unique' === $parentTag) {
         switch ($name) {
             case 'unique-column':
                 $this->currUnique->addColumn($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currUnique->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ($parentTag == 'behavior') {
         switch ($name) {
             case 'parameter':
                 $this->currBehavior->addParameter($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('vendor' === $parentTag) {
         switch ($name) {
             case 'parameter':
                 $this->currVendorObject->setParameter($attributes['name'], $attributes['value']);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } else {
         // it must be an invalid tag
         $this->_throwInvalidTagException($parser, $name);
     }
     $this->pushCurrentSchemaTag($name);
 }
Example #24
0
 public function testGetColumnDDLAutoIncrement()
 {
     $database = new Database();
     $database->setPlatform($this->getPlatform());
     $table = new Table('foo_table');
     $table->setIdMethod(IdMethod::NATIVE);
     $database->addTable($table);
     $column = new Column('foo');
     $column->getDomain()->copy($this->getPlatform()->getDomainForType(PropelTypes::BIGINT));
     $column->setAutoIncrement(true);
     $table->addColumn($column);
     $expected = 'foo bigserial';
     $this->assertEquals($expected, $this->getPlatform()->getColumnDDL($column));
 }
Example #25
0
 public function testAddTableSkipsDatabaseNamespaceWhenTableNamespaceIsAbsolute()
 {
     $db = new Database();
     $db->setNamespace('Foo');
     $t1 = new Table('t1');
     $t1->setNamespace('\\Bar');
     $db->addTable($t1);
     $this->assertEquals('Bar', $t1->getNamespace());
 }
Example #26
0
 public function testQualifiedName()
 {
     $table = new Table();
     $table->setSchema("foo");
     $table->setCommonName("bar");
     $this->assertEquals($table->getName(), "bar");
     $this->assertEquals($table->getCommonName(), "bar");
     $database = new Database();
     $database->addTable($table);
     $database->setPlatform(new NoSchemaPlatform());
     $this->assertEquals($table->getName(), "bar");
     $database->setPlatform(new SchemaPlatform());
     $this->assertEquals($table->getName(), "foo.bar");
 }