setRandDBTimezone sets the timezone of the database to the given timezone. Note that the updated timezone only comes into effect on reconnect, so we create our own connection for this and close the DB after we're done.
(t testing.TB, dbURL, dbname, tz string)
| 149 | // Note that the updated timezone only comes into effect on reconnect, so we |
| 150 | // create our own connection for this and close the DB after we're done. |
| 151 | func setDBTimezone(t testing.TB, dbURL, dbname, tz string) { |
| 152 | t.Helper() |
| 153 | |
| 154 | sqlDB, err := sql.Open("postgres", dbURL) |
| 155 | require.NoError(t, err) |
| 156 | defer func() { |
| 157 | _ = sqlDB.Close() |
| 158 | }() |
| 159 | |
| 160 | // nolint: gosec // This unfortunately does not work with placeholders. |
| 161 | _, err = sqlDB.Exec(fmt.Sprintf("ALTER DATABASE %s SET TIMEZONE TO %q", dbname, tz)) |
| 162 | require.NoError(t, err, "failed to set timezone for database") |
| 163 | } |
| 164 | |
| 165 | // dbNameFromConnectionURL returns the database name from the given connection URL, |
| 166 | // where connectionURL is of the form postgres://user:pass@host:port/dbname |