完善配置快照预检与数据库迁移失败处理
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-08-30 21:34:23 +08:00
1 parent 71a052fbc9
commit 70a2f39042
9 files changed
+147 -2

No files matched your search

@@ -126,6 +126,10 @@ func (e *SQLExecutor) Run(ctx context.Context, plan Plan, update func(Status) er
return ErrSwitchUnavailable
}
if err := e.authorizedSwitch(ctx, plan.expectedGeneration, plan.targetConfig, plan.actor); err != nil {
if isKnownSwitchRejection(err) {
cleanupStructuredTarget(e.copier, plan.sourceConfig, plan.targetConfig)
return err
}
return errors.Join(ErrSwitchOutcomeUnknown, err)
}
if cleanup, ok := e.copier.(StructuredDatabaseCleanup); ok {
@@ -134,6 +138,14 @@ func (e *SQLExecutor) Run(ctx context.Context, plan Plan, update func(Status) er
return nil
}
func isKnownSwitchRejection(err error) bool {
return errors.Is(err, config.ErrAuthorizationRequired) ||
errors.Is(err, config.ErrDatabaseMigrationTarget) ||
errors.Is(err, config.ErrGenerationConflict) ||
errors.Is(err, config.ErrStepUpRequired) ||
errors.Is(err, config.ErrInvalidRuntimeConfig)
}
func cleanupStructuredTarget(copier StructuredDatabaseCopier, source, target config.DatabaseConfig) {
cleanup, ok := copier.(StructuredDatabaseCleanup)
if !ok {
@@ -190,6 +190,20 @@ func (c *cleanupRecordingCopier) Complete(config.DatabaseConfig, config.Database
c.completeCalled = true
}
func TestSQLExecutorCleansTargetForKnownConfigurationSwitchRejection(t *testing.T) {
copier := &cleanupRecordingCopier{}
executor := NewSQLExecutor(copier, func(context.Context, uint64, config.DatabaseConfig, config.AuditActor) error {
return config.ErrGenerationConflict
})
err := executor.Run(context.Background(), validStructuredPlan(t), func(Status) error { return nil })
if !errors.Is(err, config.ErrGenerationConflict) || errors.Is(err, ErrSwitchOutcomeUnknown) {
t.Fatalf("known switch rejection = %v", err)
}
if !copier.cleanupCalled {
t.Fatal("structured target was not cleaned after known switch rejection")
}
}
func TestSQLExecutorPreservesStructuredTargetWhenConfigurationSwitchOutcomeIsUnknown(t *testing.T) {
copier := &cleanupRecordingCopier{}
executor := NewSQLExecutor(copier, func(context.Context, uint64, config.DatabaseConfig, config.AuditActor) error {
@@ -134,6 +134,10 @@ func (e *SQLiteExecutor) Run(ctx context.Context, plan Plan, update func(Status)
switchErr = e.switchConfig(ctx, plan.targetConfig)
}
if switchErr != nil {
if isKnownSwitchRejection(switchErr) {
_ = os.Remove(targetPath)
return switchErr
}
return errors.Join(ErrSwitchOutcomeUnknown, switchErr)
}
switched = true
@@ -139,6 +139,23 @@ func TestSQLiteExecutorRequiresConfigurationSwitch(t *testing.T) {
}
}
func TestSQLiteExecutorCleansTargetForKnownConfigurationSwitchRejection(t *testing.T) {
service, root := newSQLiteMigrationFixture(t, func(context.Context, config.DatabaseConfig) error {
return config.ErrGenerationConflict
})
record, token, err := service.Preview(context.Background(), config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "smail.db"}, 1, "known-rejection.db")
if err != nil {
t.Fatal(err)
}
failed, err := service.Execute(context.Background(), record.ID, token)
if !errors.Is(err, config.ErrGenerationConflict) || errors.Is(err, ErrSwitchOutcomeUnknown) {
t.Fatalf("known switch rejection = %v, record = %+v", err, failed)
}
if _, statErr := os.Stat(filepath.Join(root, "config", "known-rejection.db")); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("target database remained after known switch rejection, stat error = %v", statErr)
}
}
func TestSQLiteExecutorPreservesTargetWhenConfigurationSwitchOutcomeIsUnknown(t *testing.T) {
service, root := newSQLiteMigrationFixture(t, func(context.Context, config.DatabaseConfig) error {
return errors.New("配置切换失败")