mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-01 22:46:38 +08:00
[优化] 修复错误
This commit is contained in:
+2
-2
@@ -23,12 +23,12 @@ func migration202606040003(backend string, ctx Context) *presslygoose.Migration
|
||||
func migratePagesDeploymentDropUnusedFields(ctx Context, db *gorm.DB, backend string) error {
|
||||
// Drop columns root_dir and entry_file from pages_deployments
|
||||
if db.Migrator().HasColumn("pages_deployments", "root_dir") {
|
||||
if err := db.Migrator().DropColumn("pages_deployments", "root_dir"); err != nil {
|
||||
if err := db.Exec("ALTER TABLE pages_deployments DROP COLUMN root_dir").Error; err != nil {
|
||||
return fmt.Errorf("failed to drop pages_deployments.root_dir: %w", err)
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn("pages_deployments", "entry_file") {
|
||||
if err := db.Migrator().DropColumn("pages_deployments", "entry_file"); err != nil {
|
||||
if err := db.Exec("ALTER TABLE pages_deployments DROP COLUMN entry_file").Error; err != nil {
|
||||
return fmt.Errorf("failed to drop pages_deployments.entry_file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -830,3 +830,57 @@ func TestAllGORMModelsAreRegistered(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureDatabaseSchemaUpToDateDropsPagesDeploymentUnusedFields(t *testing.T) {
|
||||
db := openBareTestSQLiteDB(t, "drop-pages-deployment-unused-fields.db")
|
||||
if err := registerSharding(db, "sqlite"); err != nil {
|
||||
t.Fatalf("register sharding: %v", err)
|
||||
}
|
||||
|
||||
if err := ensureDatabaseSchemaUpToDate(db, "sqlite"); err != nil {
|
||||
t.Fatalf("first ensureDatabaseSchemaUpToDate: %v", err)
|
||||
}
|
||||
|
||||
// Verify columns do not exist
|
||||
if db.Migrator().HasColumn("pages_deployments", "root_dir") {
|
||||
t.Fatal("expected root_dir column to be absent initially")
|
||||
}
|
||||
if db.Migrator().HasColumn("pages_deployments", "entry_file") {
|
||||
t.Fatal("expected entry_file column to be absent initially")
|
||||
}
|
||||
|
||||
// Manually add columns to simulate old state
|
||||
if err := db.Exec("ALTER TABLE pages_deployments ADD COLUMN root_dir TEXT").Error; err != nil {
|
||||
t.Fatalf("failed to add root_dir column: %v", err)
|
||||
}
|
||||
if err := db.Exec("ALTER TABLE pages_deployments ADD COLUMN entry_file TEXT").Error; err != nil {
|
||||
t.Fatalf("failed to add entry_file column: %v", err)
|
||||
}
|
||||
|
||||
// Verify columns were added
|
||||
if !db.Migrator().HasColumn("pages_deployments", "root_dir") {
|
||||
t.Fatal("expected root_dir column to be present after manual add")
|
||||
}
|
||||
if !db.Migrator().HasColumn("pages_deployments", "entry_file") {
|
||||
t.Fatal("expected entry_file column to be present after manual add")
|
||||
}
|
||||
|
||||
// Remove the migration record from goose_db_version table
|
||||
const versionToRerun = 202606040003
|
||||
if err := db.Exec("DELETE FROM goose_db_version WHERE version_id = ?", versionToRerun).Error; err != nil {
|
||||
t.Fatalf("failed to delete migration record: %v", err)
|
||||
}
|
||||
|
||||
// Run migration again
|
||||
if err := ensureDatabaseSchemaUpToDate(db, "sqlite"); err != nil {
|
||||
t.Fatalf("second ensureDatabaseSchemaUpToDate: %v", err)
|
||||
}
|
||||
|
||||
// Verify columns were dropped successfully
|
||||
if db.Migrator().HasColumn("pages_deployments", "root_dir") {
|
||||
t.Fatal("expected root_dir column to be dropped after migration rerun")
|
||||
}
|
||||
if db.Migrator().HasColumn("pages_deployments", "entry_file") {
|
||||
t.Fatal("expected entry_file column to be dropped after migration rerun")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user