fix: 使用 UUIDv7 生成 UUID,设置 create_time 和 update_time 为东八区时区

This commit is contained in:
fish
2026-03-28 20:16:40 +08:00
parent 4ff974439f
commit c57cc89dfe
2 changed files with 12 additions and 9 deletions

View File

@@ -41,7 +41,7 @@ func (r *UserRepository) Register(req *domain.RegisterRequest) (*domain.Register
} }
// 创建用户 // 创建用户
userID := uuid.New() userID := uuid.NewV7()
now := time.Now() now := time.Now()
userQuery := "INSERT INTO user_main (id, deleted, create_time, update_time) VALUES ($1, $2, $3, $4)" userQuery := "INSERT INTO user_main (id, deleted, create_time, update_time) VALUES ($1, $2, $3, $4)"
@@ -51,7 +51,7 @@ func (r *UserRepository) Register(req *domain.RegisterRequest) (*domain.Register
} }
// 创建登录账号 // 创建登录账号
accountID := uuid.New() accountID := uuid.NewV7()
accountQuery := "INSERT INTO user_login_account (id, user_id, account, deleted, create_time, update_time) VALUES ($1, $2, $3, $4, $5, $6)" accountQuery := "INSERT INTO user_login_account (id, user_id, account, deleted, create_time, update_time) VALUES ($1, $2, $3, $4, $5, $6)"
if _, err := tx.Exec(accountQuery, accountID, userID, req.Account, false, now, now); err != nil { if _, err := tx.Exec(accountQuery, accountID, userID, req.Account, false, now, now); err != nil {
tx.Rollback() tx.Rollback()
@@ -66,7 +66,7 @@ func (r *UserRepository) Register(req *domain.RegisterRequest) (*domain.Register
} }
// 创建密码记录 // 创建密码记录
passwordID := uuid.New() passwordID := uuid.NewV7()
passwordQuery := "INSERT INTO user_login_password (id, user_id, password, deleted, create_time, update_time) VALUES ($1, $2, $3, $4, $5, $6)" passwordQuery := "INSERT INTO user_login_password (id, user_id, password, deleted, create_time, update_time) VALUES ($1, $2, $3, $4, $5, $6)"
if _, err := tx.Exec(passwordQuery, passwordID, userID, string(hashedPassword), false, now, now); err != nil { if _, err := tx.Exec(passwordQuery, passwordID, userID, string(hashedPassword), false, now, now); err != nil {
tx.Rollback() tx.Rollback()

View File

@@ -1,9 +1,12 @@
-- 设置时区为东八区
SET TIMEZONE = 'Asia/Shanghai';
-- 创建 user_main 表 -- 创建 user_main 表
CREATE TABLE IF NOT EXISTS user_main ( CREATE TABLE IF NOT EXISTS user_main (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
deleted BOOLEAN DEFAULT false, deleted BOOLEAN DEFAULT false,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, create_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP update_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
); );
-- 创建 user_login_account 表 -- 创建 user_login_account 表
@@ -12,8 +15,8 @@ CREATE TABLE IF NOT EXISTS user_login_account (
user_id UUID NOT NULL REFERENCES user_main(id), user_id UUID NOT NULL REFERENCES user_main(id),
account VARCHAR(255) NOT NULL, account VARCHAR(255) NOT NULL,
deleted BOOLEAN DEFAULT false, deleted BOOLEAN DEFAULT false,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, create_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, update_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
UNIQUE(account) UNIQUE(account)
); );
@@ -23,8 +26,8 @@ CREATE TABLE IF NOT EXISTS user_login_password (
user_id UUID NOT NULL REFERENCES user_main(id), user_id UUID NOT NULL REFERENCES user_main(id),
password VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL,
deleted BOOLEAN DEFAULT false, deleted BOOLEAN DEFAULT false,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, create_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP update_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
); );
-- 创建索引 -- 创建索引