Skip to content
This repository
Browse code

Fix issue where createSchema() would omit primary keys sometimes.

Fix missing primary key SQL when using the primary flag + other indexes.

Fixes #3292
  • Loading branch information...
commit a0a61b5980272ec98dba0f02c9896d23a30d51fc 1 parent 1c0c860
Mark Story authored
2  lib/Cake/Model/Datasource/DboSource.php
@@ -2984,7 +2984,7 @@ public function createSchema($schema, $tableName = null) {
2984 2984
 						$tableParameters = array_merge($tableParameters, $this->buildTableParameters($col, $table));
2985 2985
 					}
2986 2986
 				}
2987  
-				if (empty($indexes) && !empty($primary)) {
  2987
+				if (!isset($columns['indexes']['PRIMARY']) && !empty($primary)) {
2988 2988
 					$col = array('PRIMARY' => array('column' => $primary, 'unique' => 1));
2989 2989
 					$indexes = array_merge($indexes, $this->buildIndex($col, $table));
2990 2990
 				}
46  lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
@@ -878,6 +878,52 @@ public function testTwoColumnsWithPrimaryKey() {
878 878
 	}
879 879
 
880 880
 /**
  881
+ * Test that the primary flag is handled correctly.
  882
+ *
  883
+ * @return void
  884
+ */
  885
+	public function testCreateSchemaAutoPrimaryKey() {
  886
+		$schema = new CakeSchema();
  887
+		$schema->tables = array(
  888
+			'no_indexes' => array(
  889
+				'id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
  890
+				'data' => array('type' => 'integer', 'null' => false),
  891
+				'indexes' => array(),
  892
+			)
  893
+		);
  894
+		$result = $this->Dbo->createSchema($schema, 'no_indexes');
  895
+		$this->assertContains('PRIMARY KEY  (`id`)', $result);
  896
+		$this->assertNotContains('UNIQUE KEY', $result);
  897
+
  898
+		$schema->tables = array(
  899
+			'primary_index' => array(
  900
+				'id' => array('type' => 'integer', 'null' => false),
  901
+				'data' => array('type' => 'integer', 'null' => false),
  902
+				'indexes' => array(
  903
+					'PRIMARY' => array('column' => 'id', 'unique' => 1),
  904
+					'some_index' => array('column' => 'data', 'unique' => 1)
  905
+				),
  906
+			)
  907
+		);
  908
+		$result = $this->Dbo->createSchema($schema, 'primary_index');
  909
+		$this->assertContains('PRIMARY KEY  (`id`)', $result);
  910
+		$this->assertContains('UNIQUE KEY `some_index` (`data`)', $result);
  911
+
  912
+		$schema->tables = array(
  913
+			'primary_flag_has_index' => array(
  914
+				'id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
  915
+				'data' => array('type' => 'integer', 'null' => false),
  916
+				'indexes' => array (
  917
+					'some_index' => array('column' => 'data', 'unique' => 1)
  918
+				),
  919
+			)
  920
+		);
  921
+		$result = $this->Dbo->createSchema($schema, 'primary_flag_has_index');
  922
+		$this->assertContains('PRIMARY KEY  (`id`)', $result);
  923
+		$this->assertContains('UNIQUE KEY `some_index` (`data`)', $result);
  924
+	}
  925
+
  926
+/**
881 927
  * Tests that listSources method sends the correct query and parses the result accordingly
882 928
  * @return void
883 929
  */

0 notes on commit a0a61b5

Please sign in to comment.
Something went wrong with that request. Please try again.