Skip to content

Commit b80c3dd

Browse files
committed
Fix PostgreSQL sequence reset for test fixtures
PostgreSQL sequences don't auto-increment when inserting records with explicit IDs, causing duplicate key violations on subsequent auto-increment inserts in fresh test environments. This was masked locally by "warmed up" sequences from previous test runs but failed consistently in CI with fresh databases. Reset sequences to MAX(id) after buildFixtures() inserts hardcoded IDs to ensure next auto-increment returns correct value. Also add InnoDB engine and strict mode to MySQL connection for better test reliability.
1 parent 0245411 commit b80c3dd

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

tests/Helpers/CanBuildTestFixtures.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,17 @@ protected function buildFixtures()
122122
['pup_id' => 6, 'pup_food_id' => 1], // Duchess eats 4Health
123123
['pup_id' => 6, 'pup_food_id' => 2] // Duchess eats Taste of the Wild
124124
]);
125+
126+
// Reset PostgreSQL sequences after manually inserting records with explicit IDs
127+
// PostgreSQL sequences don't auto-update when you insert with explicit IDs,
128+
// causing duplicate key errors on subsequent auto-increment inserts
129+
if (Capsule::connection()->getDriverName() === 'pgsql') {
130+
Capsule::statement("SELECT setval('companies_id_seq', (SELECT MAX(id) FROM companies))");
131+
Capsule::statement("SELECT setval('pup_foods_id_seq', (SELECT MAX(id) FROM pup_foods))");
132+
Capsule::statement("SELECT setval('users_id_seq', (SELECT MAX(id) FROM users))");
133+
Capsule::statement("SELECT setval('packs_id_seq', (SELECT MAX(id) FROM packs))");
134+
Capsule::statement("SELECT setval('pups_id_seq', (SELECT MAX(id) FROM pups))");
135+
Capsule::statement("SELECT setval('collars_id_seq', (SELECT MAX(id) FROM collars))");
136+
}
125137
}
126138
}

tests/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
'collation' => 'utf8_unicode_ci',
5353
'prefix' => '',
5454
'schema' => 'public',
55+
'engine' => 'InnoDB',
56+
'strict' => true,
5557
]);
5658
} else if ($dbDriver === 'pgsql') {
5759
$capsule->addConnection([

0 commit comments

Comments
 (0)