chore: convert dbauthz tests to also run with Postgres (#15862)

Another PR to address https://github.com/coder/coder/issues/15109.

- adds the DisableForeignKeysAndTriggers utility, which simplifies
converting tests from in-mem to postgres
- converts the dbauthz test suite to pass on both the in-mem db and
Postgres
This commit is contained in:
Hugo Dutka
2025-01-08 16:22:51 +01:00
committed by GitHub
parent 13cfaae619
commit 106b1cd3bc
13 changed files with 1675 additions and 332 deletions
+25 -1
View File
@@ -87,6 +87,18 @@ func NewDBWithSQLDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub
return db, ps, sqlDB
}
var DefaultTimezone = "Canada/Newfoundland"
// NowInDefaultTimezone returns the current time rounded to the nearest microsecond in the default timezone
// used by postgres in tests. Useful for object equality checks.
func NowInDefaultTimezone() time.Time {
loc, err := time.LoadLocation(DefaultTimezone)
if err != nil {
panic(err)
}
return time.Now().In(loc).Round(time.Microsecond)
}
func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
t.Helper()
@@ -115,7 +127,7 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
// - It has a non-UTC offset
// - It has a fractional hour UTC offset
// - It includes a daylight savings time component
o.fixedTimezone = "Canada/Newfoundland"
o.fixedTimezone = DefaultTimezone
}
dbName := dbNameFromConnectionURL(t, connectionURL)
setDBTimezone(t, connectionURL, dbName, o.fixedTimezone)
@@ -318,3 +330,15 @@ func normalizeDump(schema []byte) []byte {
return schema
}
// Deprecated: disable foreign keys was created to aid in migrating off
// of the test-only in-memory database. Do not use this in new code.
func DisableForeignKeysAndTriggers(t *testing.T, db database.Store) {
err := db.DisableForeignKeysAndTriggers(context.Background())
if t != nil {
require.NoError(t, err)
}
if err != nil {
panic(err)
}
}