[优化] 代码优化

This commit is contained in:
ryan
2026-06-04 10:12:53 +08:00
parent bd659d493d
commit 29fdc378a1
8 changed files with 46 additions and 23 deletions
@@ -52,7 +52,7 @@ func validatePagesStaticHosting(db *gorm.DB) error {
return fmt.Errorf("column pages_projects.%s is missing", column)
}
}
for _, column := range []string{"project_id", "checksum", "artifact_path", "entry_file"} {
for _, column := range []string{"project_id", "checksum", "artifact_path"} {
if !db.Migrator().HasColumn("pages_deployments", column) {
return fmt.Errorf("column pages_deployments.%s is missing", column)
}
@@ -1,8 +1,6 @@
package goose
import (
"fmt"
presslygoose "github.com/pressly/goose/v3"
"gorm.io/gorm"
)
@@ -24,9 +22,5 @@ func migratePagesDeploymentRootDir(ctx Context, db *gorm.DB, backend string) err
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
return err
}
// Verify that the column exists
if !db.Migrator().HasColumn("pages_deployments", "root_dir") {
return fmt.Errorf("column pages_deployments.root_dir is missing")
}
return nil
}
@@ -0,0 +1,36 @@
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.Migrator().DropColumn("pages_deployments", "root_dir"); 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 {
return fmt.Errorf("failed to drop pages_deployments.entry_file: %w", err)
}
}
return nil
}
@@ -47,6 +47,7 @@ func registeredMigrations(backend string, ctx Context) []*presslygoose.Migration
migration202606030004(backend, ctx),
migration202606040001(backend, ctx),
migration202606040002(backend, ctx),
migration202606040003(backend, ctx),
}
}
-2
View File
@@ -35,8 +35,6 @@ type PagesDeployment struct {
ArtifactPath string `json:"artifact_path" gorm:"size:2048;not null"`
FileCount int `json:"file_count" gorm:"not null;default:0"`
TotalSize int64 `json:"total_size" gorm:"not null;default:0"`
RootDir string `json:"root_dir" gorm:"size:512;not null;default:''"`
EntryFile string `json:"entry_file" gorm:"size:512;not null;default:'index.html'"`
CreatedBy string `json:"created_by" gorm:"size:64;not null;default:''"`
CreatedAt time.Time `json:"created_at"`
ActivatedAt *time.Time `json:"activated_at"`
+2 -2
View File
@@ -612,7 +612,7 @@ func buildSnapshotPagesDeployment(projectID *uint) (*snapshotPagesDeployment, er
return nil, errors.New("Pages 激活部署不属于当前项目")
}
localRoot := fmt.Sprintf("%s/deployments/%d/current", openrestyrender.PagesDirPlaceholder, deployment.ID)
cleanedRootDir := strings.TrimSpace(deployment.RootDir)
cleanedRootDir := strings.TrimSpace(project.RootDir)
if cleanedRootDir != "" {
localRoot = path.Join(localRoot, cleanedRootDir)
}
@@ -622,7 +622,7 @@ func buildSnapshotPagesDeployment(projectID *uint) (*snapshotPagesDeployment, er
DeploymentID: deployment.ID,
DeploymentNumber: deployment.DeploymentNumber,
Checksum: deployment.Checksum,
EntryFile: deployment.EntryFile,
EntryFile: project.EntryFile,
SPAFallbackEnabled: project.SPAFallbackEnabled,
SPAFallbackPath: normalizeStoredPagesFallbackPath(project.SPAFallbackPath),
APIProxyEnabled: project.APIProxyEnabled,
-6
View File
@@ -74,8 +74,6 @@ type PagesDeploymentView struct {
Status string `json:"status"`
FileCount int `json:"file_count"`
TotalSize int64 `json:"total_size"`
RootDir string `json:"root_dir"`
EntryFile string `json:"entry_file"`
CreatedBy string `json:"created_by"`
CreatedAt time.Time `json:"created_at"`
ActivatedAt *time.Time `json:"activated_at"`
@@ -288,8 +286,6 @@ func UploadPagesDeployment(projectID uint, fileHeader *multipart.FileHeader, roo
ArtifactPath: artifactPath,
FileCount: manifest.FileCount,
TotalSize: manifest.TotalSize,
RootDir: rootDir,
EntryFile: manifest.EntryFile,
CreatedBy: strings.TrimSpace(createdBy),
}
if err := tx.Create(deployment).Error; err != nil {
@@ -547,8 +543,6 @@ func buildPagesDeploymentView(deployment *model.PagesDeployment) *PagesDeploymen
Status: deployment.Status,
FileCount: deployment.FileCount,
TotalSize: deployment.TotalSize,
RootDir: deployment.RootDir,
EntryFile: deployment.EntryFile,
CreatedBy: deployment.CreatedBy,
CreatedAt: deployment.CreatedAt,
ActivatedAt: deployment.ActivatedAt,
+6 -6
View File
@@ -268,8 +268,8 @@ func TestUploadPagesDeploymentWithTopLevelFolder(t *testing.T) {
if deployment.FileCount != 2 {
t.Fatalf("expected 2 files, got %d", deployment.FileCount)
}
if deployment.EntryFile != "index.html" {
t.Fatalf("expected EntryFile to be index.html, got %q", deployment.EntryFile)
if project.EntryFile != "index.html" {
t.Fatalf("expected EntryFile to be index.html, got %q", project.EntryFile)
}
}
@@ -354,11 +354,11 @@ func TestUploadPagesDeploymentWithRootDir(t *testing.T) {
if deployment.FileCount != 3 {
t.Fatalf("expected 3 files, got %d", deployment.FileCount)
}
if deployment.RootDir != "build" {
t.Fatalf("expected RootDir to be 'build', got %q", deployment.RootDir)
if project.RootDir != "build" {
t.Fatalf("expected RootDir to be 'build', got %q", project.RootDir)
}
if deployment.EntryFile != "index.html" {
t.Fatalf("expected EntryFile to be 'index.html', got %q", deployment.EntryFile)
if project.EntryFile != "index.html" {
t.Fatalf("expected EntryFile to be 'index.html', got %q", project.EntryFile)
}
// 2. Update project configuration to a wrong entry file relative to root directory, upload should fail