예제 #1
0
 /**
  * @dataProvider provideTableSpecificAttributes
  *
  */
 public function testCreateDefaultIndexName($tableName, $maxColumnNameLength, $indexName)
 {
     $table = $this->getTableMock($tableName, array('common_name' => $tableName, 'indices' => array(new Index(), new Index()), 'database' => $this->getDatabaseMock('bookstore', array('platform' => $this->getPlatformMock(true, array('max_column_name_length' => $maxColumnNameLength))))));
     $index = new Index();
     $index->setTable($table);
     $this->assertSame($indexName, $index->getName());
 }
예제 #2
0
 /**
  * @dataProvider provideTableSpecificAttributes
  *
  */
 public function testCreateDefaultIndexName($tableName, $maxColumnNameLength, $indexName)
 {
     $database = $this->getDatabaseMock('bookstore');
     $database->expects($this->any())->method('getMaxColumnNameLength')->will($this->returnValue($maxColumnNameLength));
     $table = $this->getTableMock($tableName, ['common_name' => $tableName, 'indices' => [new Index(), new Index()], 'database' => $database]);
     $index = new Index();
     $index->setTable($table);
     $this->assertSame($indexName, $index->getName());
 }
예제 #3
0
파일: Table.php 프로젝트: disider/Propel2
 /**
  * Adds a new index to the indices list and set the
  * parent table of the column to the current table.
  *
  * @param  Index|array $index
  * @return Index
  *
  * @throw  InvalidArgumentException
  */
 public function addIndex($index)
 {
     if ($index instanceof Index) {
         if ($this->hasIndex($index->getName())) {
             throw new InvalidArgumentException(sprintf('Index "%s" already exist.', $index->getName()));
         }
         if (!$index->getColumns()) {
             throw new InvalidArgumentException(sprintf('Index "%s" has no columns.', $index->getName()));
         }
         $index->setTable($this);
         // force the name to be created if empty.
         $this->indices[] = $index;
         return $index;
     }
     $idx = new Index();
     $idx->loadMapping($index);
     foreach ((array) @$index['columns'] as $column) {
         $idx->addColumn($column);
     }
     return $this->addIndex($idx);
 }
예제 #4
0
    public function testToString()
    {
        $tableA = new Table('A');
        $tableB = new Table('B');
        $diff = new TableDiff($tableA, $tableB);
        $diff->addAddedColumn('id', new Column('id', 'integer'));
        $diff->addRemovedColumn('category_id', new Column('category_id', 'integer'));
        $colFoo = new Column('foo', 'integer');
        $colBar = new Column('bar', 'integer');
        $tableA->addColumn($colFoo);
        $tableA->addColumn($colBar);
        $diff->addRenamedColumn($colFoo, $colBar);
        $columnDiff = new ColumnDiff($colFoo, $colBar);
        $diff->addModifiedColumn('foo', $columnDiff);
        $fk = new ForeignKey('category');
        $fk->setTable($tableA);
        $fk->setForeignTableCommonName('B');
        $fk->addReference('category_id', 'id');
        $fkChanged = clone $fk;
        $fkChanged->setForeignTableCommonName('C');
        $fkChanged->addReference('bla', 'id2');
        $fkChanged->setOnDelete('cascade');
        $fkChanged->setOnUpdate('cascade');
        $diff->addAddedFk('category', $fk);
        $diff->addModifiedFk('category', $fk, $fkChanged);
        $diff->addRemovedFk('category', $fk);
        $index = new Index('test_index');
        $index->setTable($tableA);
        $index->setColumns([$colFoo]);
        $indexChanged = clone $index;
        $indexChanged->setColumns([$colBar]);
        $diff->addAddedIndex('test_index', $index);
        $diff->addModifiedIndex('test_index', $index, $indexChanged);
        $diff->addRemovedIndex('test_index', $index);
        $string = (string) $diff;
        $expected = '  A:
    addedColumns:
      - id
    removedColumns:
      - category_id
    modifiedColumns:
      A.FOO:
        modifiedProperties:
    renamedColumns:
      foo: bar
    addedIndices:
      - test_index
    removedIndices:
      - test_index
    modifiedIndices:
      - test_index
    addedFks:
      - category
    removedFks:
      - category
    modifiedFks:
      category:
          localColumns: from ["category_id"] to ["category_id","bla"]
          foreignColumns: from ["id"] to ["id","id2"]
          onUpdate: from  to CASCADE
          onDelete: from  to CASCADE
';
        $this->assertEquals($expected, $string);
    }
