compare() 공개 메소드

Compares two sets of schemas.
public compare ( array | object $old, array | object $new = null ) : array
$old array | object Schema object or array.
$new array | object Schema object or array.
리턴 array Tables (that are added, dropped, or changed.)
 /**
  * Update database with Schema object
  * Should be called via the run method
  *
  * @param CakeSchema $Schema
  * @param string $table
  * @return void
  */
 protected function _update(&$Schema, $table = null)
 {
     $db = ConnectionManager::getDataSource($this->Schema->connection);
     $this->out(__d('cake_console', 'Comparing Database to Schema...'));
     $options = array();
     if (isset($this->params['force'])) {
         $options['models'] = false;
     }
     $Old = $this->Schema->read($options);
     $compare = $this->Schema->compare($Old, $Schema);
     $contents = array();
     if (empty($table)) {
         foreach ($compare as $table => $changes) {
             $contents[$table] = $db->alterSchema(array($table => $changes), $table);
         }
     } elseif (isset($compare[$table])) {
         $contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
     }
     if (empty($contents)) {
         $this->out(__d('cake_console', 'Schema is up to date.'));
         $this->_stop();
     }
     $this->out("\n" . __d('cake_console', 'The following statements will run.'));
     $this->out(array_map('trim', $contents));
     if ('y' == $this->in(__d('cake_console', 'Are you sure you want to alter the tables?'), array('y', 'n'), 'n')) {
         $this->out();
         $this->out(__d('cake_console', 'Updating Database...'));
         $this->_run($contents, 'update', $Schema);
     }
     $this->out(__d('cake_console', 'End update.'));
 }
예제 #2
0
 /**
  * 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->db->alterSchema($schema2->compare($schema1));
     $this->assertEquals(2, substr_count($result, 'field_two'), 'Too many fields');
     $this->assertFalse(strpos(';ALTER', $result), 'Too many semi colons');
 }
예제 #3
0
 /**
  * Test the alter index capabilities of postgres
  *
  * @access public
  * @return void
  */
 function testAlterIndexes()
 {
     $this->db->cacheSources = false;
     $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), 'group1' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true))));
     $this->db->query($this->db->createSchema($schema1));
     $schema2 = new CakeSchema(array('name' => 'AlterTest2', 'connection' => 'test_suite', '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('column' => 'name', 'unique' => 0), 'group_idx' => array('column' => 'group1', 'unique' => 0), 'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0), 'PRIMARY' => array('column' => 'id', 'unique' => 1)))));
     $this->db->query($this->db->alterSchema($schema2->compare($schema1)));
     $indexes = $this->db->index('altertest');
     $this->assertEqual($schema2->tables['altertest']['indexes'], $indexes);
     // Change three indexes, delete one and add another one
     $schema3 = new CakeSchema(array('name' => 'AlterTest3', 'connection' => 'test_suite', '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('column' => 'name', 'unique' => 1), 'group_idx' => array('column' => 'group2', 'unique' => 0), 'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => 0), 'another_idx' => array('column' => array('group1', 'name'), 'unique' => 0)))));
     $this->db->query($this->db->alterSchema($schema3->compare($schema2)));
     $indexes = $this->db->index('altertest');
     $this->assertEqual($schema3->tables['altertest']['indexes'], $indexes);
     // Compare us to ourself.
     $this->assertEqual($schema3->compare($schema3), array());
     // Drop the indexes
     $this->db->query($this->db->alterSchema($schema1->compare($schema3)));
     $indexes = $this->db->index('altertest');
     $this->assertEqual(array(), $indexes);
     $this->db->query($this->db->dropSchema($schema1));
 }
예제 #4
0
 /**
  * test comparing '' and null and making sure they are different.
  *
  * @return void
  */
 public function testCompareEmptyStringAndNull()
 {
     $One = new CakeSchema(array('posts' => array('id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'), 'name' => array('type' => 'string', 'null' => false, 'default' => ''))));
     $Two = new CakeSchema(array('posts' => array('id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'), 'name' => array('type' => 'string', 'null' => false, 'default' => null))));
     $compare = $One->compare($Two);
     $expected = array('posts' => array('change' => array('name' => array('type' => 'string', 'null' => false, 'default' => null))));
     $this->assertEquals($expected, $compare);
 }
예제 #5
0
파일: Enum.php 프로젝트: razzman/mi_enums
 /**
  * afterSave method
  *
  * If a default has just been saved check the db and update the table if the db default is different.
  *
  * @return void
  * @access public
  */
 function afterSave()
 {
     if (isset($this->data['Enum']['default']) && $this->data['Enum']['default']) {
         $conditions['type'] = isset($this->data['Enum']['type']) ? $this->data['Enum']['type'] : $this->field('type');
         $conditions['NOT']['Enum.id'] = $this->id;
         $this->updateAll(array('default' => 'false'), $conditions);
         $default = isset($this->data['Enum']['value']) ? $this->data['Enum']['value'] : $this->field('value');
         if ($default) {
             $db =& ConnectionManager::getDataSource($this->useDbConfig);
             App::import('Model', 'CakeSchema');
             $Schema = new CakeSchema(array('connection' => $this->useDbConfig));
             $update = $current = $Schema->read(array('models' => false));
             list($model, $field) = explode('.', $conditions['type']);
             $table = Inflector::tableize($model);
             if (isset($update['tables'][$table][$field])) {
                 $update['tables'][$table][$field]['default'] = $default;
                 $update['tables'][$table][$field]['null'] = true;
                 $compare = $Schema->compare($current, $update);
                 foreach ($compare as $table => $changes) {
                     $db->execute($db->alterSchema(array($table => $changes), $table));
                 }
                 Cache::delete($this->useDbConfig . '_' . $table, '_cake_model_');
             }
         }
     }
 }