From 3478c703530ed8f5cbbb5218c7fed90081e64ff8 Mon Sep 17 00:00:00 2001 From: Douglas-Lee Date: Tue, 8 Jul 2025 11:25:38 +0800 Subject: [PATCH 1/3] fix(db): `created_at` and `updated_at` are not accurately reflect the actual time when the database time zone was not UTC. --- test/admin/endpoints_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/admin/endpoints_test.go b/test/admin/endpoints_test.go index 4fffc0cb..8f615097 100644 --- a/test/admin/endpoints_test.go +++ b/test/admin/endpoints_test.go @@ -12,6 +12,7 @@ import ( "github.com/webhookx-io/webhookx/db/entities" "github.com/webhookx-io/webhookx/test/helper" "github.com/webhookx-io/webhookx/utils" + "time" ) var _ = Describe("/endpoints", Ordered, func() { @@ -39,6 +40,7 @@ var _ = Describe("/endpoints", Ordered, func() { Context("POST", func() { It("creates an endpoint", func() { + now := time.Now() resp, err := adminClient.R(). SetBody(map[string]interface{}{ "request": map[string]interface{}{ @@ -66,6 +68,10 @@ var _ = Describe("/endpoints", Ordered, func() { e, err := db.Endpoints.Get(context.TODO(), result.ID) assert.Nil(GinkgoT(), err) assert.NotNil(GinkgoT(), e) + + assert.True(GinkgoT(), now.Unix() < e.UpdatedAt.Unix()) + assert.True(GinkgoT(), now.Unix() < e.UpdatedAt.Unix()) + assert.Equal(GinkgoT(), e.CreatedAt.Unix(), e.UpdatedAt.Unix()) }) Context("errors", func() { From 3e3b093f97d98d73b24fb7db6d8be934733fdf7e Mon Sep 17 00:00:00 2001 From: Douglas-Lee Date: Tue, 8 Jul 2025 11:32:02 +0800 Subject: [PATCH 2/3] add migration sqls --- db/migrations/9_timestamp.down.sql | 20 ++++++++++++++++++++ db/migrations/9_timestamp.up.sql | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 db/migrations/9_timestamp.down.sql create mode 100644 db/migrations/9_timestamp.up.sql diff --git a/db/migrations/9_timestamp.down.sql b/db/migrations/9_timestamp.down.sql new file mode 100644 index 00000000..fca4f4f4 --- /dev/null +++ b/db/migrations/9_timestamp.down.sql @@ -0,0 +1,20 @@ +ALTER TABLE IF EXISTS ONLY "workspaces" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; +ALTER TABLE IF EXISTS ONLY "workspaces" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; + +ALTER TABLE IF EXISTS ONLY "sources" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; +ALTER TABLE IF EXISTS ONLY "sources" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; + +ALTER TABLE IF EXISTS ONLY "plugins" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; +ALTER TABLE IF EXISTS ONLY "plugins" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; + +ALTER TABLE IF EXISTS ONLY "events" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; +ALTER TABLE IF EXISTS ONLY "events" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; + +ALTER TABLE IF EXISTS ONLY "endpoints" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; +ALTER TABLE IF EXISTS ONLY "endpoints" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; + +ALTER TABLE IF EXISTS ONLY "attempts" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; +ALTER TABLE IF EXISTS ONLY "attempts" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; + +ALTER TABLE IF EXISTS ONLY "attempt_details" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; +ALTER TABLE IF EXISTS ONLY "attempt_details" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC'; diff --git a/db/migrations/9_timestamp.up.sql b/db/migrations/9_timestamp.up.sql new file mode 100644 index 00000000..e407786a --- /dev/null +++ b/db/migrations/9_timestamp.up.sql @@ -0,0 +1,20 @@ +ALTER TABLE IF EXISTS ONLY "workspaces" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE IF EXISTS ONLY "workspaces" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3); + +ALTER TABLE IF EXISTS ONLY "sources" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE IF EXISTS ONLY "sources" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3); + +ALTER TABLE IF EXISTS ONLY "plugins" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE IF EXISTS ONLY "plugins" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3); + +ALTER TABLE IF EXISTS ONLY "events" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE IF EXISTS ONLY "events" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3); + +ALTER TABLE IF EXISTS ONLY "endpoints" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE IF EXISTS ONLY "endpoints" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3); + +ALTER TABLE IF EXISTS ONLY "attempts" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE IF EXISTS ONLY "attempts" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3); + +ALTER TABLE IF EXISTS ONLY "attempt_details" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3); +ALTER TABLE IF EXISTS ONLY "attempt_details" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3); From 5550668cd8f874610422e865984bcf7e85f02e72 Mon Sep 17 00:00:00 2001 From: Douglas-Lee Date: Tue, 8 Jul 2025 11:37:55 +0800 Subject: [PATCH 3/3] update test --- test/admin/endpoints_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/admin/endpoints_test.go b/test/admin/endpoints_test.go index 8f615097..67af0757 100644 --- a/test/admin/endpoints_test.go +++ b/test/admin/endpoints_test.go @@ -69,9 +69,9 @@ var _ = Describe("/endpoints", Ordered, func() { assert.Nil(GinkgoT(), err) assert.NotNil(GinkgoT(), e) - assert.True(GinkgoT(), now.Unix() < e.UpdatedAt.Unix()) - assert.True(GinkgoT(), now.Unix() < e.UpdatedAt.Unix()) - assert.Equal(GinkgoT(), e.CreatedAt.Unix(), e.UpdatedAt.Unix()) + assert.True(GinkgoT(), now.UnixMilli() <= e.UpdatedAt.UnixMilli()) + assert.True(GinkgoT(), now.UnixMilli() <= e.UpdatedAt.UnixMilli()) + assert.Equal(GinkgoT(), e.CreatedAt, e.UpdatedAt) }) Context("errors", func() {