fix: 初验针对修改
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type FileStatus string
|
||||
type FileVisibility string
|
||||
|
||||
const (
|
||||
FileStatusPending FileStatus = "pending"
|
||||
@@ -15,20 +16,26 @@ const (
|
||||
FileStatusExpired FileStatus = "expired"
|
||||
)
|
||||
|
||||
const (
|
||||
FileVisibilityPrivate FileVisibility = "private"
|
||||
FileVisibilityPublic FileVisibility = "public"
|
||||
)
|
||||
|
||||
type FileObject struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Identity string `gorm:"size:64;not null;uniqueIndex" json:"identity"`
|
||||
Namespace string `gorm:"size:64;not null" json:"namespace"`
|
||||
Provider string `gorm:"size:32;not null" json:"provider"`
|
||||
Bucket string `gorm:"size:255;not null" json:"bucket"`
|
||||
ObjectKey string `gorm:"size:512;not null;uniqueIndex" json:"object_key"`
|
||||
StorageContainer string `gorm:"size:255;not null" json:"-"`
|
||||
ObjectKey string `gorm:"size:512;not null;uniqueIndex" json:"-"`
|
||||
OriginalName string `gorm:"size:255;not null" json:"original_name"`
|
||||
Extension string `gorm:"size:32" json:"extension"`
|
||||
ContentType string `gorm:"size:255" json:"content_type"`
|
||||
ExpectedSize int64 `gorm:"not null" json:"expected_size"`
|
||||
ActualSize int64 `json:"actual_size"`
|
||||
ETag string `gorm:"size:255" json:"etag"`
|
||||
Status FileStatus `gorm:"size:16;not null;index:idx_files_object_status_expires_at,priority:1;index:idx_files_object_status_delete_lease,priority:1" json:"status"`
|
||||
StorageETag string `gorm:"column:storage_etag;size:255" json:"storage_etag"`
|
||||
Visibility FileVisibility `gorm:"size:16;not null;default:private;index:idx_files_object_visibility_status,priority:1" json:"visibility"`
|
||||
Status FileStatus `gorm:"size:16;not null;index:idx_files_object_status_expires_at,priority:1;index:idx_files_object_status_delete_lease,priority:1;index:idx_files_object_visibility_status,priority:2" json:"status"`
|
||||
OwnerType string `gorm:"size:64;not null;index:idx_files_object_owner_type_identity,priority:1" json:"owner_type"`
|
||||
OwnerID uint `gorm:"not null" json:"owner_id"`
|
||||
OwnerIdentity string `gorm:"size:64;not null;index:idx_files_object_owner_type_identity,priority:2" json:"owner_identity"`
|
||||
|
||||
66
internal/models/schema_version.go
Normal file
66
internal/models/schema_version.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const requiredSchemaScript = "0001_baseline.up.sql"
|
||||
|
||||
var requiredSchema = struct {
|
||||
checksum string
|
||||
tables []string
|
||||
}{
|
||||
checksum: "ee621597df0d1c80e98cf38b941c1ac00e62f570223b9818bdab20c4e6f96689",
|
||||
tables: []string{"files_object"},
|
||||
}
|
||||
|
||||
// RequireSchemaVersion 确认当前数据库已经由受控迁移脚本初始化。
|
||||
func RequireSchemaVersion(db *gorm.DB, service string) error {
|
||||
if db == nil {
|
||||
return fmt.Errorf("%s 数据库连接未初始化", service)
|
||||
}
|
||||
|
||||
var count int64
|
||||
err := db.Raw(`
|
||||
SELECT COUNT(*)
|
||||
FROM public.ops_schema_migrations
|
||||
WHERE service = ? AND script_name = ? AND checksum = ? AND status = 'success'`,
|
||||
service, requiredSchemaScript, requiredSchema.checksum).Scan(&count).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s 数据库版本检查失败,请先执行 migrate-all.sh up --service %s: %w", service, service, err)
|
||||
}
|
||||
if count != 1 {
|
||||
return fmt.Errorf("%s 数据库迁移版本不匹配,请执行 migrate-all.sh status --service %s", service, service)
|
||||
}
|
||||
|
||||
for _, table := range requiredSchema.tables {
|
||||
var valid bool
|
||||
err = db.Raw(`
|
||||
SELECT to_regclass(?) IS NOT NULL AND EXISTS (
|
||||
SELECT 1
|
||||
FROM pg_constraint c
|
||||
JOIN pg_class t ON t.oid = c.conrelid
|
||||
JOIN pg_namespace n ON n.oid = t.relnamespace
|
||||
WHERE n.nspname = 'public' AND t.relname = ? AND c.contype = 'p'
|
||||
)`, "public."+table, table).Scan(&valid).Error
|
||||
if err != nil || !valid {
|
||||
return fmt.Errorf("%s 数据库关键表结构不完整:%s", service, table)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RequireStorageProvider(db *gorm.DB, provider string) error {
|
||||
var providers []string
|
||||
if err := db.Model(&FileObject{}).Distinct("provider").Pluck("provider", &providers).Error; err != nil {
|
||||
return fmt.Errorf("检查文件存储类型失败: %w", err)
|
||||
}
|
||||
for _, storedProvider := range providers {
|
||||
if storedProvider != provider {
|
||||
return fmt.Errorf("数据库存在 %s 存储的文件,当前配置为 %s,禁止混用存储后端", storedProvider, provider)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user