schema update CREATEs non-existing tables instead of ALTER

This commit is contained in:
Rémi Dewitte 2013-04-03 17:37:09 +02:00
parent bb776cfa72
commit c70a3868a9
4 changed files with 53 additions and 9 deletions

View file

@ -374,10 +374,18 @@ class SchemaShell extends AppShell {
if (empty($table)) { if (empty($table)) {
foreach ($compare as $table => $changes) { foreach ($compare as $table => $changes) {
$contents[$table] = $db->alterSchema(array($table => $changes), $table); if (isset($compare[$table]['create'])) {
$contents[$table] = $db->createSchema($Schema, $table);
} else {
$contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
}
} }
} elseif (isset($compare[$table])) { } elseif (isset($compare[$table])) {
$contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table); if (isset($compare[$table]['create'])) {
$contents[$table] = $db->createSchema($Schema, $table);
} else {
$contents[$table] = $db->alterSchema(array($table => $compare[$table]), $table);
}
} }
if (empty($contents)) { if (empty($contents)) {

View file

@ -476,7 +476,7 @@ class CakeSchema extends Object {
continue; continue;
} }
if (!array_key_exists($table, $old)) { if (!array_key_exists($table, $old)) {
$tables[$table]['add'] = $fields; $tables[$table]['create'] = $fields;
} else { } else {
$diff = $this->_arrayDiffAssoc($fields, $old[$table]); $diff = $this->_arrayDiffAssoc($fields, $old[$table]);
if (!empty($diff)) { if (!empty($diff)) {

View file

@ -79,6 +79,14 @@ class SchemaShellTestSchema extends CakeSchema {
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null), 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
); );
public $newone = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
'testit' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
);
} }
/** /**
@ -507,6 +515,34 @@ class SchemaShellTest extends CakeTestCase {
$this->Shell->update(); $this->Shell->update();
} }
/**
* test run update with a table arg. and checks that a CREATE statement is issued
* table creation
* @return void
*/
public function testUpdateWithTableCreate() {
$this->Shell = $this->getMock(
'SchemaShell',
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_run'),
array(&$this->Dispatcher)
);
$this->Shell->params = array(
'connection' => 'test',
'force' => true
);
$this->Shell->args = array('SchemaShellTest', 'newone');
$this->Shell->startup();
$this->Shell->expects($this->any())
->method('in')
->will($this->returnValue('y'));
$r = $this->Shell->expects($this->once())
->method('_run')
->with($this->arrayHasKey('newone'), 'update', $this->isInstanceOf('CakeSchema'));
$this->Shell->update();
}
/** /**
* test that the plugin param creates the correct path in the schema object. * test that the plugin param creates the correct path in the schema object.
* *

View file

@ -865,13 +865,13 @@ class CakeSchemaTest extends CakeTestCase {
$compare = $New->compare($this->Schema, $tables); $compare = $New->compare($this->Schema, $tables);
$expected = array( $expected = array(
'ratings' => array( 'ratings' => array(
'add' => array( 'create' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'), 'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'),
'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => null, 'after' => 'id'), 'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => null),
'model' => array('type' => 'varchar', 'null' => false, 'default' => null, 'after' => 'foreign_key'), 'model' => array('type' => 'varchar', 'null' => false, 'default' => null),
'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => null, 'after' => 'model'), 'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => null),
'created' => array('type' => 'datetime', 'null' => false, 'default' => null, 'after' => 'value'), 'created' => array('type' => 'datetime', 'null' => false, 'default' => null),
'modified' => array('type' => 'datetime', 'null' => false, 'default' => null, 'after' => 'created'), 'modified' => array('type' => 'datetime', 'null' => false, 'default' => null),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)), 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM') 'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
) )