Skip to content

Commit a30c901

Browse files
authored
fix(entities): created_at and updated_at didn't accurately reflect the actual time when database time zone was not UTC (#201)
1 parent 8b05d6a commit a30c901

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

db/migrations/9_timestamp.down.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
ALTER TABLE IF EXISTS ONLY "workspaces" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
2+
ALTER TABLE IF EXISTS ONLY "workspaces" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
3+
4+
ALTER TABLE IF EXISTS ONLY "sources" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
5+
ALTER TABLE IF EXISTS ONLY "sources" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
6+
7+
ALTER TABLE IF EXISTS ONLY "plugins" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
8+
ALTER TABLE IF EXISTS ONLY "plugins" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
9+
10+
ALTER TABLE IF EXISTS ONLY "events" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
11+
ALTER TABLE IF EXISTS ONLY "events" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
12+
13+
ALTER TABLE IF EXISTS ONLY "endpoints" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
14+
ALTER TABLE IF EXISTS ONLY "endpoints" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
15+
16+
ALTER TABLE IF EXISTS ONLY "attempts" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
17+
ALTER TABLE IF EXISTS ONLY "attempts" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
18+
19+
ALTER TABLE IF EXISTS ONLY "attempt_details" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';
20+
ALTER TABLE IF EXISTS ONLY "attempt_details" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3) AT TIME ZONE 'UTC';

db/migrations/9_timestamp.up.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
ALTER TABLE IF EXISTS ONLY "workspaces" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3);
2+
ALTER TABLE IF EXISTS ONLY "workspaces" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3);
3+
4+
ALTER TABLE IF EXISTS ONLY "sources" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3);
5+
ALTER TABLE IF EXISTS ONLY "sources" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3);
6+
7+
ALTER TABLE IF EXISTS ONLY "plugins" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3);
8+
ALTER TABLE IF EXISTS ONLY "plugins" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3);
9+
10+
ALTER TABLE IF EXISTS ONLY "events" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3);
11+
ALTER TABLE IF EXISTS ONLY "events" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3);
12+
13+
ALTER TABLE IF EXISTS ONLY "endpoints" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3);
14+
ALTER TABLE IF EXISTS ONLY "endpoints" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3);
15+
16+
ALTER TABLE IF EXISTS ONLY "attempts" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3);
17+
ALTER TABLE IF EXISTS ONLY "attempts" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3);
18+
19+
ALTER TABLE IF EXISTS ONLY "attempt_details" ALTER COLUMN "created_at" SET DEFAULT CURRENT_TIMESTAMP(3);
20+
ALTER TABLE IF EXISTS ONLY "attempt_details" ALTER COLUMN "updated_at" SET DEFAULT CURRENT_TIMESTAMP(3);

test/admin/endpoints_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/webhookx-io/webhookx/db/entities"
1313
"github.com/webhookx-io/webhookx/test/helper"
1414
"github.com/webhookx-io/webhookx/utils"
15+
"time"
1516
)
1617

1718
var _ = Describe("/endpoints", Ordered, func() {
@@ -39,6 +40,7 @@ var _ = Describe("/endpoints", Ordered, func() {
3940

4041
Context("POST", func() {
4142
It("creates an endpoint", func() {
43+
now := time.Now()
4244
resp, err := adminClient.R().
4345
SetBody(map[string]interface{}{
4446
"request": map[string]interface{}{
@@ -66,6 +68,10 @@ var _ = Describe("/endpoints", Ordered, func() {
6668
e, err := db.Endpoints.Get(context.TODO(), result.ID)
6769
assert.Nil(GinkgoT(), err)
6870
assert.NotNil(GinkgoT(), e)
71+
72+
assert.True(GinkgoT(), now.UnixMilli() <= e.UpdatedAt.UnixMilli())
73+
assert.True(GinkgoT(), now.UnixMilli() <= e.UpdatedAt.UnixMilli())
74+
assert.Equal(GinkgoT(), e.CreatedAt, e.UpdatedAt)
6975
})
7076

7177
Context("errors", func() {

0 commit comments

Comments
 (0)