chore: fix timestamp type in postgres

pull/2601/head
Steven 2 years ago
parent c50f4f4cb4
commit d275713aff

@ -2,7 +2,6 @@ package postgres
import ( import (
"context" "context"
"time"
"github.com/Masterminds/squirrel" "github.com/Masterminds/squirrel"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -35,7 +34,7 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store
if create.CreatedTs != 0 { if create.CreatedTs != 0 {
qb = qb.Columns("created_ts") qb = qb.Columns("created_ts")
values = append(values, squirrel.Expr("TO_TIMESTAMP(?)", create.CreatedTs)) values = append(values, create.CreatedTs)
} }
qb = qb.Values(values...).Suffix("RETURNING id") qb = qb.Values(values...).Suffix("RETURNING id")
@ -60,7 +59,7 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store
} }
func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*store.Activity, error) { func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*store.Activity, error) {
qb := squirrel.Select("id", "creator_id", "type", "level", "payload", "created_ts"). qb := squirrel.Select("id", "created_ts", "creator_id", "type", "level", "payload").
From("activity"). From("activity").
Where("1 = 1"). Where("1 = 1").
PlaceholderFormat(squirrel.Dollar) PlaceholderFormat(squirrel.Dollar)
@ -87,20 +86,17 @@ func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*s
for rows.Next() { for rows.Next() {
activity := &store.Activity{} activity := &store.Activity{}
var payloadBytes []byte var payloadBytes []byte
createdTsPlaceHolder := time.Time{}
if err := rows.Scan( if err := rows.Scan(
&activity.ID, &activity.ID,
&activity.CreatedTs,
&activity.CreatorID, &activity.CreatorID,
&activity.Type, &activity.Type,
&activity.Level, &activity.Level,
&payloadBytes, &payloadBytes,
&createdTsPlaceHolder,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
activity.CreatedTs = createdTsPlaceHolder.Unix()
payload := &storepb.ActivityPayload{} payload := &storepb.ActivityPayload{}
if err := protojson.Unmarshal(payloadBytes, payload); err != nil { if err := protojson.Unmarshal(payloadBytes, payload); err != nil {
return nil, err return nil, err

@ -2,7 +2,6 @@ package postgres
import ( import (
"context" "context"
"time"
"github.com/Masterminds/squirrel" "github.com/Masterminds/squirrel"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -76,13 +75,10 @@ func (d *DB) ListInboxes(ctx context.Context, find *store.FindInbox) ([]*store.I
for rows.Next() { for rows.Next() {
inbox := &store.Inbox{} inbox := &store.Inbox{}
var messageBytes []byte var messageBytes []byte
createdTsPlaceHolder := time.Time{} if err := rows.Scan(&inbox.ID, &inbox.CreatedTs, &inbox.SenderID, &inbox.ReceiverID, &inbox.Status, &messageBytes); err != nil {
if err := rows.Scan(&inbox.ID, &createdTsPlaceHolder, &inbox.SenderID, &inbox.ReceiverID, &inbox.Status, &messageBytes); err != nil {
return nil, err return nil, err
} }
inbox.CreatedTs = createdTsPlaceHolder.Unix()
message := &storepb.InboxMessage{} message := &storepb.InboxMessage{}
if err := protojsonUnmarshaler.Unmarshal(messageBytes, message); err != nil { if err := protojsonUnmarshaler.Unmarshal(messageBytes, message); err != nil {
return nil, err return nil, err

@ -3,7 +3,6 @@ package postgres
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/binary"
"fmt" "fmt"
"strings" "strings"
@ -30,12 +29,12 @@ func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, e
if create.CreatedTs != 0 { if create.CreatedTs != 0 {
builder = builder.Columns("created_ts") builder = builder.Columns("created_ts")
values = append(values, squirrel.Expr("to_timestamp(?)", create.CreatedTs)) values = append(values, create.CreatedTs)
} }
if create.UpdatedTs != 0 { if create.UpdatedTs != 0 {
builder = builder.Columns("updated_ts") builder = builder.Columns("updated_ts")
values = append(values, squirrel.Expr("to_timestamp(?)", create.UpdatedTs)) values = append(values, create.UpdatedTs)
} }
if create.RowStatus != "" { if create.RowStatus != "" {
@ -78,8 +77,8 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
builder := squirrel.Select( builder := squirrel.Select(
"memo.id AS id", "memo.id AS id",
"memo.creator_id AS creator_id", "memo.creator_id AS creator_id",
"EXTRACT(EPOCH FROM memo.created_ts) AS created_ts", "memo.created_ts AS created_ts",
"EXTRACT(EPOCH FROM memo.updated_ts) AS updated_ts", "memo.updated_ts AS updated_ts",
"memo.row_status AS row_status", "memo.row_status AS row_status",
"memo.content AS content", "memo.content AS content",
"memo.visibility AS visibility", "memo.visibility AS visibility",
@ -101,10 +100,10 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
builder = builder.Where("memo.row_status = ?", *v) builder = builder.Where("memo.row_status = ?", *v)
} }
if v := find.CreatedTsBefore; v != nil { if v := find.CreatedTsBefore; v != nil {
builder = builder.Where("EXTRACT(EPOCH FROM memo.created_ts) < ?", *v) builder = builder.Where("memo.created_ts < ?", *v)
} }
if v := find.CreatedTsAfter; v != nil { if v := find.CreatedTsAfter; v != nil {
builder = builder.Where("EXTRACT(EPOCH FROM memo.created_ts) > ?", *v) builder = builder.Where("memo.created_ts > ?", *v)
} }
if v := find.Pinned; v != nil { if v := find.Pinned; v != nil {
builder = builder.Where("memo_organizer.pinned = 1") builder = builder.Where("memo_organizer.pinned = 1")
@ -158,14 +157,13 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
// Process the result set // Process the result set
list := make([]*store.Memo, 0) list := make([]*store.Memo, 0)
updatedTsPlaceHolder, createdTsPlaceHolder := make([]uint8, 8), make([]uint8, 8)
for rows.Next() { for rows.Next() {
var memo store.Memo var memo store.Memo
if err := rows.Scan( if err := rows.Scan(
&memo.ID, &memo.ID,
&memo.CreatorID, &memo.CreatorID,
&createdTsPlaceHolder, &memo.CreatedTs,
&updatedTsPlaceHolder, &memo.UpdatedTs,
&memo.RowStatus, &memo.RowStatus,
&memo.Content, &memo.Content,
&memo.Visibility, &memo.Visibility,
@ -174,10 +172,6 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
return nil, err return nil, err
} }
// Convert the timestamps from Postgres to Go
memo.CreatedTs = int64(binary.BigEndian.Uint64(createdTsPlaceHolder))
memo.UpdatedTs = int64(binary.BigEndian.Uint64(updatedTsPlaceHolder))
list = append(list, &memo) list = append(list, &memo)
} }
@ -209,10 +203,10 @@ func (d *DB) UpdateMemo(ctx context.Context, update *store.UpdateMemo) error {
// Conditionally add set clauses // Conditionally add set clauses
if v := update.CreatedTs; v != nil { if v := update.CreatedTs; v != nil {
builder = builder.Set("created_ts", squirrel.Expr("to_timestamp(?)", *v)) builder = builder.Set("created_ts", *v)
} }
if v := update.UpdatedTs; v != nil { if v := update.UpdatedTs; v != nil {
builder = builder.Set("updated_ts", squirrel.Expr("to_timestamp(?)", *v)) builder = builder.Set("updated_ts", *v)
} }
if v := update.RowStatus; v != nil { if v := update.RowStatus; v != nil {
builder = builder.Set("row_status", *v) builder = builder.Set("row_status", *v)

@ -17,7 +17,7 @@ DROP TABLE IF EXISTS webhook CASCADE;
-- migration_history -- migration_history
CREATE TABLE migration_history ( CREATE TABLE migration_history (
version TEXT NOT NULL PRIMARY KEY, version TEXT NOT NULL PRIMARY KEY,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW())
); );
-- system_setting -- system_setting
@ -30,8 +30,8 @@ CREATE TABLE system_setting (
-- user -- user
CREATE TABLE "user" ( CREATE TABLE "user" (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
row_status TEXT NOT NULL DEFAULT 'NORMAL', row_status TEXT NOT NULL DEFAULT 'NORMAL',
username TEXT NOT NULL UNIQUE, username TEXT NOT NULL UNIQUE,
role TEXT NOT NULL DEFAULT 'USER', role TEXT NOT NULL DEFAULT 'USER',
@ -53,8 +53,8 @@ CREATE TABLE user_setting (
CREATE TABLE memo ( CREATE TABLE memo (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
row_status TEXT NOT NULL DEFAULT 'NORMAL', row_status TEXT NOT NULL DEFAULT 'NORMAL',
content TEXT NOT NULL, content TEXT NOT NULL,
visibility TEXT NOT NULL DEFAULT 'PRIVATE' visibility TEXT NOT NULL DEFAULT 'PRIVATE'
@ -80,8 +80,8 @@ CREATE TABLE memo_relation (
CREATE TABLE resource ( CREATE TABLE resource (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
filename TEXT NOT NULL, filename TEXT NOT NULL,
blob BYTEA, blob BYTEA,
external_link TEXT NOT NULL, external_link TEXT NOT NULL,
@ -102,7 +102,7 @@ CREATE TABLE tag (
CREATE TABLE activity ( CREATE TABLE activity (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
type TEXT NOT NULL DEFAULT '', type TEXT NOT NULL DEFAULT '',
level TEXT NOT NULL DEFAULT 'INFO', level TEXT NOT NULL DEFAULT 'INFO',
payload JSONB NOT NULL DEFAULT '{}' payload JSONB NOT NULL DEFAULT '{}'
@ -128,7 +128,7 @@ CREATE TABLE idp (
-- inbox -- inbox
CREATE TABLE inbox ( CREATE TABLE inbox (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
sender_id INTEGER NOT NULL, sender_id INTEGER NOT NULL,
receiver_id INTEGER NOT NULL, receiver_id INTEGER NOT NULL,
status TEXT NOT NULL, status TEXT NOT NULL,
@ -138,8 +138,8 @@ CREATE TABLE inbox (
-- webhook -- webhook
CREATE TABLE webhook ( CREATE TABLE webhook (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
row_status TEXT NOT NULL DEFAULT 'NORMAL', row_status TEXT NOT NULL DEFAULT 'NORMAL',
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,

@ -17,7 +17,7 @@ DROP TABLE IF EXISTS webhook CASCADE;
-- migration_history -- migration_history
CREATE TABLE migration_history ( CREATE TABLE migration_history (
version TEXT NOT NULL PRIMARY KEY, version TEXT NOT NULL PRIMARY KEY,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW())
); );
-- system_setting -- system_setting
@ -30,8 +30,8 @@ CREATE TABLE system_setting (
-- user -- user
CREATE TABLE "user" ( CREATE TABLE "user" (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
row_status TEXT NOT NULL DEFAULT 'NORMAL', row_status TEXT NOT NULL DEFAULT 'NORMAL',
username TEXT NOT NULL UNIQUE, username TEXT NOT NULL UNIQUE,
role TEXT NOT NULL DEFAULT 'USER', role TEXT NOT NULL DEFAULT 'USER',
@ -53,8 +53,8 @@ CREATE TABLE user_setting (
CREATE TABLE memo ( CREATE TABLE memo (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
row_status TEXT NOT NULL DEFAULT 'NORMAL', row_status TEXT NOT NULL DEFAULT 'NORMAL',
content TEXT NOT NULL, content TEXT NOT NULL,
visibility TEXT NOT NULL DEFAULT 'PRIVATE' visibility TEXT NOT NULL DEFAULT 'PRIVATE'
@ -80,8 +80,8 @@ CREATE TABLE memo_relation (
CREATE TABLE resource ( CREATE TABLE resource (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
filename TEXT NOT NULL, filename TEXT NOT NULL,
blob BYTEA, blob BYTEA,
external_link TEXT NOT NULL, external_link TEXT NOT NULL,
@ -102,7 +102,7 @@ CREATE TABLE tag (
CREATE TABLE activity ( CREATE TABLE activity (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
type TEXT NOT NULL DEFAULT '', type TEXT NOT NULL DEFAULT '',
level TEXT NOT NULL DEFAULT 'INFO', level TEXT NOT NULL DEFAULT 'INFO',
payload JSONB NOT NULL DEFAULT '{}' payload JSONB NOT NULL DEFAULT '{}'
@ -128,7 +128,7 @@ CREATE TABLE idp (
-- inbox -- inbox
CREATE TABLE inbox ( CREATE TABLE inbox (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
sender_id INTEGER NOT NULL, sender_id INTEGER NOT NULL,
receiver_id INTEGER NOT NULL, receiver_id INTEGER NOT NULL,
status TEXT NOT NULL, status TEXT NOT NULL,
@ -138,8 +138,8 @@ CREATE TABLE inbox (
-- webhook -- webhook
CREATE TABLE webhook ( CREATE TABLE webhook (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
created_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, created_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
updated_ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_ts BIGINT NOT NULL DEFAULT EXTRACT(EPOCH FROM NOW()),
row_status TEXT NOT NULL DEFAULT 'NORMAL', row_status TEXT NOT NULL DEFAULT 'NORMAL',
creator_id INTEGER NOT NULL, creator_id INTEGER NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,

@ -2,7 +2,6 @@ package postgres
import ( import (
"context" "context"
"time"
"github.com/Masterminds/squirrel" "github.com/Masterminds/squirrel"
@ -28,11 +27,9 @@ func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *store.FindMigratio
list := make([]*store.MigrationHistory, 0) list := make([]*store.MigrationHistory, 0)
for rows.Next() { for rows.Next() {
var migrationHistory store.MigrationHistory var migrationHistory store.MigrationHistory
var createdTs time.Time if err := rows.Scan(&migrationHistory.Version, &migrationHistory.CreatedTs); err != nil {
if err := rows.Scan(&migrationHistory.Version, &createdTs); err != nil {
return nil, err return nil, err
} }
migrationHistory.CreatedTs = createdTs.UnixNano()
list = append(list, &migrationHistory) list = append(list, &migrationHistory)
} }
@ -60,7 +57,6 @@ func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMig
} }
var migrationHistory store.MigrationHistory var migrationHistory store.MigrationHistory
var createdTs time.Time
query, args, err = squirrel.Select("version", "created_ts"). query, args, err = squirrel.Select("version", "created_ts").
From("migration_history"). From("migration_history").
Where(squirrel.Eq{"version": upsert.Version}). Where(squirrel.Eq{"version": upsert.Version}).
@ -70,10 +66,9 @@ func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *store.UpsertMig
return nil, err return nil, err
} }
if err := d.db.QueryRowContext(ctx, query, args...).Scan(&migrationHistory.Version, &createdTs); err != nil { if err := d.db.QueryRowContext(ctx, query, args...).Scan(&migrationHistory.Version, &migrationHistory.CreatedTs); err != nil {
return nil, err return nil, err
} }
migrationHistory.CreatedTs = createdTs.UnixNano()
return &migrationHistory, nil return &migrationHistory, nil
} }

@ -23,12 +23,12 @@ func (d *DB) CreateResource(ctx context.Context, create *store.Resource) (*store
if create.CreatedTs != 0 { if create.CreatedTs != 0 {
qb = qb.Columns("created_ts") qb = qb.Columns("created_ts")
values = append(values, time.Unix(0, create.CreatedTs)) values = append(values, create.CreatedTs)
} }
if create.UpdatedTs != 0 { if create.UpdatedTs != 0 {
qb = qb.Columns("updated_ts") qb = qb.Columns("updated_ts")
values = append(values, time.Unix(0, create.UpdatedTs)) values = append(values, create.UpdatedTs)
} }
if create.MemoID != nil { if create.MemoID != nil {
@ -105,7 +105,6 @@ func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*st
for rows.Next() { for rows.Next() {
resource := store.Resource{} resource := store.Resource{}
var memoID sql.NullInt32 var memoID sql.NullInt32
var createdTs, updatedTs time.Time
dests := []any{ dests := []any{
&resource.ID, &resource.ID,
&resource.Filename, &resource.Filename,
@ -113,8 +112,8 @@ func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*st
&resource.Type, &resource.Type,
&resource.Size, &resource.Size,
&resource.CreatorID, &resource.CreatorID,
&createdTs, &resource.CreatedTs,
&updatedTs, &resource.UpdatedTs,
&resource.InternalPath, &resource.InternalPath,
&memoID, &memoID,
} }
@ -125,9 +124,6 @@ func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*st
return nil, err return nil, err
} }
resource.CreatedTs = createdTs.UnixNano()
resource.UpdatedTs = updatedTs.UnixNano()
if memoID.Valid { if memoID.Valid {
resource.MemoID = &memoID.Int32 resource.MemoID = &memoID.Int32
} }

@ -11,7 +11,7 @@ import (
func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) { func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
// Start building the insert statement // Start building the insert statement
builder := squirrel.Insert("\"user\"").PlaceholderFormat(squirrel.Dollar) builder := squirrel.Insert(`"user"`).PlaceholderFormat(squirrel.Dollar)
columns := []string{"username", "role", "email", "nickname", "password_hash", "avatar_url"} columns := []string{"username", "role", "email", "nickname", "password_hash", "avatar_url"}
builder = builder.Columns(columns...) builder = builder.Columns(columns...)
@ -25,12 +25,12 @@ func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, e
if create.CreatedTs != 0 { if create.CreatedTs != 0 {
builder = builder.Columns("created_ts") builder = builder.Columns("created_ts")
values = append(values, squirrel.Expr("TO_TIMESTAMP(?)", create.CreatedTs)) values = append(values, create.CreatedTs)
} }
if create.UpdatedTs != 0 { if create.UpdatedTs != 0 {
builder = builder.Columns("updated_ts") builder = builder.Columns("updated_ts")
values = append(values, squirrel.Expr("TO_TIMESTAMP(?)", create.UpdatedTs)) values = append(values, create.UpdatedTs)
} }
if create.ID != 0 { if create.ID != 0 {
@ -66,7 +66,7 @@ func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, e
func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) { func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) {
// Start building the update statement // Start building the update statement
builder := squirrel.Update("\"user\"").PlaceholderFormat(squirrel.Dollar) builder := squirrel.Update(`"user"`).PlaceholderFormat(squirrel.Dollar)
// Conditionally add set clauses // Conditionally add set clauses
if v := update.UpdatedTs; v != nil { if v := update.UpdatedTs; v != nil {
@ -115,9 +115,8 @@ func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.U
func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) { func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) {
// Start building the SELECT statement // Start building the SELECT statement
builder := squirrel.Select("id", "username", "role", "email", "nickname", "password_hash", "avatar_url", builder := squirrel.Select("id", "username", "role", "email", "nickname", "password_hash", "avatar_url", "created_ts", "updated_ts", "row_status").
"FLOOR(EXTRACT(EPOCH FROM created_ts)) AS created_ts", "FLOOR(EXTRACT(EPOCH FROM updated_ts)) AS updated_ts", "row_status"). From(`"user"`).
From("\"user\"").
PlaceholderFormat(squirrel.Dollar) PlaceholderFormat(squirrel.Dollar)
// 1 = 1 is often used as a no-op in SQL, ensuring there's always a WHERE clause // 1 = 1 is often used as a no-op in SQL, ensuring there's always a WHERE clause
@ -196,7 +195,7 @@ func (d *DB) GetUser(ctx context.Context, find *store.FindUser) (*store.User, er
func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) error { func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) error {
// Start building the DELETE statement // Start building the DELETE statement
builder := squirrel.Delete("\"user\""). builder := squirrel.Delete(`"user"`).
PlaceholderFormat(squirrel.Dollar). PlaceholderFormat(squirrel.Dollar).
Where(squirrel.Eq{"id": delete.ID}) Where(squirrel.Eq{"id": delete.ID})

@ -2,7 +2,6 @@ package postgres
import ( import (
"context" "context"
"time"
"github.com/Masterminds/squirrel" "github.com/Masterminds/squirrel"
@ -63,12 +62,10 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
for rows.Next() { for rows.Next() {
webhook := &storepb.Webhook{} webhook := &storepb.Webhook{}
var rowStatus string var rowStatus string
var createdTs, updatedTs time.Time
if err := rows.Scan( if err := rows.Scan(
&webhook.Id, &webhook.Id,
&createdTs, &webhook.CreatedTs,
&updatedTs, &webhook.UpdatedTs,
&rowStatus, &rowStatus,
&webhook.CreatorId, &webhook.CreatorId,
&webhook.Name, &webhook.Name,
@ -77,8 +74,6 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
return nil, err return nil, err
} }
webhook.CreatedTs = createdTs.UnixNano()
webhook.UpdatedTs = updatedTs.UnixNano()
webhook.RowStatus = storepb.RowStatus(storepb.RowStatus_value[rowStatus]) webhook.RowStatus = storepb.RowStatus(storepb.RowStatus_value[rowStatus])
list = append(list, webhook) list = append(list, webhook)

Loading…
Cancel
Save