refactor(migration): delete pkg/migrator, move SQL to per-plugin embed

BREAKING: pkg/migrator/ deleted entirely. Migration SQL files are now
owned by each plugin in its own migrations/ directory.

Architecture:
- Delete pkg/migrator/ (26 global SQL files + ClickHouse migration)
- Move global SQL to per-plugin migrations/ with go:embed + Register()
- Rewrite cmd/app.go gooseEngine: uses Inject[DBService] for DB, iterates
  all plugin-registered MigrationEntry, runs goose.Up per entry
- core.MigrationEngine.Migrate signature changed: *Context instead of
  context.Context, so engine can resolve services via IoC

Per-plugin migration ownership:
  auth/             → w_access_tokens, w_auth_sources, w_external_accounts
  user/             → w_users (seed system user)
  admin/            → w_system_configs, w_templates (seeds)
  upload/           → w_uploads, w_upload_stats
  message_gateway/  → w_push_*, w_message_*
  risk_control/     → w_user_access_logs (PG + ClickHouse)
  driver_asynq_cron/  → w_schedules
  driver_asynq_worker/ → w_task_executions

Dependencies:
- cmd/banner.go: removed migration report display (migrations are automatic)
- cmd/reset_passwd.go: removed PreRun migrator.Migrate() call
- go.mod: clickhouse-go kept (used by plugins/infra/database/clickhouse.go)
This commit is contained in:
ryan
2026-08-28 12:19:25 +08:00
parent c9b702d234
commit 530a9dd3ee
79 changed files with 552 additions and 1534 deletions
+3 -3
View File
@@ -212,7 +212,7 @@ func (a *App) ApplyPlugins() error {
}
// RunMigrations dispatches migration execution across all registered plugin migration entries.
func (a *App) RunMigrations(ctx context.Context) error {
func (a *App) RunMigrations() error {
entries := a.ctx.Migrations().Entries()
if len(entries) == 0 {
return nil
@@ -233,7 +233,7 @@ func (a *App) RunMigrations(ctx context.Context) error {
return nil
}
if err := engine.Migrate(ctx, entries); err != nil {
if err := engine.Migrate(a.ctx, entries); err != nil {
return fmt.Errorf("core: migration failed: %w", err)
}
@@ -275,7 +275,7 @@ func (a *App) Start(ctx ...context.Context) error {
}
// 2. Run migrations
if err := a.RunMigrations(baseCtx); err != nil {
if err := a.RunMigrations(); err != nil {
a.mu.Lock()
a.running = false
a.mu.Unlock()
+3 -3
View File
@@ -321,7 +321,7 @@ func TestAppStartDriverFailureRollback(t *testing.T) {
func TestAppMigrationEngineExecution(t *testing.T) {
var migratedEntries []extpoints.MigrationEntry
runner := core.MigrationRunner(func(ctx context.Context, entries []extpoints.MigrationEntry) error {
runner := core.MigrationRunner(func(ctx *core.Context, entries []extpoints.MigrationEntry) error {
migratedEntries = entries
return nil
})
@@ -354,7 +354,7 @@ func TestAppMigrationEngineExecution(t *testing.T) {
func TestAppMigrationEngineFromIoCContainer(t *testing.T) {
var executed bool
runner := core.MigrationRunner(func(ctx context.Context, entries []extpoints.MigrationEntry) error {
runner := core.MigrationRunner(func(ctx *core.Context, entries []extpoints.MigrationEntry) error {
executed = true
return nil
})
@@ -483,7 +483,7 @@ func TestAppIdempotencyAndErrorStates(t *testing.T) {
assert.False(t, app2.IsRunning())
// Migration failure
migFailRunner := core.MigrationRunner(func(ctx context.Context, entries []extpoints.MigrationEntry) error {
migFailRunner := core.MigrationRunner(func(ctx *core.Context, entries []extpoints.MigrationEntry) error {
return errors.New("sql migrate error")
})
sqlFS := fstest.MapFS{
+5 -3
View File
@@ -96,15 +96,17 @@ const (
)
// MigrationEngine is the interface for executing database migrations across registered plugins.
// The ctx parameter is the root micro-kernel Context, allowing the engine to resolve
// services from the IoC container via core.Inject or core.Using.
type MigrationEngine interface {
Migrate(ctx context.Context, entries []MigrationEntry) error
Migrate(ctx *Context, entries []MigrationEntry) error
}
// MigrationRunner is a function adapter implementing MigrationEngine.
type MigrationRunner func(ctx context.Context, entries []MigrationEntry) error
type MigrationRunner func(ctx *Context, entries []MigrationEntry) error
// Migrate calls the underlying migration function.
func (fn MigrationRunner) Migrate(ctx context.Context, entries []MigrationEntry) error {
func (fn MigrationRunner) Migrate(ctx *Context, entries []MigrationEntry) error {
return fn(ctx, entries)
}