框架表改名 w_{name}

This commit is contained in:
ryan
2026-06-11 09:27:15 +08:00
parent 786fe71778
commit 312bc7d4d5
17 changed files with 245 additions and 21 deletions
+5 -5
View File
@@ -219,7 +219,7 @@ func buildAccessLogFilters(ctx context.Context, c *gin.Context) ([]string, []int
func fetchAccessLogDetails(ctx context.Context, whereClause string, args []interface{}, pageSize int, offset int) ([]accessLogItem, error) {
dataQuery := fmt.Sprintf(`
SELECT id, user_id, path, method, ip, user_agent, headers, status, latency, created_at
FROM user_access_logs
FROM w_user_access_logs
%s
ORDER BY created_at DESC, id DESC
LIMIT ? OFFSET ?
@@ -325,7 +325,7 @@ func GetAccessLogs(c *gin.Context) {
// 4. 查询日志总数
var total uint64
countQuery := fmt.Sprintf("SELECT count() FROM user_access_logs %s", whereClause)
countQuery := fmt.Sprintf("SELECT count() FROM w_user_access_logs %s", whereClause)
if err := db.ChConn.QueryRow(c.Request.Context(), countQuery, args...).Scan(&total); err != nil {
c.JSON(http.StatusInternalServerError, util.Err("查询 ClickHouse 日志统计失败: "+err.Error()))
return
@@ -412,7 +412,7 @@ func GetLogsAnalytics(c *gin.Context) {
func queryAccessTrend(ctx context.Context, startTime time.Time) []trendItem {
trendRows, err := db.ChConn.Query(ctx, `
SELECT toDate(created_at) as date, count() as count
FROM user_access_logs
FROM w_user_access_logs
WHERE created_at >= ?
GROUP BY date
ORDER BY date ASC
@@ -451,7 +451,7 @@ func queryAccessTrend(ctx context.Context, startTime time.Time) []trendItem {
func queryBrowserDistribution(ctx context.Context, startTime time.Time) []browserItem {
uaRows, err := db.ChConn.Query(ctx, `
SELECT user_agent, count() as count
FROM user_access_logs
FROM w_user_access_logs
WHERE created_at >= ?
GROUP BY user_agent
`, startTime)
@@ -486,7 +486,7 @@ func queryBrowserDistribution(ctx context.Context, startTime time.Time) []browse
func queryTopActiveUsers(ctx context.Context, startTime time.Time) []topUserItem {
userRows, err := db.ChConn.Query(ctx, `
SELECT user_id, count() as count
FROM user_access_logs
FROM w_user_access_logs
WHERE created_at >= ? AND user_id > 0
GROUP BY user_id
ORDER BY count DESC
+5 -5
View File
@@ -107,7 +107,7 @@ func ListUsers(c *gin.Context) {
var users []user
var total int64
query := db.DB(c.Request.Context()).Table("users")
query := db.DB(c.Request.Context()).Model(&model.User{})
username := strings.TrimSpace(req.Username)
@@ -217,7 +217,7 @@ func UpdateUserStatus(c *gin.Context) {
IsAdmin bool `gorm:"column:is_admin"`
}
if err := db.DB(c.Request.Context()).
Table("users").
Model(&model.User{}).
Select("id, is_admin").
Where("id = ?", id).
First(&targetUser).Error; err != nil {
@@ -235,7 +235,7 @@ func UpdateUserStatus(c *gin.Context) {
}
if err := db.DB(c.Request.Context()).
Table("users").
Model(&model.User{}).
Where("id = ?", id).
Update("is_active", req.IsActive).Error; err != nil {
c.JSON(http.StatusInternalServerError, util.Err(updateUserFailed))
@@ -276,7 +276,7 @@ func DeleteUser(c *gin.Context) {
IsAdmin bool `gorm:"column:is_admin"`
}
if err := db.DB(c.Request.Context()).
Table("users").
Model(&model.User{}).
Select("id, is_admin").
Where("id = ?", id).
First(&targetUser).Error; err != nil {
@@ -354,7 +354,7 @@ func CreateUser(c *gin.Context) {
ctx := c.Request.Context()
var count int64
if err := db.DB(ctx).Table("users").Where("username = ?", req.Username).Count(&count).Error; err != nil {
if err := db.DB(ctx).Model(&model.User{}).Where("username = ?", req.Username).Count(&count).Error; err != nil {
c.JSON(http.StatusInternalServerError, util.Err(err.Error()))
return
}
+1 -1
View File
@@ -68,7 +68,7 @@ func startBatchWorker() {
}
ctx := context.Background()
b, err := db.ChConn.PrepareBatch(ctx, "INSERT INTO user_access_logs (id, user_id, path, method, ip, user_agent, headers, status, latency, created_at)")
b, err := db.ChConn.PrepareBatch(ctx, "INSERT INTO w_user_access_logs (id, user_id, path, method, ip, user_agent, headers, status, latency, created_at)")
if err != nil {
logger.ErrorF(ctx, "[RiskControl] Prepare ClickHouse batch failed: %v", err)
batch = nil
+1 -1
View File
@@ -185,7 +185,7 @@ func TestLoginRequiresPasswordChangeForInitialPlaintextAdmin(t *testing.T) {
)
now := time.Now()
if err := dbConn.Exec(
`INSERT INTO users (id, username, password, nickname, is_active, is_admin, last_login_at, created_at, updated_at)
`INSERT INTO w_users (id, username, password, nickname, is_active, is_admin, last_login_at, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
adminID,
adminUsername,
@@ -0,0 +1,66 @@
-- +goose Up
ALTER TABLE users RENAME TO w_users;
ALTER TABLE auth_sources RENAME TO w_auth_sources;
ALTER TABLE external_accounts RENAME TO w_external_accounts;
ALTER TABLE system_configs RENAME TO w_system_configs;
ALTER TABLE uploads RENAME TO w_uploads;
ALTER TABLE access_tokens RENAME TO w_access_tokens;
ALTER TABLE task_executions RENAME TO w_task_executions;
ALTER TABLE templates RENAME TO w_templates;
ALTER TABLE schedules RENAME TO w_schedules;
-- Rename indexes for consistency
ALTER INDEX IF EXISTS idx_users_email RENAME TO idx_w_users_email;
ALTER INDEX IF EXISTS idx_users_is_active RENAME TO idx_w_users_is_active;
ALTER INDEX IF EXISTS idx_users_last_login_at RENAME TO idx_w_users_last_login_at;
ALTER INDEX IF EXISTS idx_users_created_at RENAME TO idx_w_users_created_at;
ALTER INDEX IF EXISTS idx_auth_sources_is_active RENAME TO idx_w_auth_sources_is_active;
ALTER INDEX IF EXISTS idx_external_accounts_auth_source_id RENAME TO idx_w_external_accounts_auth_source_id;
ALTER INDEX IF EXISTS idx_external_accounts_user_id RENAME TO idx_w_external_accounts_user_id;
ALTER INDEX IF EXISTS idx_external_accounts_source_external RENAME TO idx_w_external_accounts_source_external;
ALTER INDEX IF EXISTS idx_uploads_user_id RENAME TO idx_w_uploads_user_id;
ALTER INDEX IF EXISTS idx_uploads_file_path RENAME TO idx_w_uploads_file_path;
ALTER INDEX IF EXISTS idx_uploads_hash RENAME TO idx_w_uploads_hash;
ALTER INDEX IF EXISTS idx_uploads_type RENAME TO idx_w_uploads_type;
ALTER INDEX IF EXISTS idx_access_tokens_user_id RENAME TO idx_w_access_tokens_user_id;
ALTER INDEX IF EXISTS idx_task_executions_task_type RENAME TO idx_w_task_executions_task_type;
ALTER INDEX IF EXISTS idx_task_executions_status RENAME TO idx_w_task_executions_status;
ALTER INDEX IF EXISTS idx_task_executions_started_at RENAME TO idx_w_task_executions_started_at;
ALTER INDEX IF EXISTS idx_task_executions_created_at RENAME TO idx_w_task_executions_created_at;
ALTER INDEX IF EXISTS idx_templates_is_system RENAME TO idx_w_templates_is_system;
ALTER INDEX IF EXISTS idx_templates_created_at RENAME TO idx_w_templates_created_at;
ALTER INDEX IF EXISTS idx_templates_updated_at RENAME TO idx_w_templates_updated_at;
ALTER INDEX IF EXISTS idx_schedules_is_active RENAME TO idx_w_schedules_is_active;
-- +goose Down
ALTER INDEX IF EXISTS idx_w_schedules_is_active RENAME TO idx_schedules_is_active;
ALTER INDEX IF EXISTS idx_w_templates_updated_at RENAME TO idx_templates_updated_at;
ALTER INDEX IF EXISTS idx_w_templates_created_at RENAME TO idx_templates_created_at;
ALTER INDEX IF EXISTS idx_w_templates_is_system RENAME TO idx_templates_is_system;
ALTER INDEX IF EXISTS idx_w_task_executions_created_at RENAME TO idx_task_executions_created_at;
ALTER INDEX IF EXISTS idx_w_task_executions_started_at RENAME TO idx_task_executions_started_at;
ALTER INDEX IF EXISTS idx_w_task_executions_status RENAME TO idx_task_executions_status;
ALTER INDEX IF EXISTS idx_w_task_executions_task_type RENAME TO idx_task_executions_task_type;
ALTER INDEX IF EXISTS idx_w_access_tokens_user_id RENAME TO idx_access_tokens_user_id;
ALTER INDEX IF EXISTS idx_w_uploads_type RENAME TO idx_uploads_type;
ALTER INDEX IF EXISTS idx_w_uploads_hash RENAME TO idx_uploads_hash;
ALTER INDEX IF EXISTS idx_w_uploads_file_path RENAME TO idx_uploads_file_path;
ALTER INDEX IF EXISTS idx_w_uploads_user_id RENAME TO idx_uploads_user_id;
ALTER INDEX IF EXISTS idx_w_external_accounts_source_external RENAME TO idx_external_accounts_source_external;
ALTER INDEX IF EXISTS idx_w_external_accounts_user_id RENAME TO idx_external_accounts_user_id;
ALTER INDEX IF EXISTS idx_w_external_accounts_auth_source_id RENAME TO idx_external_accounts_auth_source_id;
ALTER INDEX IF EXISTS idx_w_auth_sources_is_active RENAME TO idx_auth_sources_is_active;
ALTER INDEX IF EXISTS idx_w_users_created_at RENAME TO idx_users_created_at;
ALTER INDEX IF EXISTS idx_w_users_last_login_at RENAME TO idx_users_last_login_at;
ALTER INDEX IF EXISTS idx_w_users_is_active RENAME TO idx_users_is_active;
ALTER INDEX IF EXISTS idx_w_users_email RENAME TO idx_users_email;
ALTER TABLE w_schedules RENAME TO schedules;
ALTER TABLE w_templates RENAME TO templates;
ALTER TABLE w_task_executions RENAME TO task_executions;
ALTER TABLE w_access_tokens RENAME TO access_tokens;
ALTER TABLE w_uploads RENAME TO uploads;
ALTER TABLE w_system_configs RENAME TO system_configs;
ALTER TABLE w_external_accounts RENAME TO external_accounts;
ALTER TABLE w_auth_sources RENAME TO auth_sources;
ALTER TABLE w_users RENAME TO users;
@@ -0,0 +1,123 @@
-- +goose Up
ALTER TABLE users RENAME TO w_users;
ALTER TABLE auth_sources RENAME TO w_auth_sources;
ALTER TABLE external_accounts RENAME TO w_external_accounts;
ALTER TABLE system_configs RENAME TO w_system_configs;
ALTER TABLE uploads RENAME TO w_uploads;
ALTER TABLE access_tokens RENAME TO w_access_tokens;
ALTER TABLE task_executions RENAME TO w_task_executions;
ALTER TABLE templates RENAME TO w_templates;
ALTER TABLE schedules RENAME TO w_schedules;
-- Drop and Recreate SQLite indexes
DROP INDEX IF EXISTS idx_users_email;
CREATE INDEX IF NOT EXISTS idx_w_users_email ON w_users (email);
DROP INDEX IF EXISTS idx_users_is_active;
CREATE INDEX IF NOT EXISTS idx_w_users_is_active ON w_users (is_active);
DROP INDEX IF EXISTS idx_users_last_login_at;
CREATE INDEX IF NOT EXISTS idx_w_users_last_login_at ON w_users (last_login_at);
DROP INDEX IF EXISTS idx_users_created_at;
CREATE INDEX IF NOT EXISTS idx_w_users_created_at ON w_users (created_at);
DROP INDEX IF EXISTS idx_auth_sources_is_active;
CREATE INDEX IF NOT EXISTS idx_w_auth_sources_is_active ON w_auth_sources (is_active);
DROP INDEX IF EXISTS idx_external_accounts_auth_source_id;
CREATE INDEX IF NOT EXISTS idx_w_external_accounts_auth_source_id ON w_external_accounts (auth_source_id);
DROP INDEX IF EXISTS idx_external_accounts_user_id;
CREATE INDEX IF NOT EXISTS idx_w_external_accounts_user_id ON w_external_accounts (user_id);
DROP INDEX IF EXISTS idx_external_accounts_source_external;
CREATE UNIQUE INDEX IF NOT EXISTS idx_w_external_accounts_source_external ON w_external_accounts (auth_source_id, external_id);
DROP INDEX IF EXISTS idx_uploads_user_id;
CREATE INDEX IF NOT EXISTS idx_w_uploads_user_id ON w_uploads (user_id);
DROP INDEX IF EXISTS idx_uploads_file_path;
CREATE INDEX IF NOT EXISTS idx_w_uploads_file_path ON w_uploads (file_path);
DROP INDEX IF EXISTS idx_uploads_hash;
CREATE INDEX IF NOT EXISTS idx_w_uploads_hash ON w_uploads (hash);
DROP INDEX IF EXISTS idx_uploads_type;
CREATE INDEX IF NOT EXISTS idx_w_uploads_type ON w_uploads (type);
DROP INDEX IF EXISTS idx_access_tokens_user_id;
CREATE INDEX IF NOT EXISTS idx_w_access_tokens_user_id ON w_access_tokens (user_id);
DROP INDEX IF EXISTS idx_task_executions_task_type;
CREATE INDEX IF NOT EXISTS idx_w_task_executions_task_type ON w_task_executions (task_type);
DROP INDEX IF EXISTS idx_task_executions_status;
CREATE INDEX IF NOT EXISTS idx_w_task_executions_status ON w_task_executions (status);
DROP INDEX IF EXISTS idx_task_executions_started_at;
CREATE INDEX IF NOT EXISTS idx_w_task_executions_started_at ON w_task_executions (started_at);
DROP INDEX IF EXISTS idx_task_executions_created_at;
CREATE INDEX IF NOT EXISTS idx_w_task_executions_created_at ON w_task_executions (created_at);
DROP INDEX IF EXISTS idx_templates_is_system;
CREATE INDEX IF NOT EXISTS idx_w_templates_is_system ON w_templates (is_system);
DROP INDEX IF EXISTS idx_templates_created_at;
CREATE INDEX IF NOT EXISTS idx_w_templates_created_at ON w_templates (created_at);
DROP INDEX IF EXISTS idx_templates_updated_at;
CREATE INDEX IF NOT EXISTS idx_w_templates_updated_at ON w_templates (updated_at);
DROP INDEX IF EXISTS idx_schedules_is_active;
CREATE INDEX IF NOT EXISTS idx_w_schedules_is_active ON w_schedules (is_active);
-- +goose Down
ALTER TABLE w_schedules RENAME TO schedules;
ALTER TABLE w_templates RENAME TO templates;
ALTER TABLE w_task_executions RENAME TO task_executions;
ALTER TABLE w_access_tokens RENAME TO access_tokens;
ALTER TABLE w_uploads RENAME TO uploads;
ALTER TABLE w_system_configs RENAME TO system_configs;
ALTER TABLE w_external_accounts RENAME TO external_accounts;
ALTER TABLE w_auth_sources RENAME TO auth_sources;
ALTER TABLE w_users RENAME TO users;
-- Revert indexes
DROP INDEX IF EXISTS idx_w_users_email;
CREATE INDEX IF NOT EXISTS idx_users_email ON users (email);
DROP INDEX IF EXISTS idx_w_users_is_active;
CREATE INDEX IF NOT EXISTS idx_users_is_active ON users (is_active);
DROP INDEX IF EXISTS idx_w_users_last_login_at;
CREATE INDEX IF NOT EXISTS idx_users_last_login_at ON users (last_login_at);
DROP INDEX IF EXISTS idx_w_users_created_at;
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users (created_at);
DROP INDEX IF EXISTS idx_w_auth_sources_is_active;
CREATE INDEX IF NOT EXISTS idx_auth_sources_is_active ON auth_sources (is_active);
DROP INDEX IF EXISTS idx_w_external_accounts_auth_source_id;
CREATE INDEX IF NOT EXISTS idx_external_accounts_auth_source_id ON external_accounts (auth_source_id);
DROP INDEX IF EXISTS idx_w_external_accounts_user_id;
CREATE INDEX IF NOT EXISTS idx_external_accounts_user_id ON external_accounts (user_id);
DROP INDEX IF EXISTS idx_w_external_accounts_source_external;
CREATE UNIQUE INDEX IF NOT EXISTS idx_external_accounts_source_external ON external_accounts (auth_source_id, external_id);
DROP INDEX IF EXISTS idx_w_uploads_user_id;
CREATE INDEX IF NOT EXISTS idx_uploads_user_id ON uploads (user_id);
DROP INDEX IF EXISTS idx_w_uploads_file_path;
CREATE INDEX IF NOT EXISTS idx_uploads_file_path ON uploads (file_path);
DROP INDEX IF EXISTS idx_w_uploads_hash;
CREATE INDEX IF NOT EXISTS idx_uploads_hash ON uploads (hash);
DROP INDEX IF EXISTS idx_w_uploads_type;
CREATE INDEX IF NOT EXISTS idx_uploads_type ON uploads (type);
DROP INDEX IF EXISTS idx_w_access_tokens_user_id;
CREATE INDEX IF NOT EXISTS idx_access_tokens_user_id ON access_tokens (user_id);
DROP INDEX IF EXISTS idx_w_task_executions_task_type;
CREATE INDEX IF NOT EXISTS idx_task_executions_task_type ON task_executions (task_type);
DROP INDEX IF EXISTS idx_w_task_executions_status;
CREATE INDEX IF NOT EXISTS idx_task_executions_status ON task_executions (status);
DROP INDEX IF EXISTS idx_w_task_executions_started_at;
CREATE INDEX IF NOT EXISTS idx_task_executions_started_at ON task_executions (started_at);
DROP INDEX IF EXISTS idx_w_task_executions_created_at;
CREATE INDEX IF NOT EXISTS idx_task_executions_created_at ON task_executions (created_at);
DROP INDEX IF EXISTS idx_w_templates_is_system;
CREATE INDEX IF NOT EXISTS idx_templates_is_system ON templates (is_system);
DROP INDEX IF EXISTS idx_w_templates_created_at;
CREATE INDEX IF NOT EXISTS idx_templates_created_at ON templates (created_at);
DROP INDEX IF EXISTS idx_w_templates_updated_at;
CREATE INDEX IF NOT EXISTS idx_templates_updated_at ON templates (updated_at);
DROP INDEX IF EXISTS idx_w_schedules_is_active;
CREATE INDEX IF NOT EXISTS idx_schedules_is_active ON schedules (is_active);
+5 -5
View File
@@ -35,15 +35,15 @@ func TestMigrateInitializesSQLiteDatabase(t *testing.T) {
Migrate()
var systemConfigCount int64
if err := sqliteDB.Table("system_configs").Count(&systemConfigCount).Error; err != nil {
t.Fatalf("Migrate() count system_configs error = %v", err)
if err := sqliteDB.Table("w_system_configs").Count(&systemConfigCount).Error; err != nil {
t.Fatalf("Migrate() count w_system_configs error = %v", err)
}
if systemConfigCount != 23 {
t.Errorf("Migrate() system_configs count = %d, want %d", systemConfigCount, 23)
t.Errorf("Migrate() w_system_configs count = %d, want %d", systemConfigCount, 23)
}
var adminCount int64
if err := sqliteDB.Table("users").Where("username = ?", "admin").Count(&adminCount).Error; err != nil {
if err := sqliteDB.Table("w_users").Where("username = ?", "admin").Count(&adminCount).Error; err != nil {
t.Fatalf("Migrate() count admin user error = %v", err)
}
if adminCount != 1 {
@@ -51,7 +51,7 @@ func TestMigrateInitializesSQLiteDatabase(t *testing.T) {
}
var templateCount int64
if err := sqliteDB.Table("templates").Count(&templateCount).Error; err != nil {
if err := sqliteDB.Table("w_templates").Count(&templateCount).Error; err != nil {
t.Fatalf("Migrate() count templates error = %v", err)
}
if templateCount != 2 {
+5
View File
@@ -30,6 +30,11 @@ type AccessToken struct {
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}
// TableName 表名
func (AccessToken) TableName() string {
return "w_access_tokens"
}
// GenerateTokenString 生成加密安全的随机 Token 值
func GenerateTokenString() (string, error) {
bytes := make([]byte, tokenByteLength)
+10
View File
@@ -38,6 +38,11 @@ type AuthSource struct {
ClientSecretConfigured bool `json:"client_secret_configured" gorm:"-"`
}
// TableName 表名
func (AuthSource) TableName() string {
return "w_auth_sources"
}
// ExternalAccount 外部账号绑定实体
type ExternalAccount struct {
ID uint64 `json:"id" gorm:"primaryKey"`
@@ -50,6 +55,11 @@ type ExternalAccount struct {
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 表名
func (ExternalAccount) TableName() string {
return "w_external_accounts"
}
// ExternalAccountView 外部帐号绑定视图(脱敏展示用)
type ExternalAccountView struct {
ID uint64 `json:"id"`
+1 -1
View File
@@ -24,7 +24,7 @@ type Schedule struct {
// TableName 表名
func (Schedule) TableName() string {
return "schedules"
return "w_schedules"
}
// CreateSchedule 创建定时任务
+5
View File
@@ -68,6 +68,11 @@ type SystemConfig struct {
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
}
// TableName 表名
func (SystemConfig) TableName() string {
return "w_system_configs"
}
// GetByKey 通过 key 查询配置(带 Redis 缓存)
func (sc *SystemConfig) GetByKey(ctx context.Context, key string) error {
if db.Redis != nil {
+1 -1
View File
@@ -49,7 +49,7 @@ type TaskExecution struct {
// TableName 表名
func (TaskExecution) TableName() string {
return "task_executions"
return "w_task_executions"
}
// CreateTaskExecution 创建任务执行记录
+1 -1
View File
@@ -338,5 +338,5 @@ func TestListTaskExecutionsDefaultPaging(t *testing.T) {
func TestTaskExecutionTableName(t *testing.T) {
execution := TaskExecution{}
assert.Equal(t, "task_executions", execution.TableName())
assert.Equal(t, "w_task_executions", execution.TableName())
}
+5
View File
@@ -29,6 +29,11 @@ type Template struct {
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;index"`
}
// TableName 表名
func (Template) TableName() string {
return "w_templates"
}
// Normalize 规范化模板字段
func (t *Template) Normalize() {
t.Key = strings.TrimSpace(t.Key)
+5
View File
@@ -47,3 +47,8 @@ type Upload struct {
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
}
// TableName 表名
func (Upload) TableName() string {
return "w_uploads"
}
+5
View File
@@ -62,6 +62,11 @@ type User struct {
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;index"`
}
// TableName 表名
func (User) TableName() string {
return "w_users"
}
// SetPassword 设置明文密码
func (u *User) SetPassword(password string) error {
u.Password = password
+1 -1
View File
@@ -2,7 +2,7 @@ CREATE DATABASE IF NOT EXISTS wavelet;
USE wavelet;
CREATE TABLE IF NOT EXISTS user_access_logs
CREATE TABLE IF NOT EXISTS w_user_access_logs
(
id UInt64,
user_id UInt64,