public convertColumnDescription ( Table $table, $row ) | ||
$table | Table |
public function convertColumnDescription(Table $table, $row)
{
$field = $this->_convertColumn($row['type']);
$field += ['null' => !$row['notnull'], 'default' => $this->_defaultValue($row['dflt_value'])];
$primary = $table->constraint('primary');
if ($row['pk'] && empty($primary)) {
$field['null'] = false;
$field['autoIncrement'] = true;
}
// SQLite does not support autoincrement on composite keys.
if ($row['pk'] && !empty($primary)) {
$existingColumn = $primary['columns'][0];
$table->addColumn($existingColumn, ['autoIncrement' => null] + $table->column($existingColumn));
}
$table->addColumn($row['name'], $field);
if ($row['pk']) {
$constraint = (array) $table->constraint('primary') + ['type' => Table::CONSTRAINT_PRIMARY, 'columns' => []];
$constraint['columns'] = array_merge($constraint['columns'], [$row['name']]);
$table->addConstraint('primary', $constraint);
}
}
/** * Tests converting multiple rows into a primary constraint with multiple * columns * * @return void */ public function testConvertCompositePrimaryKey() { $driver = $this->getMock('Cake\\Database\\Driver\\Sqlite'); $dialect = new SqliteSchema($driver); $field1 = ['pk' => true, 'name' => 'field1', 'type' => 'INTEGER(11)', 'notnull' => false, 'dflt_value' => 0]; $field2 = ['pk' => true, 'name' => 'field2', 'type' => 'INTEGER(11)', 'notnull' => false, 'dflt_value' => 1]; $table = new \Cake\Database\Schema\Table('table'); $dialect->convertColumnDescription($table, $field1); $dialect->convertColumnDescription($table, $field2); $this->assertEquals(['field1', 'field2'], $table->primaryKey()); }