mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 00:26:37 +08:00
[优化] 合并
This commit is contained in:
@@ -1,35 +0,0 @@
|
|||||||
package goose
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
presslygoose "github.com/pressly/goose/v3"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
const versionPagesAPIProxy int64 = 202606030004
|
|
||||||
|
|
||||||
// migration202606030004 adds API proxying fields (enabled, path, pass, rewrite)
|
|
||||||
// to Pages projects.
|
|
||||||
func migration202606030004(backend string, ctx Context) *presslygoose.Migration {
|
|
||||||
return newGORMMigration(
|
|
||||||
versionPagesAPIProxy,
|
|
||||||
"202606030004_add_pages_api_proxy.go",
|
|
||||||
backend,
|
|
||||||
ctx,
|
|
||||||
migratePagesAPIProxy,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func migratePagesAPIProxy(ctx Context, db *gorm.DB, backend string) error {
|
|
||||||
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Verify that the columns exist
|
|
||||||
for _, col := range []string{"api_proxy_enabled", "api_proxy_path", "api_proxy_pass", "api_proxy_rewrite"} {
|
|
||||||
if !db.Migrator().HasColumn("pages_projects", col) {
|
|
||||||
return fmt.Errorf("column pages_projects.%s is missing", col)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package goose
|
|
||||||
|
|
||||||
import (
|
|
||||||
presslygoose "github.com/pressly/goose/v3"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
const versionPagesDeploymentRootDir int64 = 202606040001
|
|
||||||
|
|
||||||
// migration202606040001 adds RootDir field to Pages deployments.
|
|
||||||
func migration202606040001(backend string, ctx Context) *presslygoose.Migration {
|
|
||||||
return newGORMMigration(
|
|
||||||
versionPagesDeploymentRootDir,
|
|
||||||
"202606040001_add_pages_deployment_root_dir.go",
|
|
||||||
backend,
|
|
||||||
ctx,
|
|
||||||
migratePagesDeploymentRootDir,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func migratePagesDeploymentRootDir(ctx Context, db *gorm.DB, backend string) error {
|
|
||||||
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package goose
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
presslygoose "github.com/pressly/goose/v3"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
const versionPagesProjectRootDir int64 = 202606040002
|
|
||||||
|
|
||||||
// migration202606040002 adds RootDir and EntryFile fields to Pages projects.
|
|
||||||
func migration202606040002(backend string, ctx Context) *presslygoose.Migration {
|
|
||||||
return newGORMMigration(
|
|
||||||
versionPagesProjectRootDir,
|
|
||||||
"202606040002_add_pages_project_root_dir.go",
|
|
||||||
backend,
|
|
||||||
ctx,
|
|
||||||
migratePagesProjectRootDir,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func migratePagesProjectRootDir(ctx Context, db *gorm.DB, backend string) error {
|
|
||||||
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Verify that the columns exist
|
|
||||||
if !db.Migrator().HasColumn("pages_projects", "root_dir") {
|
|
||||||
return fmt.Errorf("column pages_projects.root_dir is missing")
|
|
||||||
}
|
|
||||||
if !db.Migrator().HasColumn("pages_projects", "entry_file") {
|
|
||||||
return fmt.Errorf("column pages_projects.entry_file is missing")
|
|
||||||
}
|
|
||||||
// Backfill existing rows: if entry_file is empty, set to 'index.html'
|
|
||||||
type PagesProject struct {
|
|
||||||
ID uint `gorm:"primaryKey"`
|
|
||||||
EntryFile string `gorm:"size:512;not null;default:'index.html'"`
|
|
||||||
}
|
|
||||||
if err := db.Model(&PagesProject{}).Where("entry_file = '' OR entry_file IS NULL").Update("entry_file", "index.html").Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to backfill pages_projects.entry_file: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
-36
@@ -1,36 +0,0 @@
|
|||||||
package goose
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
presslygoose "github.com/pressly/goose/v3"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
const versionPagesDeploymentDropUnusedFields int64 = 202606040003
|
|
||||||
|
|
||||||
// migration202606040003 drops RootDir and EntryFile fields from Pages deployments.
|
|
||||||
func migration202606040003(backend string, ctx Context) *presslygoose.Migration {
|
|
||||||
return newGORMMigration(
|
|
||||||
versionPagesDeploymentDropUnusedFields,
|
|
||||||
"202606040003_drop_pages_deployment_unused_fields.go",
|
|
||||||
backend,
|
|
||||||
ctx,
|
|
||||||
migratePagesDeploymentDropUnusedFields,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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.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.Exec("ALTER TABLE pages_deployments DROP COLUMN entry_file").Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to drop pages_deployments.entry_file: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package goose
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
presslygoose "github.com/pressly/goose/v3"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
const versionPagesFeaturesAndCleanup int64 = 202606040004
|
||||||
|
|
||||||
|
// migration202606040004 merges migrations 202606030004, 202606040001, 202606040002, and 202606040003.
|
||||||
|
// It adds Pages API proxying fields and RootDir/EntryFile to Pages projects,
|
||||||
|
// backfills default entry_file to 'index.html', and ensures unused fields (root_dir, entry_file)
|
||||||
|
// are dropped from Pages deployments.
|
||||||
|
func migration202606040004(backend string, ctx Context) *presslygoose.Migration {
|
||||||
|
return newGORMMigration(
|
||||||
|
versionPagesFeaturesAndCleanup,
|
||||||
|
"202606040004_add_pages_features_and_cleanup.go",
|
||||||
|
backend,
|
||||||
|
ctx,
|
||||||
|
migratePagesFeaturesAndCleanup,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func migratePagesFeaturesAndCleanup(ctx Context, db *gorm.DB, backend string) error {
|
||||||
|
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Verify Pages projects columns
|
||||||
|
cols := []string{
|
||||||
|
"api_proxy_enabled", "api_proxy_path", "api_proxy_pass", "api_proxy_rewrite",
|
||||||
|
"root_dir", "entry_file",
|
||||||
|
}
|
||||||
|
for _, col := range cols {
|
||||||
|
if !db.Migrator().HasColumn("pages_projects", col) {
|
||||||
|
return fmt.Errorf("column pages_projects.%s is missing", col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Backfill pages_projects.entry_file to 'index.html' if empty
|
||||||
|
type PagesProject struct {
|
||||||
|
ID uint `gorm:"primaryKey"`
|
||||||
|
EntryFile string `gorm:"size:512;not null;default:'index.html'"`
|
||||||
|
}
|
||||||
|
if err := db.Model(&PagesProject{}).Where("entry_file = '' OR entry_file IS NULL").Update("entry_file", "index.html").Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to backfill pages_projects.entry_file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Drop unused fields root_dir and entry_file from pages_deployments if they exist
|
||||||
|
if db.Migrator().HasColumn("pages_deployments", "root_dir") {
|
||||||
|
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.Exec("ALTER TABLE pages_deployments DROP COLUMN entry_file").Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to drop pages_deployments.entry_file: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -44,10 +44,7 @@ func registeredMigrations(backend string, ctx Context) []*presslygoose.Migration
|
|||||||
migration202606030001(backend, ctx),
|
migration202606030001(backend, ctx),
|
||||||
migration202606030002(backend, ctx),
|
migration202606030002(backend, ctx),
|
||||||
migration202606030003(backend, ctx),
|
migration202606030003(backend, ctx),
|
||||||
migration202606030004(backend, ctx),
|
migration202606040004(backend, ctx),
|
||||||
migration202606040001(backend, ctx),
|
|
||||||
migration202606040002(backend, ctx),
|
|
||||||
migration202606040003(backend, ctx),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -866,7 +866,7 @@ func TestEnsureDatabaseSchemaUpToDateDropsPagesDeploymentUnusedFields(t *testing
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove the migration record from goose_db_version table
|
// Remove the migration record from goose_db_version table
|
||||||
const versionToRerun = 202606040003
|
const versionToRerun = 202606040004
|
||||||
if err := db.Exec("DELETE FROM goose_db_version WHERE version_id = ?", versionToRerun).Error; err != nil {
|
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)
|
t.Fatalf("failed to delete migration record: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user