From 8cf237e01584841733aa7da626a4b1d526470427 Mon Sep 17 00:00:00 2001 From: noah Date: Sat, 26 Feb 2022 15:51:06 +0900 Subject: [PATCH 1/2] Add default value to the 'latest_deployed_at field --- model/ent/entc.go | 2 +- model/ent/repo/repo.go | 2 ++ model/ent/repo_create.go | 4 ++++ model/ent/runtime.go | 4 ++++ model/ent/schema/repo.go | 1 + 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/model/ent/entc.go b/model/ent/entc.go index 93c55b5d..99d306e0 100644 --- a/model/ent/entc.go +++ b/model/ent/entc.go @@ -1,4 +1,4 @@ -// +build ignore +//go:build ignore package main diff --git a/model/ent/repo/repo.go b/model/ent/repo/repo.go index ac78e78e..64dbcbd0 100644 --- a/model/ent/repo/repo.go +++ b/model/ent/repo/repo.go @@ -116,4 +116,6 @@ var ( DefaultUpdatedAt func() time.Time // UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field. UpdateDefaultUpdatedAt func() time.Time + // DefaultLatestDeployedAt holds the default value on creation for the "latest_deployed_at" field. + DefaultLatestDeployedAt func() time.Time ) diff --git a/model/ent/repo_create.go b/model/ent/repo_create.go index 89d384e0..21107a8a 100644 --- a/model/ent/repo_create.go +++ b/model/ent/repo_create.go @@ -299,6 +299,10 @@ func (rc *RepoCreate) defaults() { v := repo.DefaultUpdatedAt() rc.mutation.SetUpdatedAt(v) } + if _, ok := rc.mutation.LatestDeployedAt(); !ok { + v := repo.DefaultLatestDeployedAt() + rc.mutation.SetLatestDeployedAt(v) + } } // check runs all checks and user-defined validators on the builder. diff --git a/model/ent/runtime.go b/model/ent/runtime.go index 43d4ffe0..7a80ec2b 100644 --- a/model/ent/runtime.go +++ b/model/ent/runtime.go @@ -154,6 +154,10 @@ func init() { repo.DefaultUpdatedAt = repoDescUpdatedAt.Default.(func() time.Time) // repo.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. repo.UpdateDefaultUpdatedAt = repoDescUpdatedAt.UpdateDefault.(func() time.Time) + // repoDescLatestDeployedAt is the schema descriptor for latest_deployed_at field. + repoDescLatestDeployedAt := repoFields[9].Descriptor() + // repo.DefaultLatestDeployedAt holds the default value on creation for the latest_deployed_at field. + repo.DefaultLatestDeployedAt = repoDescLatestDeployedAt.Default.(func() time.Time) reviewFields := schema.Review{}.Fields() _ = reviewFields // reviewDescCreatedAt is the schema descriptor for created_at field. diff --git a/model/ent/schema/repo.go b/model/ent/schema/repo.go index 0888b86c..bfe13025 100644 --- a/model/ent/schema/repo.go +++ b/model/ent/schema/repo.go @@ -34,6 +34,7 @@ func (Repo) Fields() []ent.Field { UpdateDefault(nowUTC), // Denormalization to sort with deployment. field.Time("latest_deployed_at"). + Default(nowUTC). Optional(), // The 'owner_id' field is the ID who activated the repository. field.Int64("owner_id"). From e3ac808c37b5ea5dc31a66c6bf800426af81ac6c Mon Sep 17 00:00:00 2001 From: noah Date: Sat, 26 Feb 2022 15:55:21 +0900 Subject: [PATCH 2/2] Replace the method --- model/ent/schema/repo.go | 2 +- model/ent/schema/shared.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/model/ent/schema/repo.go b/model/ent/schema/repo.go index bfe13025..02823580 100644 --- a/model/ent/schema/repo.go +++ b/model/ent/schema/repo.go @@ -34,7 +34,7 @@ func (Repo) Fields() []ent.Field { UpdateDefault(nowUTC), // Denormalization to sort with deployment. field.Time("latest_deployed_at"). - Default(nowUTC). + Default(zeroTime). Optional(), // The 'owner_id' field is the ID who activated the repository. field.Int64("owner_id"). diff --git a/model/ent/schema/shared.go b/model/ent/schema/shared.go index f2b765c3..1f723b4c 100644 --- a/model/ent/schema/shared.go +++ b/model/ent/schema/shared.go @@ -19,3 +19,7 @@ func generateHash() string { func nowUTC() time.Time { return time.Now().UTC() } + +func zeroTime() time.Time { + return time.Time{} +}