完善证书申请续期与安装初始化流程
This commit is contained in:
1 parent
fd8d8608f5
commit
07636abe44
66 files changed
+9470
-165
No files matched your search
@@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -24,7 +25,11 @@ func TestOpenAndMigrateSQLite(t *testing.T) {
|
||||
if err := db.DB.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&count); err != nil {
|
||||
t.Fatalf("读取迁移版本失败:%v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
if count != 2 {
|
||||
t.Fatalf("迁移版本数量错误:%d", count)
|
||||
}
|
||||
var autoRenew bool
|
||||
if err := db.DB.QueryRow("SELECT auto_renew FROM domains LIMIT 1").Scan(&autoRenew); err != nil && err != sql.ErrNoRows {
|
||||
t.Fatalf("检查域名选项字段失败:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed migrations/sqlite/0001_initial.sql migrations/mysql/0001_initial.sql
|
||||
//go:embed migrations/sqlite/0001_initial.sql migrations/sqlite/0002_domain_options.sql migrations/mysql/0001_initial.sql migrations/mysql/0002_domain_options.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
@@ -29,13 +29,18 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
if err == sql.ErrNoRows {
|
||||
version = 0
|
||||
}
|
||||
if version >= 1 {
|
||||
return nil
|
||||
}
|
||||
path := "migrations/sqlite/0001_initial.sql"
|
||||
if driver == "mysql" {
|
||||
path = "migrations/mysql/0001_initial.sql"
|
||||
for nextVersion := version + 1; nextVersion <= latestMigrationVersion; nextVersion++ {
|
||||
if err := applyMigration(ctx, db, driver, nextVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const latestMigrationVersion = 2
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取迁移文件失败:%w", err)
|
||||
@@ -47,7 +52,7 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
return fmt.Errorf("执行数据库迁移失败:%w", err)
|
||||
}
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, "INSERT INTO schema_migrations (version) VALUES (1)"); err != nil {
|
||||
if _, err := db.ExecContext(ctx, "INSERT INTO schema_migrations (version) VALUES (?)", version); err != nil {
|
||||
return fmt.Errorf("记录迁移版本失败:%w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -62,7 +67,7 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
return fmt.Errorf("执行数据库迁移失败:%w", err)
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "INSERT INTO schema_migrations (version) VALUES (1)"); err != nil {
|
||||
if _, err := tx.ExecContext(ctx, "INSERT INTO schema_migrations (version) VALUES (?)", version); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return fmt.Errorf("记录迁移版本失败:%w", err)
|
||||
}
|
||||
@@ -72,6 +77,17 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrationName(version int) string {
|
||||
switch version {
|
||||
case 1:
|
||||
return "initial"
|
||||
case 2:
|
||||
return "domain_options"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func migrationStatements(content []byte) []string {
|
||||
statements := make([]string, 0)
|
||||
for _, statement := range strings.Split(string(content), ";") {
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE domains ADD COLUMN auto_renew BOOLEAN NOT NULL DEFAULT TRUE;
|
||||
ALTER TABLE domains ADD COLUMN acme_environment VARCHAR(32) NOT NULL DEFAULT 'staging';
|
||||
ALTER TABLE domains ADD COLUMN dns_provider VARCHAR(64) NOT NULL DEFAULT '';
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE domains ADD COLUMN auto_renew BOOLEAN NOT NULL DEFAULT 1;
|
||||
ALTER TABLE domains ADD COLUMN acme_environment TEXT NOT NULL DEFAULT 'staging';
|
||||
ALTER TABLE domains ADD COLUMN dns_provider TEXT NOT NULL DEFAULT '';
|
||||
@@ -52,7 +52,7 @@ func TestSQLiteMigrationIsIdempotentAndCreatesBaseTables(t *testing.T) {
|
||||
if err := db.DB.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&versions); err != nil {
|
||||
t.Fatalf("读取迁移版本失败:%v", err)
|
||||
}
|
||||
if versions != 1 {
|
||||
if versions != 2 {
|
||||
t.Fatalf("重复迁移写入了多条版本记录:%d", versions)
|
||||
}
|
||||
var tables int
|
||||
|
||||
Reference in new issue
Block a user