예제 #5
0
파일: Table.php 프로젝트: robin850/Propel2
 /**
  * Adds a new index to the indices list and set the
  * parent table of the column to the current table.
  *
  * @param  Index $index
  * @return Index
  */
 public function addIndex($index)
 {
     if ($index instanceof Index) {
         $index->setTable($this);
         // force the name to be created if empty.
         $index->getName();
         $this->indices[] = $index;
         return $index;
     }
     $idx = new Index();
     $idx->loadMapping($index);
     return $this->addIndex($idx);
 }
예제 #6
0
 protected function addArchiveTable()
 {
     $table = $this->getTable();
     $database = $table->getDatabase();
     $archiveTableName = $this->getParameter('archive_table') ? $this->getParameter('archive_table') : $this->getTable()->getOriginCommonName() . '_archive';
     if (!$database->hasTable($archiveTableName)) {
         // create the version table
         $archiveTable = $database->addTable(['name' => $archiveTableName, 'phpName' => $this->getParameter('archive_phpname'), 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null, 'identifierQuoting' => $table->getIdentifierQuoting()]);
         $archiveTable->isArchiveTable = true;
         // copy all the columns
         foreach ($table->getColumns() as $column) {
             $columnInArchiveTable = clone $column;
             if ($columnInArchiveTable->hasReferrers()) {
                 $columnInArchiveTable->clearReferrers();
             }
             if ($columnInArchiveTable->isAutoincrement()) {
                 $columnInArchiveTable->setAutoIncrement(false);
             }
             $archiveTable->addColumn($columnInArchiveTable);
         }
         // add archived_at column
         if ($this->getParameter('log_archived_at') == 'true') {
             $archiveTable->addColumn(['name' => $this->getParameter('archived_at_column'), 'type' => 'TIMESTAMP']);
         }
         // do not copy foreign keys
         // copy the indices
         foreach ($table->getIndices() as $index) {
             $copiedIndex = clone $index;
             $archiveTable->addIndex($copiedIndex);
         }
         // copy unique indices to indices
         // see https://github.com/propelorm/Propel/issues/175 for details
         foreach ($table->getUnices() as $unique) {
             $index = new Index();
             $index->setTable($table);
             foreach ($unique->getColumns() as $columnName) {
                 if ($size = $unique->getColumnSize($columnName)) {
                     $index->addColumn(['name' => $columnName, 'size' => $size]);
                 } else {
                     $index->addColumn(['name' => $columnName]);
                 }
             }
             $archiveTable->addIndex($index);
         }
         // every behavior adding a table should re-execute database behaviors
         foreach ($database->getBehaviors() as $behavior) {
             $behavior->modifyDatabase();
         }
         $this->archiveTable = $archiveTable;
     } else {
         $this->archiveTable = $database->getTable($archiveTableName);
     }
 }
예제 #7
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);
 }
예제 #8
0
 public function testReverseDiffHasModifiedIndices()
 {
     $table = new Table();
     $table->setDatabase(new Database('foo', new DefaultPlatform()));
     $fromIndex = new Index('i1');
     $fromIndex->setTable($table);
     $toIndex = new Index('i1');
     $toIndex->setTable($table);
     $diff = $this->createTableDiff();
     $diff->addModifiedIndex('i1', $fromIndex, $toIndex);
     $reverseDiff = $diff->getReverseDiff();
     $this->assertTrue($reverseDiff->hasModifiedIndices());
     $this->assertSame(['i1' => [$toIndex, $fromIndex]], $reverseDiff->getModifiedIndices());
 }