Exemple #1
0
 public function testResetColumnsSize()
 {
     $columns[] = $this->getColumnMock('foo', array('size' => 100));
     $columns[] = $this->getColumnMock('bar', array('size' => 5));
     $index = new Index();
     $index->setColumns($columns);
     $this->assertTrue($index->hasColumnSize('foo'));
     $this->assertTrue($index->hasColumnSize('bar'));
     $index->resetColumnsSize();
     $this->assertFalse($index->hasColumnSize('foo'));
     $this->assertFalse($index->hasColumnSize('bar'));
 }
Exemple #2
0
 /**
  * Adds extra indices for reverse foreign keys
  * This is required for MySQL databases,
  * and is called from Database::doFinalInitialization()
  */
 public function addExtraIndices()
 {
     /**
      * A collection of indexed columns. The keys is the column name
      * (concatenated with a comma in the case of multi-col index), the value is
      * an array with the names of the indexes that index these columns. We use
      * it to determine which additional indexes must be created for foreign
      * keys. It could also be used to detect duplicate indexes, but this is not
      * implemented yet.
      * @var array
      */
     $_indices = array();
     $this->collectIndexedColumns('PRIMARY', $this->getPrimaryKey(), $_indices);
     $_tableIndices = array_merge($this->getIndices(), $this->getUnices());
     foreach ($_tableIndices as $_index) {
         $this->collectIndexedColumns($_index->getName(), $_index->getColumns(), $_indices);
     }
     // we're determining which tables have foreign keys that point to this table,
     // since MySQL needs an index on any column that is referenced by another table
     // (yep, MySQL _is_ a PITA)
     $counter = 0;
     foreach ($this->getReferrers() as $foreignKey) {
         $referencedColumns = $foreignKey->getForeignColumnObjects();
         $referencedColumnsHash = $this->getColumnList($referencedColumns);
         if (!array_key_exists($referencedColumnsHash, $_indices)) {
             // no matching index defined in the schema, so we have to create one
             $index = new Index();
             $index->setName(sprintf('I_referenced_%s_%s', $foreignKey->getName(), ++$counter));
             $index->setColumns($referencedColumns);
             $index->resetColumnSize();
             $this->addIndex($index);
             // Add this new index to our collection, otherwise we might add it again (bug #725)
             $this->collectIndexedColumns($index->getName(), $referencedColumns, $_indices);
         }
     }
     // we're adding indices for this table foreign keys
     foreach ($this->getForeignKeys() as $foreignKey) {
         $localColumns = $foreignKey->getLocalColumnObjects();
         $localColumnsHash = $this->getColumnList($localColumns);
         if (!array_key_exists($localColumnsHash, $_indices)) {
             // no matching index defined in the schema, so we have to create one. MySQL needs indices on any columns that serve as foreign keys. these are not auto-created prior to 4.1.2
             $index = new Index();
             $index->setName(substr_replace($foreignKey->getName(), 'FI_', strrpos($foreignKey->getName(), 'FK_'), 3));
             $index->setColumns($localColumns);
             $index->resetColumnSize();
             $this->addIndex($index);
             $this->collectIndexedColumns($index->getName(), $localColumns, $_indices);
         }
     }
 }
 public function testIsIndex()
 {
     $table = new Table();
     $column1 = new Column('category_id');
     $column2 = new Column('type');
     $table->addColumn($column1);
     $table->addColumn($column2);
     $index = new Index('test_index');
     $index->setColumns([$column1, $column2]);
     $table->addIndex($index);
     $this->assertTrue($table->isIndex(['category_id', 'type']));
     $this->assertTrue($table->isIndex(['type', 'category_id']));
     $this->assertFalse($table->isIndex(['category_id', 'type2']));
     $this->assertFalse($table->isIndex(['asd']));
 }
Exemple #4
0
$fkCategoryPost->setOnDelete('SETNULL');
$fkPostTag = new ForeignKey('fk_post_has_tags');
$fkPostTag->addReference('post_id', 'id');
$fkPostTag->setForeignTableCommonName('blog_post');
$fkPostTag->setPhpName('Post');
$fkPostTag->setDefaultJoin('Criteria::LEFT_JOIN');
$fkPostTag->setOnDelete('CASCADE');
$fkTagPost = new ForeignKey('fk_tag_has_posts');
$fkTagPost->addReference('tag_id', 'id');
$fkTagPost->setForeignTableCommonName('blog_tag');
$fkTagPost->setPhpName('Tag');
$fkTagPost->setDefaultJoin('Criteria::LEFT_JOIN');
$fkTagPost->setOnDelete('CASCADE');
/* Regular Indexes */
$pageContentFulltextIdx = new Index('page_content_fulltext_idx');
$pageContentFulltextIdx->setColumns([['name' => 'content']]);
$pageContentFulltextIdx->addVendorInfo(new VendorInfo('mysql', ['Index_type' => 'FULLTEXT']));
/* Unique Indexes */
$authorUsernameUnique = new Unique('author_password_unique_idx');
$authorUsernameUnique->setColumns([['name' => 'username', 'size' => '8']]);
/* Behaviors */
$timestampableBehavior = new TimestampableBehavior();
$timestampableBehavior->setName('timestampable');
$sluggableBehavior = new SluggableBehavior();
$sluggableBehavior->setName('sluggable');
/* Tables */
$table1 = new Table('blog_post');
$table1->setDescription('The list of posts');
$table1->setNamespace('Blog');
$table1->setPackage('Acme.Blog');
$table1->addColumns([$column11, $column12, $column13, $column14, $column15, $column16, $column17]);
Exemple #5
0
 /**
  * Creates a new index.
  *
  * @param  string $name    The index name
  * @param  array  $columns The list of columns to index
  * @return Index  $index   The created index
  */
 protected function createIndex($name, array $columns)
 {
     $index = new Index($name);
     $index->setColumns($columns);
     $index->resetColumnsSize();
     $this->addIndex($index);
     return $index;
 }
Exemple #6
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);
    }
 public function testSetModifiedIndices()
 {
     $table = new Table('users');
     $table->setDatabase(new Database('foo', new DefaultPlatform()));
     $fromIndex = new Index('username_unique_idx');
     $fromIndex->setTable($table);
     $fromIndex->setColumns([new Column('username')]);
     $toIndex = new Index('username_unique_idx');
     $toIndex->setTable($table);
     $toIndex->setColumns([new Column('client_id'), new Column('username')]);
     $diff = $this->createTableDiff();
     $diff->setModifiedIndices([[$fromIndex, $toIndex]]);
     $this->assertCount(1, $diff->getModifiedIndices());
     $this->assertTrue($diff->hasModifiedIndices());
 }