alterSchema() public method

Generate a alter syntax from CakeSchema::compare()
public alterSchema ( mixed $compare, string $table = null ) : boolean
$compare mixed The comparison data.
$table string The table name.
return boolean
 /**
  * test alterSchema on two tables.
  *
  * @return void
  */
 public function testAlteringTwoTables()
 {
     $schema1 = new CakeSchema(array('name' => 'AlterTest1', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50)), 'other_table' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50))));
     $schema2 = new CakeSchema(array('name' => 'AlterTest1', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50)), 'other_table' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50))));
     $result = $this->Dbo->alterSchema($schema2->compare($schema1));
     $this->assertEquals(2, substr_count($result, 'field_two'), 'Too many fields');
 }
 /**
  * Alter Indexes method
  *
  * @param array $indexes List of indexes
  * @param string $type Type of operation to be done
  * @param string $table table name
  * @throws MigrationException
  * @return void
  */
 protected function _alterIndexes($indexes, $type, $table)
 {
     foreach ($indexes as $key => $index) {
         if (is_numeric($key)) {
             $key = $index;
             $index = array();
         }
         $sql = $this->db->alterSchema(array($table => array($type => array('indexes' => array($key => $index)))));
         if ($this->_invokePrecheck('beforeAction', $type . '_index', array('table' => $table, 'index' => $key))) {
             $this->_invokeCallbacks('beforeAction', $type . '_index', array('table' => $table, 'index' => $key));
             if (@$this->db->execute($sql) === false) {
                 throw new MigrationException($this, sprintf(__d('migrations', 'SQL Error: %s'), $this->db->error));
             }
         }
         $this->_invokeCallbacks('afterAction', $type . '_index', array('table' => $table, 'index' => $key));
     }
 }
 /**
  * Rename an existing index.
  *
  * @param string $table
  * @param string $oldName
  * @param string $newName
  * @throws MissingTableException if table does not exist in database
  * @throws MissingIndexException if the $oldName index does not exist on the table
  * @throws IndexAlreadyExistsException if the $newName index already exists on the table
  * @throws MigrationException if an sql error occurred
  * @return Migration
  */
 public function renameIndex($table, $oldName, $newName)
 {
     if (!in_array($this->_db->fullTableName($table, false, false), $this->_db->listSources())) {
         throw new MissingTableException(__d('migration', 'Table "%s" does not exist in database.', $this->_db->fullTableName($table, false, false)));
     }
     $existingIndexes = $this->_db->index($table);
     if (get_class($this->_db) === 'Postgres') {
         $oldName = strtolower($oldName);
         $newName = strtolower($newName);
     }
     if (!array_key_exists($oldName, $existingIndexes)) {
         throw new MissingIndexException(__d('migration', 'Index "%s" does not exist on table "%s".', array($oldName, $table)));
     }
     if (array_key_exists($newName, $existingIndexes)) {
         throw new IndexAlreadyExistsException(__d('migration', 'Index "%s" already exists on table "%s".', array($newName, $table)));
     }
     try {
         $this->_db->execute($this->_db->alterSchema(array($table => array('drop' => array('indexes' => array($oldName => array())), 'add' => array('indexes' => array($newName => $existingIndexes[$oldName]))))));
     } catch (Exception $e) {
         throw new MigrationException(__d('migration', 'SQL Error: %s', $e->getMessage()));
     }
     return $this;
 }
 /**
  * Test the alterSchema RENAME statements
  *
  * @return void
  */
 public function testAlterSchemaRenameTo()
 {
     $query = $this->Dbo->alterSchema(array('posts' => array('change' => array('title' => array('name' => 'subject', 'type' => 'string', 'null' => false)))));
     $this->assertContains('RENAME "title" TO "subject";', $query);
     $this->assertContains('ALTER COLUMN "subject" TYPE', $query);
     $this->assertNotContains(";\n\tALTER COLUMN \"subject\" TYPE", $query);
     $this->assertNotContains('ALTER COLUMN "title" TYPE "subject"', $query);
 }
 /**
  * Test the alter index capabilities of postgres
  *
  * @return void
  */
 public function testAlterIndexes()
 {
     $this->Dbo->cacheSources = false;
     $schema1 = new CakeSchema(array('name' => 'AlterTest1', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true))));
     $this->Dbo->rawQuery($this->Dbo->createSchema($schema1));
     $schema2 = new CakeSchema(array('name' => 'AlterTest2', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true), 'indexes' => array('name_idx' => array('unique' => false, 'column' => 'name'), 'group_idx' => array('unique' => false, 'column' => 'group1'), 'compound_idx' => array('unique' => false, 'column' => array('group1', 'group2')), 'PRIMARY' => array('unique' => true, 'column' => 'id')))));
     $this->Dbo->query($this->Dbo->alterSchema($schema2->compare($schema1)));
     $indexes = $this->Dbo->index('altertest');
     $this->assertEquals($schema2->tables['altertest']['indexes'], $indexes);
     // Change three indexes, delete one and add another one
     $schema3 = new CakeSchema(array('name' => 'AlterTest3', 'connection' => 'test', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50), 'group1' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true), 'indexes' => array('name_idx' => array('unique' => true, 'column' => 'name'), 'group_idx' => array('unique' => false, 'column' => 'group2'), 'compound_idx' => array('unique' => false, 'column' => array('group2', 'group1')), 'another_idx' => array('unique' => false, 'column' => array('group1', 'name'))))));
     $this->Dbo->query($this->Dbo->alterSchema($schema3->compare($schema2)));
     $indexes = $this->Dbo->index('altertest');
     $this->assertEquals($schema3->tables['altertest']['indexes'], $indexes);
     // Compare us to ourself.
     $this->assertEquals($schema3->compare($schema3), array());
     // Drop the indexes
     $this->Dbo->query($this->Dbo->alterSchema($schema1->compare($schema3)));
     $indexes = $this->Dbo->index('altertest');
     $this->assertEquals(array(), $indexes);
     $this->Dbo->query($this->Dbo->dropSchema($schema1));
 }
Example #6
0
 /**
  * test alterSchema on two tables.
  *
  * @return void
  */
 function testAlteringTwoTables()
 {
     $schema1 =& new CakeSchema(array('name' => 'AlterTest1', 'connection' => 'test_suite', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50)), 'other_table' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'name' => array('type' => 'string', 'null' => false, 'length' => 50))));
     $schema2 =& new CakeSchema(array('name' => 'AlterTest1', 'connection' => 'test_suite', 'altertest' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50)), 'other_table' => array('id' => array('type' => 'integer', 'null' => false, 'default' => 0), 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50))));
     $result = $this->db->alterSchema($schema2->compare($schema1));
     $this->assertEqual(2, substr_count($result, 'field_two'), 'Too many fields');
     $this->assertFalse(strpos(';ALTER', $result), 'Too many semi colons');
 }