refactor(coderd/database): split Time and Now into dbtime package (#9482)

Ref: #9380
This commit is contained in:
Mathias Fredriksson
2023-09-01 16:50:12 +00:00
committed by GitHub
parent 702b064cac
commit 19d7da3d24
89 changed files with 466 additions and 390 deletions
+8 -7
View File
@@ -20,6 +20,7 @@ import (
"github.com/coder/coder/v2/buildinfo"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/database/pubsub"
)
@@ -66,9 +67,9 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, ps pubsub.P
// nolint:gocritic // Inserting a replica is a system function.
replica, err := db.InsertReplica(dbauthz.AsSystemRestricted(ctx), database.InsertReplicaParams{
ID: options.ID,
CreatedAt: database.Now(),
StartedAt: database.Now(),
UpdatedAt: database.Now(),
CreatedAt: dbtime.Now(),
StartedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
Hostname: hostname,
RegionID: options.RegionID,
RelayAddress: options.RelayAddress,
@@ -144,7 +145,7 @@ func (m *Manager) PublishUpdate() error {
// If the replica was updated > the time, it's considered healthy.
// If the replica was updated < the time, it's considered stale.
func (m *Manager) updateInterval() time.Time {
return database.Now().Add(-3 * m.options.UpdateInterval)
return dbtime.Now().Add(-3 * m.options.UpdateInterval)
}
// loop runs the replica update sequence on an update interval.
@@ -305,7 +306,7 @@ func (m *Manager) syncReplicas(ctx context.Context) error {
// nolint:gocritic // Updating a replica is a system function.
replica, err := m.db.UpdateReplica(dbauthz.AsSystemRestricted(ctx), database.UpdateReplicaParams{
ID: m.self.ID,
UpdatedAt: database.Now(),
UpdatedAt: dbtime.Now(),
StartedAt: m.self.StartedAt,
StoppedAt: m.self.StoppedAt,
RelayAddress: m.self.RelayAddress,
@@ -413,10 +414,10 @@ func (m *Manager) Close() error {
// nolint:gocritic // Updating a replica is a system function.
_, err := m.db.UpdateReplica(dbauthz.AsSystemRestricted(ctx), database.UpdateReplicaParams{
ID: m.self.ID,
UpdatedAt: database.Now(),
UpdatedAt: dbtime.Now(),
StartedAt: m.self.StartedAt,
StoppedAt: sql.NullTime{
Time: database.Now(),
Time: dbtime.Now(),
Valid: true,
},
RelayAddress: m.self.RelayAddress,
+12 -11
View File
@@ -18,6 +18,7 @@ import (
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbfake"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/database/pubsub"
"github.com/coder/coder/v2/enterprise/replicasync"
"github.com/coder/coder/v2/testutil"
@@ -61,9 +62,9 @@ func TestReplica(t *testing.T) {
db, pubsub := dbtestutil.NewDB(t)
peer, err := db.InsertReplica(context.Background(), database.InsertReplicaParams{
ID: uuid.New(),
CreatedAt: database.Now(),
StartedAt: database.Now(),
UpdatedAt: database.Now(),
CreatedAt: dbtime.Now(),
StartedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
Hostname: "something",
RelayAddress: srv.URL,
Primary: true,
@@ -106,9 +107,9 @@ func TestReplica(t *testing.T) {
db, pubsub := dbtestutil.NewDB(t)
peer, err := db.InsertReplica(context.Background(), database.InsertReplicaParams{
ID: uuid.New(),
CreatedAt: database.Now(),
StartedAt: database.Now(),
UpdatedAt: database.Now(),
CreatedAt: dbtime.Now(),
StartedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
Hostname: "something",
RelayAddress: srv.URL,
Primary: true,
@@ -133,9 +134,9 @@ func TestReplica(t *testing.T) {
db, pubsub := dbtestutil.NewDB(t)
peer, err := db.InsertReplica(context.Background(), database.InsertReplicaParams{
ID: uuid.New(),
CreatedAt: database.Now().Add(time.Minute),
StartedAt: database.Now().Add(time.Minute),
UpdatedAt: database.Now().Add(time.Minute),
CreatedAt: dbtime.Now().Add(time.Minute),
StartedAt: dbtime.Now().Add(time.Minute),
UpdatedAt: dbtime.Now().Add(time.Minute),
Hostname: "something",
// Fake address to dial!
RelayAddress: "http://127.0.0.1:1",
@@ -173,7 +174,7 @@ func TestReplica(t *testing.T) {
peer, err := db.InsertReplica(ctx, database.InsertReplicaParams{
ID: uuid.New(),
RelayAddress: srv.URL,
UpdatedAt: database.Now(),
UpdatedAt: dbtime.Now(),
Primary: true,
})
require.NoError(t, err)
@@ -192,7 +193,7 @@ func TestReplica(t *testing.T) {
db, pubsub := dbtestutil.NewDB(t)
_, err := db.InsertReplica(context.Background(), database.InsertReplicaParams{
ID: uuid.New(),
UpdatedAt: database.Now().Add(-time.Hour),
UpdatedAt: dbtime.Now().Add(-time.Hour),
Primary: true,
})
require.NoError(t, err)