Skip to content

Commit 84ecd06

Browse files
committed
test: apply database migrations in end-to-end test setup
- Updated the `TestSupabaseAuthUserSyncRunner_EndToEnd` to apply necessary database migrations before running tests. - Refactored the `SetupDatabase` function to include a new method `ApplyMigrations` for better migration management.
1 parent 4d622ce commit 84ecd06

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

packages/dashboard-api/internal/supabaseauthusersync/runner_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func TestSupabaseAuthUserSyncRunner_EndToEnd(t *testing.T) {
4444
t.Parallel()
4545

4646
db := testutils.SetupDatabase(t)
47+
db.ApplyMigrations(t, "packages/db/pkg/auth/migrations")
4748

4849
runRepairsInsertUpdateDeleteDrift(t, db)
4950
runReclaimsStaleQueueLocks(t, db)

packages/db/pkg/testutils/db.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Database struct {
3838
SqlcClient *db.Client
3939
AuthDb *authdb.Client
4040
TestQueries *queries.Queries
41+
connStr string
4142
}
4243

4344
// SetupDatabase creates a fresh PostgreSQL container with migrations applied
@@ -103,37 +104,41 @@ func SetupDatabase(t *testing.T) *Database {
103104
SqlcClient: sqlcClient,
104105
AuthDb: authDb,
105106
TestQueries: testQueries,
107+
connStr: connStr,
106108
}
107109
}
108110

109-
// runDatabaseMigrations executes all required database migrations
110-
func runDatabaseMigrations(t *testing.T, connStr string) {
111+
func (db *Database) ApplyMigrations(t *testing.T, migrationDirs ...string) {
111112
t.Helper()
112113

113114
cmd := exec.CommandContext(t.Context(), "git", "rev-parse", "--show-toplevel")
114115
output, err := cmd.Output()
115116
require.NoError(t, err, "Failed to find git root")
116117
repoRoot := strings.TrimSpace(string(output))
117118

118-
db, err := goose.OpenDBWithDriver("pgx", connStr)
119+
sqlDB, err := goose.OpenDBWithDriver("pgx", db.connStr)
119120
require.NoError(t, err)
120121
t.Cleanup(func() {
121-
err := db.Close()
122+
err := sqlDB.Close()
122123
assert.NoError(t, err)
123124
})
124125

125-
// run the db migration
126-
for _, migrationsDir := range []string{
127-
filepath.Join(repoRoot, "packages", "db", "migrations"),
128-
filepath.Join(repoRoot, "packages", "db", "pkg", "auth", "migrations"),
129-
} {
126+
for _, migrationsDir := range migrationDirs {
130127
err = goose.RunWithOptionsContext(
131128
t.Context(),
132129
"up",
133-
db,
134-
migrationsDir,
130+
sqlDB,
131+
filepath.Join(repoRoot, migrationsDir),
135132
nil,
136133
)
137134
require.NoError(t, err)
138135
}
139136
}
137+
138+
// runDatabaseMigrations executes all required database migrations
139+
func runDatabaseMigrations(t *testing.T, connStr string) {
140+
t.Helper()
141+
142+
db := &Database{connStr: connStr}
143+
db.ApplyMigrations(t, filepath.Join("packages", "db", "migrations"))
144+
}

0 commit comments

Comments
 (0)