Example #1
1
 /**
  *
  */
 public function parse(Database $database)
 {
     $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo');
     $stmt = $this->dbh->query("SHOW TABLES");
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
         $name = $row[0];
         $table = new Table($name);
         $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);
         if ($this->addVendorInfo) {
             $this->addTableVendorInfo($table);
         }
     }
 }
Example #2
1
 /**
  *
  */
 public function parse(Database $database, Task $task = null)
 {
     $stmt = $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();
     while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
         $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->addForeignKeys($table);
         $this->addIndexes($table);
         $this->addPrimaryKey($table);
     }
     return count($tables);
 }
 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 PropelSchemaValidator($appData);
     $this->assertFalse($validator->validate());
     $this->assertContains('Table "bar" declares a phpName already used in another table', $validator->getErrors());
 }
 /**
  *
  */
 public function parse(Database $database, Task $task = null)
 {
     $stmt = $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();
     while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
         $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 parse(Database $database, PDOTask $task = null)
 {
     $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo');
     $stmt = $this->dbh->query("SHOW TABLES");
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     $task->log("Reverse Engineering Tables");
     while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
         $name = $row[0];
         $task->log("  Adding table '" . $name . "'");
         $table = new Table($name);
         $database->addTable($table);
         $tables[] = $table;
     }
     // Now populate only columns.
     $task->log("Reverse Engineering Columns");
     foreach ($tables as $table) {
         $task->log("  Adding columns for table '" . $table->getName() . "'");
         $this->addColumns($table);
     }
     // Now add indices and constraints.
     $task->log("Reverse Engineering Indices And Constraints");
     foreach ($tables as $table) {
         $task->log("  Adding indices and constraints for table '" . $table->getName() . "'");
         $this->addForeignKeys($table);
         $this->addIndexes($table);
         $this->addPrimaryKey($table);
         if ($this->addVendorInfo) {
             $this->addTableVendorInfo($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.
  */
 public function parse(Database $database, PDOTask $task = null)
 {
     $tables = array();
     $stmt = $this->dbh->query("SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE = 'TABLE'");
     $task->log("Reverse Engineering Table Structures");
     // First load the tables (important that this happen before filling out details of tables)
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         if (strpos($row['OBJECT_NAME'], '$') !== false) {
             // this is an Oracle internal table or materialized view - prune
             continue;
         }
         $table = new Table($row['OBJECT_NAME']);
         $task->log("Adding table '" . $table->getName() . "'");
         $database->addTable($table);
         // Add columns, primary keys and indexes.
         $this->addColumns($table);
         $this->addPrimaryKey($table);
         $this->addIndexes($table);
         $tables[] = $table;
     }
     $task->log("Reverse Engineering Foreign Keys");
     foreach ($tables as $table) {
         $task->log("Adding foreign keys for table '" . $table->getName() . "'");
         $this->addForeignKeys($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');
 }
 /**
  * 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.
  * @return int
  */
 public function parse(Database $database, Task $task = null)
 {
     $tables = array();
     $stmt = $this->dbh->query("SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE = 'TABLE'");
     $seqPattern = $this->getGeneratorConfig()->getBuildProperty('oracleAutoincrementSequencePattern');
     if ($task) {
         $task->log("Reverse Engineering Table Structures", Project::MSG_VERBOSE);
     }
     // First load the tables (important that this happen before filling out details of tables)
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         if (strpos($row['OBJECT_NAME'], '$') !== false) {
             // 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());
         if ($task) {
             $task->log("Adding table '" . $table->getName() . "'", Project::MSG_VERBOSE);
         }
         $database->addTable($table);
         // Add columns, primary keys and indexes.
         $this->addColumns($table);
         $this->addPrimaryKey($table);
         $this->addIndexes($table);
         $pkColumns = $table->getPrimaryKey();
         if (count($pkColumns) == 1 && $seqPattern) {
             $seqName = str_replace('${table}', $tableName, $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;
     }
     if ($task) {
         $task->log("Reverse Engineering Foreign Keys", Project::MSG_VERBOSE);
     }
     foreach ($tables as $table) {
         if ($task) {
             $task->log("Adding foreign keys for table '" . $table->getName() . "'", Project::MSG_VERBOSE);
         }
         $this->addForeignKeys($table);
     }
     return count($tables);
 }
 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);
     $appData = new AppData();
     $appData->addDatabase($database);
     $validator = new PropelSchemaValidator($appData);
     $this->assertTrue($validator->validate());
 }
Example #10
0
    /**
     *
     */
    public function parse(Database $database, Task $task = null)
    {
        //		$this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo');
        $stmt = $this->dbh->query('
SELECT NAME
FROM MSysObjects
WHERE Type In (1,4,6) AND Left([Name],4)<>"MSYS"');
        // 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];
            if ($name == $this->getMigrationTable()) {
                continue;
            }
            if ($task) {
                $task->log("  Adding table '" . $name . "'", Project::MSG_VERBOSE);
            }
            $table = new Table($name);
            $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 #11
0
 /**
  *
  */
 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);
 }
 /**
  *
  */
 public function parse(Database $database, PDOTask $task = null)
 {
     $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\t\t\t\t\t\t\t\t    case when n.nspname='public' then c.relname else n.nspname||'.'||c.relname end as relname\n\t\t\t\t\t\t\t\t    FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)\n\t\t\t\t\t\t\t\t    WHERE c.relkind = 'r'\n\t\t\t\t\t\t\t\t      AND n.nspname NOT IN ('information_schema','pg_catalog')\n\t\t\t\t\t\t\t\t      AND n.nspname NOT LIKE 'pg_temp%'\n\t\t\t\t\t\t\t\t      AND n.nspname NOT LIKE 'pg_toast%'\n\t\t\t\t\t\t\t\t    ORDER BY relname");
     $tableWraps = array();
     // First load the tables (important that this happen before filling out details of tables)
     $task->log("Reverse Engineering Tables");
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $name = $row['relname'];
         $task->log("  Adding table '" . $name . "'");
         $oid = $row['oid'];
         $table = new Table($name);
         $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.
     $task->log("Reverse Engineering Columns");
     foreach ($tableWraps as $wrap) {
         $task->log("  Adding columns for table '" . $wrap->table->getName() . "'");
         $this->addColumns($wrap->table, $wrap->oid, $version);
     }
     // Now add indexes and constraints.
     $task->log("Reverse Engineering Indices And Constraints");
     foreach ($tableWraps as $wrap) {
         $task->log("  Adding indices and constraints for table '" . $wrap->table->getName() . "'");
         $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);
 }
Example #13
0
 /**
  * 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.
  */
 public function parse(Database $database)
 {
     $tables = array();
     $stmt = $this->dbh->query("SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE = 'TABLE'");
     /* @var stmt PDOStatement */
     // First load the tables (important that this happen before filling out details of tables)
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $table = new Table($row['OBJECT_NAME']);
         $database->addTable($table);
         // Add columns, primary keys and indexes.
         $this->addColumns($table);
         $this->addPrimaryKey($table);
         $this->addIndexes($table);
         $tables[] = $table;
     }
     foreach ($tables as $table) {
         $this->addForeignKeys($table);
     }
 }
Example #14
0
 /**
  *
  */
 public function parse(Database $database)
 {
     $stmt = $this->dbh->query("SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE = 'TABLE'");
     // First load the tables (important that this happen before filling out details of tables)
     $tables = array();
     while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
         $name = $row[0];
         $table = new Table($name);
         $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);
     }
 }
 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);
     $pdo = $this->getPdoConnection($datasource);
     $res = PropelSQLParser::executeString($statements, $pdo);
     if (!$res) {
         throw new Exception(sprintf('Unable to create migration table in datasource "%s"', $datasource));
     }
 }
 public function testCompareSeveralRenamedSameTables()
 {
     $d1 = new Database();
     $t1 = new Table('table1');
     $c1 = new Column('col1');
     $c1->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $t1->addColumn($c1);
     $d1->addTable($t1);
     $t2 = new Table('table2');
     $c2 = new Column('col1');
     $c2->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $t2->addColumn($c2);
     $d1->addTable($t2);
     $t3 = new Table('table3');
     $c3 = new Column('col1');
     $c3->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $t3->addColumn($c3);
     $d1->addTable($t3);
     $d2 = new Database();
     $t4 = new Table('table4');
     $c4 = new Column('col1');
     $c4->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $t4->addColumn($c4);
     $d2->addTable($t4);
     $t5 = new Table('table5');
     $c5 = new Column('col1');
     $c5->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $t5->addColumn($c5);
     $d2->addTable($t5);
     $t6 = new Table('table3');
     $c6 = new Column('col1');
     $c6->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $t6->addColumn($c6);
     $d2->addTable($t6);
     $dc = new PropelDatabaseComparator();
     $dc->setFromDatabase($d1);
     $dc->setToDatabase($d2);
     $nbDiffs = $dc->compareTables();
     $databaseDiff = $dc->getDatabaseDiff();
     $this->assertEquals(2, $nbDiffs);
     $this->assertEquals(2, count($databaseDiff->getRenamedTables()));
     $this->assertEquals(array('table1' => 'table4', 'table2' => 'table5'), $databaseDiff->getRenamedTables());
     $this->assertEquals(array(), $databaseDiff->getAddedTables());
     $this->assertEquals(array(), $databaseDiff->getRemovedTables());
 }
 public function testAddTableWithSameNameOnDifferentSchema()
 {
     $db = new Database();
     $db->setPlatform(new SchemaPlatform());
     $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());
 }
