Browse Source

Merge pull request #6774 from cakephp/issue-6668

Update MySQL table generation for non-conventional schemas
José Lorenzo Rodríguez 10 years ago
parent
commit
14d03282bd

+ 8 - 4
src/Database/Schema/MysqlSchema.php

@@ -221,9 +221,9 @@ class MysqlSchema extends BaseSchema
     public function describeForeignKeySql($tableName, $config)
     {
         $sql = 'SELECT * FROM information_schema.key_column_usage AS kcu
-			INNER JOIN information_schema.referential_constraints AS rc
-			ON (kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME)
-			WHERE kcu.TABLE_SCHEMA = ? AND kcu.TABLE_NAME = ? and rc.TABLE_NAME = ?';
+            INNER JOIN information_schema.referential_constraints AS rc
+            ON (kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME)
+            WHERE kcu.TABLE_SCHEMA = ? AND kcu.TABLE_NAME = ? and rc.TABLE_NAME = ?';
 
         return [$sql, [$config['database'], $tableName, $tableName]];
     }
@@ -332,8 +332,12 @@ class MysqlSchema extends BaseSchema
         if (isset($data['null']) && $data['null'] === false) {
             $out .= ' NOT NULL';
         }
+        $addAutoIncrement = (
+            [$name] == (array)$table->primaryKey() &&
+            !$table->hasAutoIncrement()
+        );
         if (in_array($data['type'], ['integer', 'biginteger']) &&
-            ([$name] == (array)$table->primaryKey() || $data['autoIncrement'] === true)
+            ($data['autoIncrement'] === true || $addAutoIncrement)
         ) {
             $out .= ' AUTO_INCREMENT';
         }

+ 16 - 0
src/Database/Schema/Table.php

@@ -508,6 +508,7 @@ class Table
                 throw new Exception($msg);
             }
         }
+
         if ($attrs['type'] === static::CONSTRAINT_FOREIGN) {
             $attrs = $this->_checkForeignKey($attrs);
         } else {
@@ -518,6 +519,21 @@ class Table
     }
 
     /**
+     * Check whether or not a table has an autoIncrement column defined.
+     *
+     * @return bool
+     */
+    public function hasAutoincrement()
+    {
+        foreach ($this->_columns as $column) {
+            if (isset($column['autoIncrement']) && $column['autoIncrement']) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Helper method to check/validate foreign keys.
      *
      * @param array $attrs Attributes to set.

+ 32 - 1
tests/TestCase/Database/Schema/MysqlSchemaTest.php

@@ -390,6 +390,37 @@ SQL;
         $this->assertArrayHasKey('collation', $result->options());
     }
 
+    public function testDescribeNonPrimaryAutoIncrement()
+    {
+        $this->_needsConnection();
+        $connection = ConnectionManager::get('test');
+
+        $sql = <<<SQL
+CREATE TABLE `odd_primary_key` (
+`id` BIGINT UNSIGNED NOT NULL,
+`other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,
+PRIMARY KEY (`id`),
+UNIQUE KEY `other_field` (`other_field`)
+)
+SQL;
+        $connection->execute($sql);
+        $schema = new SchemaCollection($connection);
+        $table = $schema->describe('odd_primary_key');
+        $connection->execute('DROP TABLE odd_primary_key');
+
+        $column = $table->column('id');
+        $this->assertNull($column['autoIncrement'], 'should not autoincrement');
+        $this->assertTrue($column['unsigned'], 'should be unsigned');
+
+        $column = $table->column('other_field');
+        $this->assertTrue($column['autoIncrement'], 'should not autoincrement');
+        $this->assertFalse($column['unsigned'], 'should not be unsigned');
+
+        $output = $table->createSql($connection);
+        $this->assertContains('`id` BIGINT UNSIGNED NOT NULL,', $output[0]);
+        $this->assertContains('`other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,', $output[0]);
+    }
+
     /**
      * Column provider for creating column sql
      *
@@ -689,7 +720,7 @@ SQL;
         $table = new Table('articles');
         $table->addColumn('id', [
                 'type' => 'integer',
-                'null' => false
+                'null' => false,
             ])
             ->addConstraint('primary', [
                 'type' => 'primary',