Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions backend/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion backend/ent/schema/auth_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func (AuthIdentity) Edges() []ent.Edge {
func (AuthIdentity) Indexes() []ent.Index {
return []ent.Index{
index.Fields("provider_type", "provider_key", "provider_subject").Unique(),
index.Fields("user_id"),
index.Fields("user_id", "provider_type"),
}
}
1 change: 0 additions & 1 deletion backend/ent/schema/channel_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (ChannelMonitor) Edges() []ent.Edge {
func (ChannelMonitor) Indexes() []ent.Index {
return []ent.Index{
index.Fields("enabled", "last_checked_at"),
index.Fields("provider"),
index.Fields("provider", "api_mode"),
index.Fields("group_name"),
index.Fields("template_id"),
Expand Down
5 changes: 1 addition & 4 deletions backend/ent/schema/payment_audit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)

// PaymentAuditLog holds the schema definition for the PaymentAuditLog entity.
Expand Down Expand Up @@ -48,7 +47,5 @@ func (PaymentAuditLog) Fields() []ent.Field {
}

func (PaymentAuditLog) Indexes() []ent.Index {
return []ent.Index{
index.Fields("order_id"),
}
return []ent.Index{}
}
6 changes: 0 additions & 6 deletions backend/ent/schema/usage_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,7 @@ func (UsageLog) Edges() []ent.Edge {
// Indexes 定义数据库索引,优化查询性能。
func (UsageLog) Indexes() []ent.Index {
return []ent.Index{
index.Fields("user_id"),
index.Fields("api_key_id"),
index.Fields("account_id"),
index.Fields("group_id"),
index.Fields("subscription_id"),
index.Fields("created_at"),
index.Fields("model"),
index.Fields("requested_model"),
index.Fields("request_id"),
// 复合索引用于时间范围查询
Expand Down
5 changes: 3 additions & 2 deletions backend/internal/repository/migrations_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,9 @@ func validateMigrationExecutionMode(name, content string) (bool, error) {
}

if strings.Contains(normalizedStmt, "CONCURRENTLY") {
isCreateIndex := strings.Contains(normalizedStmt, "CREATE") && strings.Contains(normalizedStmt, "INDEX")
isDropIndex := strings.Contains(normalizedStmt, "DROP") && strings.Contains(normalizedStmt, "INDEX")
// 按首关键字判定,避免索引名含 "created" 等子串使 DROP 被误判为 CREATE。
isCreateIndex := strings.HasPrefix(normalizedStmt, "CREATE") && strings.Contains(normalizedStmt, "INDEX")
isDropIndex := strings.HasPrefix(normalizedStmt, "DROP") && strings.Contains(normalizedStmt, "INDEX")
if !isCreateIndex && !isDropIndex {
return false, errors.New("*_notx.sql currently only supports CREATE/DROP INDEX CONCURRENTLY statements")
}
Expand Down
6 changes: 6 additions & 0 deletions backend/internal/repository/migrations_runner_notx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ DROP INDEX CONCURRENTLY IF EXISTS idx_b;
require.True(t, nonTx)
require.NoError(t, err)
})

t.Run("notx迁移允许DROP名称含created的索引", func(t *testing.T) {
nonTx, err := validateMigrationExecutionMode("143_drop_idx_notx.sql", "DROP INDEX CONCURRENTLY IF EXISTS idx_usage_logs_created_at;")
require.True(t, nonTx)
require.NoError(t, err)
})
}

func TestApplyMigrationsFS_NonTransactionalMigration(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions backend/migrations/143_drop_redundant_indexes_notx.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- 143:删除被云数据库巡检标记为冗余的单列索引(均为既有复合/唯一索引的最左前缀,可被覆盖)。
-- 采用 *_notx + DROP INDEX CONCURRENTLY 避免在高写入表上持有排他锁;IF EXISTS 保证可重复执行。

DROP INDEX CONCURRENTLY IF EXISTS idx_usage_logs_user_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_usage_logs_api_key_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_usage_logs_account_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_usage_logs_model;
DROP INDEX CONCURRENTLY IF EXISTS idx_usage_logs_subscription_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_usage_logs_created_at;
DROP INDEX CONCURRENTLY IF EXISTS idx_usage_dashboard_hourly_users_bucket_start;
DROP INDEX CONCURRENTLY IF EXISTS idx_usage_dashboard_daily_users_bucket_date;
DROP INDEX CONCURRENTLY IF EXISTS idx_payment_audit_logs_order_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_channel_monitors_provider;
DROP INDEX CONCURRENTLY IF EXISTS auth_identity_migration_reports_type_idx;
DROP INDEX CONCURRENTLY IF EXISTS auth_identities_user_id_idx;
DROP INDEX CONCURRENTLY IF EXISTS user_provider_default_grants_user_id_idx;
Loading