Skip to content
Permalink
Browse files

Extract a method for building constraints.

Refs #7871
  • Loading branch information...
markstory committed Dec 19, 2015
1 parent ef27fb4 commit 00fdc44798dc120bcff8cdeaff83e40144b0ea92
Showing with 36 additions and 22 deletions.
  1. +36 −22 src/Database/Schema/PostgresSchema.php
@@ -234,28 +234,7 @@ public function convertIndexDescription(Table $table, $row)
$type = Table::CONSTRAINT_UNIQUE;
}
if ($type === Table::CONSTRAINT_PRIMARY || $type === Table::CONSTRAINT_UNIQUE) {
$constraint = $table->constraint($name);
if (!$constraint) {
$constraint = [
'type' => $type,
'columns' => []
];
}
$constraint['columns'][] = $row['attname'];
$table->addConstraint($name, $constraint);
// If there is only one column in the primary key and it is integery,
// make it autoincrement.
$columns = $constraint['columns'];
$columnDef = $table->column($columns[0]);
if ($type === Table::CONSTRAINT_PRIMARY &&
count($columns) === 1 &&
in_array($columnDef['type'], ['integer', 'biginteger'])
) {
$columnDef['autoIncrement'] = true;
$table->addColumn($columns[0], $columnDef);
}
$this->_convertConstraint($table, $name, $type, $row);
return;
}
$index = $table->index($name);
@@ -269,6 +248,41 @@ public function convertIndexDescription(Table $table, $row)
$table->addIndex($name, $index);
}
/**
* Add/update a constraint into the schema object.
*
* @param \Cake\Database\Schema\Table $table The table to update.
* @param string $name The index name.
* @param string $type The index type.
* @param array $row The metadata record to update with.
* @return void
*/
protected function _convertConstraint($table, $name, $type, $row)
{
$constraint = $table->constraint($name);
if (!$constraint) {
$constraint = [
'type' => $type,
'columns' => []
];
}
$constraint['columns'][] = $row['attname'];
$table->addConstraint($name, $constraint);
// If there is only one column in the primary key and it is integery,
// make it autoincrement.
$columns = $constraint['columns'];
$columnDef = $table->column($columns[0]);
if ($type === Table::CONSTRAINT_PRIMARY &&
count($columns) === 1 &&
in_array($columnDef['type'], ['integer', 'biginteger'])
) {
$columnDef['autoIncrement'] = true;
$table->addColumn($columns[0], $columnDef);
}
}
/**
* {@inheritDoc}
*/

0 comments on commit 00fdc44

Please sign in to comment.
You can’t perform that action at this time.