Skip to content

Commit

Permalink
Extract a method for building constraints.
Browse files Browse the repository at this point in the history
Refs #7871
  • Loading branch information
markstory committed Dec 19, 2015
1 parent ef27fb4 commit 00fdc44
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions src/Database/Schema/PostgresSchema.php
Expand Up @@ -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);
Expand All @@ -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}
*/
Expand Down

0 comments on commit 00fdc44

Please sign in to comment.