阻断数据库不安全降级并校验三套迁移完整性
This commit is contained in:
1 parent
a2dfd5ef69
commit
8b33282a6e
5 files changed
+307
-7
No files matched your search
@@ -23,13 +23,9 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
if _, err := db.ExecContext(ctx, migrationTableSQL(driver)); err != nil {
|
||||
return fmt.Errorf("创建迁移表失败:%w", err)
|
||||
}
|
||||
var version int
|
||||
err := db.QueryRowContext(ctx, "SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1").Scan(&version)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return fmt.Errorf("读取迁移版本失败:%w", err)
|
||||
}
|
||||
if err == sql.ErrNoRows {
|
||||
version = 0
|
||||
version, err := currentMigrationVersion(ctx, db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for nextVersion := version + 1; nextVersion <= latestMigrationVersion; nextVersion++ {
|
||||
if err := applyMigration(ctx, db, driver, nextVersion); err != nil {
|
||||
@@ -41,6 +37,33 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
|
||||
const latestMigrationVersion = 9
|
||||
|
||||
// currentMigrationVersion 在执行业务迁移前拒绝新版数据库及不连续的迁移历史。
|
||||
func currentMigrationVersion(ctx context.Context, db *sql.DB) (int, error) {
|
||||
rows, err := db.QueryContext(ctx, "SELECT version FROM schema_migrations ORDER BY version")
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("读取迁移版本失败:%w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
current := 0
|
||||
for rows.Next() {
|
||||
var version int
|
||||
if err := rows.Scan(&version); err != nil {
|
||||
return 0, fmt.Errorf("读取迁移版本失败:%w", err)
|
||||
}
|
||||
if version > latestMigrationVersion {
|
||||
return 0, fmt.Errorf("数据库版本 %d 高于当前程序支持的版本 %d,拒绝启动;请使用兼容版本程序,不要删除迁移记录", version, latestMigrationVersion)
|
||||
}
|
||||
if version != current+1 {
|
||||
return 0, fmt.Errorf("数据库迁移历史不连续:期望版本 %d,实际版本 %d,拒绝继续迁移", current+1, version)
|
||||
}
|
||||
current = version
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return 0, fmt.Errorf("读取迁移版本失败:%w", err)
|
||||
}
|
||||
return current, nil
|
||||
}
|
||||
|
||||
func applyMigration(ctx context.Context, db *sql.DB, driver string, version int) error {
|
||||
path := fmt.Sprintf("migrations/%s/%04d_%s.sql", driver, version, migrationName(version))
|
||||
content, err := migrationFiles.ReadFile(path)
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func checkMigrationCatalog(source, embedded fs.FS) error {
|
||||
for _, driver := range []string{"sqlite", "mysql", "postgres"} {
|
||||
directory := "migrations/" + driver
|
||||
for _, catalog := range []fs.FS{source, embedded} {
|
||||
entries, err := fs.ReadDir(catalog, directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(entries) != latestMigrationVersion {
|
||||
return fmt.Errorf("%s 的文件数 %d 与登记版本 %d 不一致", directory, len(entries), latestMigrationVersion)
|
||||
}
|
||||
for version, entry := range entries {
|
||||
expected := fmt.Sprintf("%04d_%s.sql", version+1, migrationName(version+1))
|
||||
if entry.IsDir() || entry.Name() != expected {
|
||||
return fmt.Errorf("%s 的迁移文件应为 %s,实际为 %s", directory, expected, entry.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
for version := 1; version <= latestMigrationVersion; version++ {
|
||||
name := path.Join(directory, fmt.Sprintf("%04d_%s.sql", version, migrationName(version)))
|
||||
content, err := fs.ReadFile(source, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
packed, err := fs.ReadFile(embedded, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(bytes.TrimSpace(content)) == 0 || !bytes.Equal(content, packed) {
|
||||
return fmt.Errorf("%s 为空或与嵌入内容不一致", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestMigrationCatalogMatchesSourceAndVersionRegistry(t *testing.T) {
|
||||
if err := checkMigrationCatalog(os.DirFS("."), migrationFiles); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrationCatalogDetectsIncompleteChanges(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
mutate func(fstest.MapFS, fstest.MapFS)
|
||||
}{
|
||||
{"unregistered_source", func(source, embedded fstest.MapFS) {
|
||||
source["migrations/mysql/0010_extra.sql"] = &fstest.MapFile{Data: []byte("SELECT 1;")}
|
||||
}},
|
||||
{"missing_embed", func(source, embedded fstest.MapFS) { delete(embedded, "migrations/postgres/0001_initial.sql") }},
|
||||
{"missing_source", func(source, embedded fstest.MapFS) { delete(source, "migrations/sqlite/0001_initial.sql") }},
|
||||
{"changed_embed", func(source, embedded fstest.MapFS) {
|
||||
embedded["migrations/mysql/0001_initial.sql"] = &fstest.MapFile{Data: []byte("SELECT 2;")}
|
||||
}},
|
||||
{"empty_file", func(source, embedded fstest.MapFS) {
|
||||
source["migrations/sqlite/0001_initial.sql"] = &fstest.MapFile{Data: []byte(" \n")}
|
||||
}},
|
||||
{"wrong_name", func(source, embedded fstest.MapFS) {
|
||||
source["migrations/mysql/0001_wrong.sql"] = source["migrations/mysql/0001_initial.sql"]
|
||||
delete(source, "migrations/mysql/0001_initial.sql")
|
||||
}},
|
||||
{"unregistered_embed", func(source, embedded fstest.MapFS) {
|
||||
embedded["migrations/postgres/0010_extra.sql"] = &fstest.MapFile{Data: []byte("SELECT 1;")}
|
||||
}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
source, embedded := fstest.MapFS{}, fstest.MapFS{}
|
||||
for _, driver := range []string{"sqlite", "mysql", "postgres"} {
|
||||
for version := 1; version <= latestMigrationVersion; version++ {
|
||||
name := fmt.Sprintf("migrations/%s/%04d_%s.sql", driver, version, migrationName(version))
|
||||
source[name] = &fstest.MapFile{Data: []byte("SELECT 1;")}
|
||||
embedded[name] = &fstest.MapFile{Data: []byte("SELECT 1;")}
|
||||
}
|
||||
}
|
||||
if err := checkMigrationCatalog(source, embedded); err != nil {
|
||||
t.Fatalf("测试基线无效:%v", err)
|
||||
}
|
||||
tc.mutate(source, embedded)
|
||||
if err := checkMigrationCatalog(source, embedded); err == nil {
|
||||
t.Fatal("未检出不完整的迁移变更")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestMigrateRejectsInvalidHistoryWithoutChangingDatabase(t *testing.T) {
|
||||
newerHistory := make([]int, latestMigrationVersion+1)
|
||||
for index := range newerHistory {
|
||||
newerHistory[index] = index + 1
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
versions []int
|
||||
message string
|
||||
}{
|
||||
{"newer_database", newerHistory, "高于当前程序支持"},
|
||||
{"missing_first", []int{2}, "迁移历史不连续"},
|
||||
{"missing_middle", []int{1, 3}, "迁移历史不连续"},
|
||||
{"zero", []int{0}, "迁移历史不连续"},
|
||||
{"negative", []int{-1}, "迁移历史不连续"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
db, err := Open(t.Context(), config.Default(t.TempDir()).Database)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
if _, err := db.DB.ExecContext(t.Context(), migrationTableSQL("sqlite")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, version := range tc.versions {
|
||||
if _, err := db.DB.ExecContext(t.Context(), "INSERT INTO schema_migrations (version) VALUES (?)", version); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := db.DB.ExecContext(t.Context(), "CREATE TABLE preserved_data (value TEXT); INSERT INTO preserved_data VALUES ('unchanged')"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = Migrate(t.Context(), db.DB, "sqlite")
|
||||
if err == nil || !strings.Contains(err.Error(), tc.message) {
|
||||
t.Fatalf("未拒绝异常迁移历史:%v", err)
|
||||
}
|
||||
var count int
|
||||
if err := db.DB.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != len(tc.versions) {
|
||||
t.Fatalf("拒绝迁移后版本记录被修改:%d", count)
|
||||
}
|
||||
if err := db.DB.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'").Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 2 {
|
||||
t.Fatalf("拒绝迁移后创建了业务表:%d", count)
|
||||
}
|
||||
var value string
|
||||
if err := db.DB.QueryRow("SELECT value FROM preserved_data").Scan(&value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value != "unchanged" {
|
||||
t.Fatal("拒绝迁移后原数据被修改")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteUpgradeFromEverySupportedVersion(t *testing.T) {
|
||||
for version := 0; version <= latestMigrationVersion; version++ {
|
||||
t.Run(fmt.Sprintf("version_%d", version), func(t *testing.T) {
|
||||
db, err := Open(t.Context(), config.Default(t.TempDir()).Database)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
if _, err := db.DB.ExecContext(t.Context(), migrationTableSQL("sqlite")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for next := 1; next <= version; next++ {
|
||||
if err := applyMigration(t.Context(), db.DB, "sqlite", next); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for repeat := 0; repeat < 2; repeat++ {
|
||||
if err := Migrate(t.Context(), db.DB, "sqlite"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
actual, err := currentMigrationVersion(t.Context(), db.DB)
|
||||
if err != nil || actual != latestMigrationVersion {
|
||||
t.Fatalf("迁移历史不完整:%d:%v", actual, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user