Skip to content

Commit 1caaf18

Browse files
committed
test(migrations): make the 000096 idempotency subtest actually re-run the migration
The "backfill is idempotent" subtest called migrations.RunMigrations a second time expecting it to re-exercise the DO block's guards. It does not: m.Up() returns migrate.ErrNoChange once the database is already at the latest version, so the migration body never runs again and the subtest passed regardless of whether the guards worked. Proved by mutation: stripping both idempotency devices from the up migration (the WHERE NOT guard and the DISTINCT dedup on the backfill UPDATE) still left the old test green. Rewrites the subtest to read 000096_seed_ri_exchanger_group.up.sql and execute its SQL directly a second time, the same pattern 000095_purchase_history_account_id_width_test.go already uses for its re-run assertion. Re-verified by the same mutation: with the fix, stripping the guards now fails the subtest (duplicate group_ids entry), and restoring them passes it again. The migration itself was already correctly idempotent; only the test coverage was empty.
1 parent b85f7a2 commit 1caaf18

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

‎internal/database/postgres/migrations/000096_seed_ri_exchanger_group_test.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package migrations_test
55

66
import (
77
"context"
8+
"os"
9+
"path/filepath"
810
"testing"
911

1012
"github.com/LeanerCloud/CUDly/internal/database/postgres/migrations"
@@ -116,8 +118,17 @@ func TestMigration_SeedRIExchangerGroup(t *testing.T) {
116118
`, adminEmail, adminGroupIDForPurchaserTest, riExchangerGroupIDTest)
117119
require.NoError(t, err)
118120

119-
// Re-running the seed must not duplicate the array entry.
120-
require.NoError(t, migrations.RunMigrations(ctx, pool, migrationsPath, "", ""))
121+
// Re-running migrations.RunMigrations here would NOT exercise the
122+
// idempotency guards below: m.Up() returns ErrNoChange once the
123+
// database is already at the latest version, so the migration body
124+
// never runs a second time and this subtest would pass unconditionally
125+
// regardless of whether the DO block's guards work. Reading the up
126+
// migration file and executing its SQL directly re-applies the DO
127+
// block for real, the same way 000095's re-run test does.
128+
upSQL, err := os.ReadFile(filepath.Join(migrationsPath, "000096_seed_ri_exchanger_group.up.sql"))
129+
require.NoError(t, err, "the up migration file must be readable")
130+
_, err = pool.Exec(ctx, string(upSQL))
131+
require.NoError(t, err, "re-running 000096 on an already-seeded database must be a no-op, not an error")
121132

122133
after := queryGroupIDsByEmail(t, ctx, pool, adminEmail)
123134
count := 0

0 commit comments

Comments
 (0)