Skip to content

Commit 55cd6b8

Browse files
TDannhauerralflang
authored andcommitted
Update Schema.php
refactor: * only warn on missing sequences where sequences make sense * Hande composite PKs correctly
1 parent 3d8112a commit 55cd6b8

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/Adapter/Postgresql/Schema.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,17 +1137,34 @@ public function pkAndSequenceFor($table)
11371137

11381138
if (!$result) {
11391139
$sql = "
1140-
SELECT c.column_name, c.ordinal_position,
1141-
pg_get_serial_sequence(t.table_name, c.column_name) as relname
1140+
SELECT c.column_name as attname, c.ordinal_position,
1141+
pg_get_serial_sequence(t.table_name, c.column_name) as relname,
1142+
col.data_type
11421143
FROM information_schema.key_column_usage AS c
11431144
LEFT JOIN information_schema.table_constraints AS t
11441145
ON t.constraint_name = c.constraint_name
1146+
LEFT JOIN information_schema.columns AS col
1147+
ON col.table_name = t.table_name
1148+
AND col.column_name = c.column_name
11451149
WHERE t.table_name = '$table' AND t.constraint_type = 'PRIMARY KEY';";
1146-
$result = $this->adapter->selectOne($sql, 'PK and custom sequence');
1150+
$results = $this->selectAll($sql, 'PK and custom sequence');
1151+
1152+
// Return null if no results or multiple rows
1153+
// (multiple rows means composite PK with multiple columns where autoincrement is not supported )
1154+
if (!$results || count($results) > 1) {
1155+
return array(null, null);
1156+
}
1157+
1158+
$result = $results[0];
11471159
}
11481160

1149-
if (!$result) {
1150-
return array(null, null);
1161+
// Only warn about missing sequences for plain integer primary keys
1162+
if (isset($result['data_type']) &&
1163+
(strpos($result['data_type'], 'int') !== false ||
1164+
strpos($result['data_type'], 'serial') !== false)) {
1165+
if ($this->_logger && !$result['relname']) {
1166+
$this->_logger->warn(sprintf('%s has Primary key %s with no default sequence', $table, $result['attname']));
1167+
}
11511168
}
11521169

11531170
// [primary_key, sequence]

0 commit comments

Comments
 (0)