Browse files

schema update CREATEs non-existing tables instead of ALTER

  • Loading branch information...
1 parent bb776cf commit c70a3868a95ab13684a9da0420f25c5e5c81dc70 @remidewitte remidewitte committed Apr 3, 2013
View
12 lib/Cake/Console/Command/SchemaShell.php
@@ -374,10 +374,18 @@ protected function _update(&$Schema, $table = null) {
if (empty($table)) {
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])) {
- $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)) {
View
2 lib/Cake/Model/CakeSchema.php
@@ -476,7 +476,7 @@ public function compare($old, $new = null) {
continue;
}
if (!array_key_exists($table, $old)) {
- $tables[$table]['add'] = $fields;
+ $tables[$table]['create'] = $fields;
} else {
$diff = $this->_arrayDiffAssoc($fields, $old[$table]);
if (!empty($diff)) {
View
36 lib/Cake/Test/Case/Console/Command/SchemaShellTest.php
@@ -79,6 +79,14 @@ class SchemaShellTestSchema extends CakeSchema {
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
'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 @@ public function testUpdateWithTable() {
$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.
*
View
12 lib/Cake/Test/Case/Model/CakeSchemaTest.php
@@ -865,13 +865,13 @@ public function testSchemaComparison() {
$compare = $New->compare($this->Schema, $tables);
$expected = array(
'ratings' => array(
- 'add' => array(
+ 'create' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'),
- 'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => null, 'after' => 'id'),
- 'model' => array('type' => 'varchar', 'null' => false, 'default' => null, 'after' => 'foreign_key'),
- 'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => null, 'after' => 'model'),
- 'created' => array('type' => 'datetime', 'null' => false, 'default' => null, 'after' => 'value'),
- 'modified' => array('type' => 'datetime', 'null' => false, 'default' => null, 'after' => 'created'),
+ 'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => null),
+ 'model' => array('type' => 'varchar', 'null' => false, 'default' => null),
+ 'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => null),
+ 'created' => array('type' => 'datetime', 'null' => false, 'default' => null),
+ 'modified' => array('type' => 'datetime', 'null' => false, 'default' => null),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
)

0 comments on commit c70a386

Please sign in to comment.