Example #18
0
 public function testCommaInEnumValueSet()
 {
     $column = new Column();
     $table = new Table();
     $database = new Database();
     $platform = new DefaultPlatform();
     $table->addColumn($column);
     $database->addTable($table);
     $database->setPlatform($platform);
     $column->loadFromXML(array('type' => PropelTypes::ENUM, 'valueSet' => 'Foo, Bar, "Foo, Bar"'));
     $valueSet = $column->getValueSet();
     $this->assertCount(3, $valueSet);
     $this->assertEquals('Foo', $valueSet[0]);
     $this->assertEquals('Bar', $valueSet[1]);
     $this->assertEquals('Foo, Bar', $valueSet[2]);
 }
 public function testColumnIsFKAndPK()
 {
     $column = new Column();
     $column->setName('id');
     $column->setPrimaryKey(true);
     $column->setAutoIncrement(true);
     $column->setType('integer');
     $table = new Table();
     $table->setCommonName('table_one');
     $table->addColumn($column);
     $db = new Database();
     $db->setName('MultipleTables');
     $db->addTable($table);
     $column = new Column();
     $column->setName('id');
     $column->setPrimaryKey(true);
     $column->setAutoIncrement(true);
     $column->setType('integer');
     $c2 = new Column();
     $c2->setPrimaryKey(true);
     $c2->setName('foreign_id');
     $c2->setType('integer');
     $table = new Table();
     $table->setCommonName('table_two');
     $table->addColumn($column);
     $table->addColumn($c2);
     $fk = new ForeignKey();
     $fk->setName('FK_1');
     $fk->addReference('foreign_id', 'id');
     $fk->setForeignTableCommonName('table_one');
     $table->addForeignKey($fk);
     $db->addTable($table);
     $expected = implode("\n", array('digraph G {', 'nodetable_one [label="{<table>table_one|<cols>id (integer) [PK]\\l}", shape=record];', 'nodetable_two [label="{<table>table_two|<cols>id (integer) [PK]\\lforeign_id (integer) [FK] [PK]\\l}", shape=record];', 'nodetable_two:cols -> nodetable_one:table [label="foreign_id=id"];', '}', ''));
     $this->assertEquals($expected, PropelDotGenerator::create($db));
 }
