feat(server): cmd/migrate up 接入 codes 库自建迁移(ApplyCodesLibMigrations)(#codes-lib)
顺带修 000020 sqlite 索引名冲突:ALTER TABLE RENAME 不会带着重命名索引, legacy_codes 上遗留的 idx_codes_status 与 codes 库自己迁移里同名索引撞名, 一并改成 idx_legacy_codes_status(up/down 对称)。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
migratemysql "github.com/golang-migrate/migrate/v4/database/mysql"
|
||||
migratesqlite "github.com/golang-migrate/migrate/v4/database/sqlite"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
libcodes "github.com/wangjia/codes"
|
||||
|
||||
"github.com/wangjia/pangolin/server/internal/db"
|
||||
"github.com/wangjia/pangolin/server/migrations"
|
||||
@@ -24,6 +26,18 @@ func MigrateUp(database *sql.DB, driver string) error {
|
||||
})
|
||||
}
|
||||
|
||||
// ApplyCodesLibMigrations creates/updates the shared codes library's own
|
||||
// tables (codes / codes_batches / codes_audit_log, tracked by the lib's
|
||||
// codes_schema_migrations). Runs AFTER pangolin's golang-migrate set — 000020
|
||||
// must have renamed the legacy `codes` table away first. Idempotent.
|
||||
func ApplyCodesLibMigrations(ctx context.Context, database *sql.DB, driver string) error {
|
||||
d := libcodes.DialectMySQL
|
||||
if db.Normalize(driver) == "sqlite" {
|
||||
d = libcodes.DialectSQLite
|
||||
}
|
||||
return libcodes.ApplyMigrations(ctx, database, d)
|
||||
}
|
||||
|
||||
// MigrateDown rolls back all applied migrations. ErrNoChange = success.
|
||||
func MigrateDown(database *sql.DB, driver string) error {
|
||||
return runMigration(database, driver, func(m *migrate.Migrate) error {
|
||||
|
||||
Reference in New Issue
Block a user