mengstack-api/internal/app/migrate/migrate.go
MengStack Dev df809f1045
Some checks failed
CI / Build & Test (push) Failing after 1m31s
feat: user CRUD API + dashboard stats + kernel infrastructure
- Add user management endpoints (list/create/get/update/delete) with pagination and search
- Add dashboard stats endpoint with tenant/user/online counts and growth metrics
- Add tenant resolver middleware for multi-tenant request scoping
- Add i18n kernel with zh/en message files and AcceptLanguage middleware
- Add WebSocket hub/handler for real-time communication
- Add job scheduler kernel with cron support
- Add plugin sandbox for isolated execution
- Add storage kernel (local filesystem)
- Add event bus kernel for pub/sub
- Add cache kernel abstraction
- Add database migration runner and version upgrade checker
- Add rate limiting middleware with Redis backend
- Add SQL migrations for rbac, audit_logs, settings, notifications, examples
- Extend user repository with list/delete/count operations
- Register all module routes with tenant resolver
2026-10-03 03:42:58 +08:00

54 lines
1.2 KiB
Go

package migrate
import (
"fmt"
"io/fs"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
"gorm.io/gorm"
)
func Run(db *gorm.DB, fsys fs.FS) error {
return runWithTable(db, fsys, "schema_migrations")
}
func RunPlugin(db *gorm.DB, pluginName string, fsys fs.FS) error {
if fsys == nil {
return nil
}
table := fmt.Sprintf("schema_migrations_%s", pluginName)
return runWithTable(db, fsys, table)
}
func runWithTable(db *gorm.DB, fsys fs.FS, table string) error {
source, err := iofs.New(fsys, ".")
if err != nil {
return fmt.Errorf("create migration source: %w", err)
}
sqlDB, err := db.DB()
if err != nil {
return err
}
driver, err := postgres.WithInstance(sqlDB, &postgres.Config{
MigrationsTable: table,
})
if err != nil {
return fmt.Errorf("create migration driver: %w", err)
}
m, err := migrate.NewWithInstance("iofs", source, "postgres", driver)
if err != nil {
return fmt.Errorf("create migrator: %w", err)
}
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
return fmt.Errorf("run migrations (%s): %w", table, err)
}
return nil
}