Example #20
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());
 }
 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 PropelDatabaseComparator();
     $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 = PropelTableComparator::computeDiff($t1, $t3);
     $this->assertEquals(array('Foo_Table' => $tableDiff), $databaseDiff->getModifiedTables());
 }
 /**
  *
  */
 public function parse(Database $database, Task $task = null)
 {
     $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo');
     $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)
     if ($task) {
         $task->log("Reverse Engineering Tables", Project::MSG_VERBOSE);
     }
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $name = $row['relname'];
         $namespacename = $row['nspname'];
         if ($name == $this->getMigrationTable()) {
             continue;
         }
         if ($task) {
             $task->log("  Adding table '" . $name . "' in schema '" . $namespacename . "'", Project::MSG_VERBOSE);
         }
         $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;
         if ($this->addVendorInfo) {
             $vi = $this->getNewVendorInfoObject($row);
             $table->addVendorInfo($vi);
         }
     }
     // Now populate only columns.
     if ($task) {
         $task->log("Reverse Engineering Columns", Project::MSG_VERBOSE);
     }
     foreach ($tableWraps as $wrap) {
         if ($task) {
             $task->log("  Adding columns for table '" . $wrap->table->getName() . "'", Project::MSG_VERBOSE);
         }
         $this->addColumns($wrap->table, $wrap->oid, $version);
     }
     // Now add indexes and constraints.
     if ($task) {
         $task->log("Reverse Engineering Indices And Constraints", Project::MSG_VERBOSE);
     }
     foreach ($tableWraps as $wrap) {
         if ($task) {
             $task->log("  Adding indices and constraints for table '" . $wrap->table->getName() . "'", Project::MSG_VERBOSE);
         }
         $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 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 PropelTableComparator();
     $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 #24
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 #25
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");
 }
Example #26
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));
 }