实现三种数据库迁移并完善认证与迁移写入保护
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-06 01:31:29 +08:00
1 parent d92dfdacff
commit f68c367042
34 files changed
+2121 -74

No files matched your search

+125
View File
@@ -0,0 +1,125 @@
package dbmigration
import (
"context"
"database/sql"
"net/http"
"strings"
"time"
)
// WritePaused 表示迁移已冻结源数据库,只有读取状态与重启可以继续。
func (s *Service) WritePaused() bool {
if s == nil {
return false
}
executor, ok := s.runner.(*DatabaseExecutor)
return ok && executor.portable != nil && executor.portable.paused.Load()
}
// GuardHTTP 等待在途 HTTP 请求及会话提交结束,再独占执行迁移。
// 放在会话中间件外层,避免迁移请求返回时才开始排空旧会话写入。
func GuardHTTP(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
service := Current()
if service == nil {
next.ServeHTTP(w, r)
return
}
service.requests.RLock()
lease := &requestLease{service: service}
defer lease.release()
r = r.WithContext(context.WithValue(r.Context(), requestLeaseKey{}, lease))
if service.WritePaused() && !maintenanceRequest(r) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Retry-After", "30")
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write([]byte(`{"errorNo":500,"errorMsg":"数据库迁移已暂停业务操作,请立即重启后端","data":"DATABASE_RESTART_REQUIRED"}`))
return
}
next.ServeHTTP(w, r)
})
}
type requestLeaseKey struct{}
type requestLease struct {
service *Service
exclusive bool
}
func (lease *requestLease) release() {
if lease.exclusive {
lease.service.requests.Unlock()
} else {
lease.service.requests.RUnlock()
}
}
// 只有权限、请求体与一次性票据均已验证,才排空请求并独占迁移。
// 独占状态由外层中间件持有到会话响应提交完成,不能在控制器返回前释放。
func (s *Service) drainRequests(ctx context.Context) func() {
if lease, ok := ctx.Value(requestLeaseKey{}).(*requestLease); ok && lease.service == s {
lease.service.requests.RUnlock()
lease.service.requests.Lock()
lease.exclusive = true
return func() {}
}
s.requests.Lock()
return s.requests.Unlock
}
func maintenanceRequest(r *http.Request) bool {
path := r.URL.Path
if r.Method == http.MethodGet {
if !strings.HasPrefix(path, "/api/") && !strings.HasPrefix(path, "/attachments/") {
return true
}
return path == "/api/ping" || path == "/api/v1/ping" ||
path == "/api/v1/config/status" || path == "/api/v1/config/base" ||
(strings.HasPrefix(path, "/api/v1/database/migrations/") && path != "/api/v1/database/migrations/preview")
}
return r.Method == http.MethodPost && (path == "/api/user/info" || path == "/api/v1/user/info" ||
path == "/api/v1/system/restart" || path == "/api/v1/system/restart/preview")
}
// WhileWritable 用于会话后台清理,与 HTTP 迁移执行共享排空边界。
func WhileWritable(action func() error) error {
service := Current()
if service == nil {
return action()
}
service.requests.RLock()
defer service.requests.RUnlock()
if service.WritePaused() {
return nil
}
return action()
}
func (s *Service) Close() {
if s == nil {
return
}
s.requests.Lock()
defer s.requests.Unlock()
if executor, ok := s.runner.(*DatabaseExecutor); ok && executor.portable != nil {
executor.portable.Close()
}
}
// WaitForDatabaseDrain 在关闭连接池后确认借出的连接全部归还。
// 等待失败时不能释放迁移源锁或继续内部重启,应终止进程并由外部重新启动。
func WaitForDatabaseDrain(ctx context.Context, database *sql.DB) error {
ticker := time.NewTicker(20 * time.Millisecond)
defer ticker.Stop()
for database != nil && database.Stats().InUse > 0 {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
return nil
}
@@ -0,0 +1,102 @@
package dbmigration
import (
"context"
"database/sql"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestDatabaseDrainWaitsForBorrowedConnectionAfterPoolClose(t *testing.T) {
database, err := sql.Open("sqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
defer database.Close()
connection, err := database.Conn(context.Background())
if err != nil {
t.Fatal(err)
}
defer connection.Close()
if err := database.Close(); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
defer cancel()
if err := WaitForDatabaseDrain(ctx, database); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("borrowed connection was not protected: %v", err)
}
if err := connection.Close(); err != nil {
t.Fatal(err)
}
if err := WaitForDatabaseDrain(context.Background(), database); err != nil {
t.Fatal(err)
}
}
func TestMaintenanceGuardDrainsRequestsAndKeepsRestartRoute(t *testing.T) {
executor := &PortableExecutor{}
service := NewService(&DatabaseExecutor{portable: executor})
previous := Current()
SetCurrent(service)
t.Cleanup(func() { SetCurrent(previous) })
entered, release, migrated := make(chan struct{}), make(chan struct{}), make(chan struct{})
handler := GuardHTTP(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/write":
close(entered)
<-release
case "/api/v1/database/migrations":
release := service.drainRequests(r.Context())
defer release()
executor.paused.Store(true)
close(migrated)
}
w.WriteHeader(http.StatusOK)
}))
done := make(chan struct{})
go func() {
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/write", nil))
close(done)
}()
<-entered
migrationDone := make(chan struct{})
go func() {
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/api/v1/database/migrations", nil))
close(migrationDone)
}()
select {
case <-migrated:
t.Error("migration did not drain request")
case <-time.After(20 * time.Millisecond):
}
close(release)
<-done
<-migrationDone
for _, test := range []struct {
method, path string
code int
}{
{http.MethodPost, "/api/v1/email/send", 503},
{http.MethodPost, "/api/v1/database/migrations", 503},
{http.MethodPut, "/api/v1/config/base", 503},
{http.MethodGet, "/api/v1/config/status", 200},
{http.MethodGet, "/api/v1/config/base", 200},
{http.MethodPost, "/api/v1/user/info", 200},
{http.MethodPost, "/api/v1/system/restart", 200},
{http.MethodPost, "/api/v1/system/restart/preview", 200},
} {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(test.method, test.path, nil))
if recorder.Code != test.code {
t.Errorf("%s: %d", test.path, recorder.Code)
}
}
called := false
if err := WhileWritable(func() error { called = true; return nil }); err != nil || called {
t.Fatal("cleanup wrote to frozen source")
}
}
@@ -0,0 +1,34 @@
package dbmigration
import (
"net/url"
"testing"
"git.pcux.cn/xuans/Smail/config"
"github.com/lib/pq"
)
func TestPortablePostgresUsesSupportedRuntimeTLSMode(t *testing.T) {
for _, name := range []string{"", "smail_next"} {
driver, dsn, err := runtimeSQLDSN(config.DatabaseConfig{
Type: config.DatabaseTypePostgreSQL, Host: "::1", Port: 5432, Username: "review",
}, "test-only-password", name)
if err != nil || driver != "postgres" {
t.Fatal("PostgreSQL connection was not built")
}
parsed, err := url.Parse(dsn)
if err != nil {
t.Fatal(err)
}
query := parsed.Query()
if query.Get("sslmode") != "require" || query.Get("timezone") != "UTC" || query.Get("search_path") != "public" {
t.Fatal("migration connection differs from supported runtime TLS or time settings")
}
if _, err := pq.NewConnector(dsn); err != nil {
t.Fatal("driver rejected generated connection parameters")
}
if name == "" && parsed.Path != "/postgres" {
t.Fatal("maintenance connection did not select postgres database")
}
}
}
@@ -0,0 +1,329 @@
package dbmigration
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/binary"
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/db/migration"
)
type portableQuery interface {
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}
func portableTables(ctx context.Context, database portableQuery, kind config.DatabaseType) ([]string, error) {
query := "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name"
if kind == config.DatabaseTypeMySQL {
query = "SELECT table_name FROM information_schema.tables WHERE table_schema=DATABASE() AND table_type='BASE TABLE' ORDER BY table_name"
}
if kind == config.DatabaseTypePostgreSQL {
query = "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE' ORDER BY table_name"
}
rows, err := database.QueryContext(ctx, query)
if err != nil {
return nil, ErrSQLConnectionUnavailable
}
defer rows.Close()
var tables []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return nil, ErrSQLUnsupportedObject
}
tables = append(tables, name)
}
if rows.Err() != nil || len(tables) == 0 {
return nil, ErrSQLUnsupportedObject
}
return tables, nil
}
func portableColumns(ctx context.Context, database portableQuery, kind config.DatabaseType, table string) ([]string, map[string]string, error) {
rows, err := database.QueryContext(ctx, "SELECT * FROM "+quoteDatabaseIdentifier(kind, table)+" WHERE 1=0")
if err != nil {
return nil, nil, ErrSQLUnsupportedObject
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, nil, err
}
types, err := rows.ColumnTypes()
if err != nil {
return nil, nil, err
}
result := make(map[string]string, len(types))
for _, column := range types {
result[column.Name()] = strings.ToUpper(column.DatabaseTypeName())
}
sort.Strings(columns)
return columns, result, nil
}
func validatePortableSchema(ctx context.Context, source portableQuery, target *sql.DB, plan Plan, tables []string) error {
targetTables, err := portableTables(ctx, target, plan.Target.Type)
if err != nil || !sameStringSet(tables, targetTables) {
return ErrSQLUnsupportedObject
}
rows, err := source.QueryContext(ctx, "SELECT version, checksum FROM schema_migration ORDER BY version")
if err != nil {
return ErrSQLUnsupportedObject
}
defer rows.Close()
definitions := migration.DefaultMigrations()
index := 0
for rows.Next() {
var version int64
var checksum string
if err := rows.Scan(&version, &checksum); err != nil || index >= len(definitions) || definitions[index].Version != version || !definitions[index].AcceptsChecksum(checksum) {
return ErrSQLUnsupportedObject
}
index++
}
if rows.Err() != nil || index != len(definitions) {
return ErrSQLUnsupportedObject
}
return nil
}
func copyPortableTables(ctx context.Context, source *sql.Conn, target *sql.DB, plan Plan, tables []string) error {
if err := validatePortableSchema(ctx, source, target, plan, tables); err != nil {
return err
}
transaction, err := target.BeginTx(ctx, nil)
if err != nil {
return ErrSQLConnectionUnavailable
}
defer transaction.Rollback()
for _, table := range tables {
if table == "schema_migration" {
continue
}
columns, _, err := portableColumns(ctx, source, plan.Source.Type, table)
if err != nil {
return err
}
targetColumns, types, err := portableColumns(ctx, transaction, plan.Target.Type, table)
if err != nil || !sameStringSet(columns, targetColumns) {
return ErrSQLUnsupportedObject
}
query := "INSERT INTO " + quoteDatabaseIdentifier(plan.Target.Type, table) + " (" + portableColumnList(plan.Target.Type, columns) + ") VALUES ("
placeholders := make([]string, len(columns))
for i := range columns {
placeholders[i] = "?"
if plan.Target.Type == config.DatabaseTypePostgreSQL {
placeholders[i] = fmt.Sprintf("$%d", i+1)
}
}
statement, err := transaction.PrepareContext(ctx, query+strings.Join(placeholders, ",")+")")
if err != nil {
return ErrSQLUnsupportedObject
}
err = visitPortableRows(ctx, source, plan.Source.Type, table, columns, func(values []any) error {
for i, value := range values {
converted, err := convertPortableValue(value, table, columns[i], types[columns[i]], plan.Target.Type)
if err != nil {
return err
}
values[i] = converted
}
if _, err := statement.ExecContext(ctx, values...); err != nil {
return ErrSQLDataVerification
}
return nil
})
_ = statement.Close()
if err != nil {
return err
}
if plan.Target.Type == config.DatabaseTypePostgreSQL && types["id"] != "" {
// 显式复制主键后必须推进序列,否则重启后的新增记录会冲突。
quoted := quotePostgreSQLIdentifier(table)
query := "SELECT setval(pg_get_serial_sequence($1, 'id'), COALESCE(MAX(id), 1), MAX(id) IS NOT NULL) FROM " + quoted
if _, err := transaction.ExecContext(ctx, query, quoted); err != nil {
return ErrSQLDataVerification
}
}
}
if err := transaction.Commit(); err != nil {
return ErrSQLDataVerification
}
return nil
}
func portableColumnList(kind config.DatabaseType, columns []string) string {
result := make([]string, len(columns))
for i, column := range columns {
result[i] = quoteDatabaseIdentifier(kind, column)
}
return strings.Join(result, ",")
}
func visitPortableRows(ctx context.Context, database portableQuery, kind config.DatabaseType, table string, columns []string, visit func([]any) error) error {
rows, err := database.QueryContext(ctx, "SELECT "+portableColumnList(kind, columns)+" FROM "+quoteDatabaseIdentifier(kind, table))
if err != nil {
return ErrSQLConnectionUnavailable
}
defer rows.Close()
for rows.Next() {
values, pointers := make([]any, len(columns)), make([]any, len(columns))
for i := range values {
pointers[i] = &values[i]
}
if err := rows.Scan(pointers...); err != nil {
return ErrSQLDataVerification
}
if err := visit(values); err != nil {
return err
}
}
if rows.Err() != nil {
return ErrSQLDataVerification
}
return nil
}
func convertPortableValue(value any, table, column, targetType string, target config.DatabaseType) (any, error) {
if value == nil {
return nil, nil
}
if table == "sessions" && column == "expiry" {
var instant time.Time
switch typed := value.(type) {
case time.Time:
instant = typed
case float64:
// sqlite3store 实际保存儒略日,而非 Unix 秒。
seconds, fraction := math.Modf((typed - 2440587.5) * 86400)
instant = time.Unix(int64(seconds), int64(fraction*1e9)).UTC()
case int64:
return nil, ErrSQLDataVerification
default:
return nil, ErrSQLDataVerification
}
if target == config.DatabaseTypeSQLite {
if original, ok := value.(float64); ok {
return original, nil
}
return 2440587.5 + (float64(instant.Unix())+float64(instant.Nanosecond())/1e9)/86400, nil
}
return instant.UTC().Truncate(time.Microsecond), nil
}
if bytes, ok := value.([]byte); ok && !strings.Contains(targetType, "BLOB") && targetType != "BYTEA" {
value = string(bytes)
}
if targetType == "BOOL" || targetType == "BOOLEAN" {
switch typed := value.(type) {
case bool:
return typed, nil
case int64:
if typed == 0 || typed == 1 {
return typed == 1, nil
}
default:
return nil, ErrSQLDataVerification
}
return nil, ErrSQLDataVerification
}
if boolean, ok := value.(bool); ok {
if boolean {
return int64(1), nil
}
return int64(0), nil
}
if instant, ok := value.(time.Time); ok {
return instant.UTC().Truncate(time.Microsecond), nil
}
if text, ok := value.(string); ok && (strings.Contains(targetType, "TIME") || targetType == "DATE") {
for _, layout := range []string{time.RFC3339Nano, "2006-01-02 15:04:05.999999999-07:00", "2006-01-02 15:04:05.999999999 -0700 MST", "2006-01-02 15:04:05.999999999", "2006-01-02"} {
if instant, err := time.Parse(layout, text); err == nil {
return instant.UTC().Truncate(time.Microsecond), nil
}
}
return nil, ErrSQLDataVerification
}
return value, nil
}
type portableDigest struct {
Count uint64
Sum [32]byte
}
func digestPortableTable(ctx context.Context, database portableQuery, kind config.DatabaseType, table string, columns []string, types map[string]string, target config.DatabaseType) (portableDigest, error) {
var digest portableDigest
err := visitPortableRows(ctx, database, kind, table, columns, func(values []any) error {
hash := sha256.New()
for i, value := range values {
value, err := convertPortableValue(value, table, columns[i], types[columns[i]], target)
if err != nil {
return err
}
var text string
switch typed := value.(type) {
case nil:
_, _ = hash.Write([]byte{0})
continue
case time.Time:
text = typed.UTC().Format(time.RFC3339Nano)
case []byte:
text = string(typed)
case bool:
text = strconv.FormatBool(typed)
case float64:
// 儒略日单位为天,六位小数会掩盖约八十六毫秒的差异。
text = strconv.FormatFloat(typed, 'g', -1, 64)
default:
text = fmt.Sprint(typed)
}
var length [8]byte
_, _ = hash.Write([]byte{1})
binary.BigEndian.PutUint64(length[:], uint64(len(text)))
_, _ = hash.Write(length[:])
_, _ = hash.Write([]byte(text))
}
row := hash.Sum(nil)
carry := uint16(0)
for i := 31; i >= 0; i-- {
carry += uint16(digest.Sum[i]) + uint16(row[i])
digest.Sum[i] = byte(carry)
carry >>= 8
}
digest.Count++
return nil
})
return digest, err
}
func verifyPortableTables(ctx context.Context, source *sql.Conn, target *sql.DB, plan Plan, tables []string) error {
for _, table := range tables {
if table == "schema_migration" {
continue
}
columns, types, err := portableColumns(ctx, target, plan.Target.Type, table)
if err != nil {
return err
}
before, err := digestPortableTable(ctx, source, plan.Source.Type, table, columns, types, plan.Target.Type)
if err != nil {
return err
}
after, err := digestPortableTable(ctx, target, plan.Target.Type, table, columns, types, plan.Target.Type)
if err != nil {
return err
}
if before != after {
return ErrSQLDataVerification
}
}
return nil
}
@@ -0,0 +1,282 @@
package dbmigration
import (
"context"
"database/sql"
"errors"
"os"
"strings"
"sync"
"sync/atomic"
"time"
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/db/migration"
)
// PortableExecutor 使用版本化目标结构复制业务数据,不拼接用户提供的 SQL。
// 成功切换后保留源库写锁直到进程重启,避免旧连接继续接收邮件造成数据丢失。
type PortableExecutor struct {
root string
open SQLConnectionFactory
switchConfig AuthorizedDatabaseSwitch
mu sync.Mutex
pending bool
paused atomic.Bool
releaseSource func()
}
func NewPortableDatabaseExecutor(root string, open SQLConnectionFactory, switchConfig AuthorizedDatabaseSwitch) Runner {
return &DatabaseExecutor{portable: &PortableExecutor{root: root, open: open, switchConfig: switchConfig}}
}
func (e *PortableExecutor) connect(ctx context.Context, database config.DatabaseConfig) (*sql.DB, error) {
if database.Type != config.DatabaseTypeSQLite {
if e.open == nil {
return nil, ErrSQLConnectionUnavailable
}
return e.open(ctx, database, database.Name)
}
path, err := NewSQLiteExecutor(e.root, nil).resolveReference(database.LocalRef)
if err != nil {
return nil, err
}
if err := checkRegularFile(path, false); err != nil {
return nil, ErrSourceDatabase
}
connection, err := sql.Open("sqlite", path+"?_pragma=busy_timeout(5000)")
if err != nil {
return nil, ErrSQLConnectionUnavailable
}
connection.SetMaxOpenConns(1)
return connection, nil
}
func (e *PortableExecutor) Preview(ctx context.Context, plan Plan) error {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
if _, err := buildTargetPlan(plan.sourceConfig, plan.targetConfig); err != nil {
return err
}
source, err := e.connect(ctx, plan.sourceConfig)
if err != nil {
return ErrSQLConnectionUnavailable
}
defer source.Close()
if err := source.PingContext(ctx); err != nil {
return ErrSQLConnectionUnavailable
}
return e.checkTarget(ctx, plan.targetConfig)
}
func (e *PortableExecutor) checkTarget(ctx context.Context, target config.DatabaseConfig) error {
if target.Type == config.DatabaseTypeSQLite {
path, err := NewSQLiteExecutor(e.root, nil).resolveReference(target.LocalRef)
if err != nil {
return err
}
return checkRegularFile(path, true)
}
if e.open == nil {
return ErrSQLConnectionUnavailable
}
admin, err := e.open(ctx, target, "")
if err != nil {
return ErrSQLConnectionUnavailable
}
defer admin.Close()
query := "SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = ?"
if target.Type == config.DatabaseTypePostgreSQL {
query = "SELECT COUNT(*) FROM pg_database WHERE datname = $1"
}
var count int
if err := admin.QueryRowContext(ctx, query, target.Name).Scan(&count); err != nil {
return ErrSQLConnectionUnavailable
}
if count != 0 {
return ErrSQLTargetExists
}
return nil
}
func (e *PortableExecutor) createTarget(ctx context.Context, target config.DatabaseConfig) (*sql.DB, error) {
if err := e.checkTarget(ctx, target); err != nil {
return nil, err
}
if target.Type == config.DatabaseTypeSQLite {
path, err := NewSQLiteExecutor(e.root, nil).resolveReference(target.LocalRef)
if err != nil {
return nil, err
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
if err != nil {
return nil, ErrTargetExists
}
if err := file.Close(); err != nil {
return nil, err
}
} else {
admin, err := e.open(ctx, target, "")
if err != nil {
return nil, ErrSQLConnectionUnavailable
}
defer admin.Close()
query := "CREATE DATABASE " + quoteDatabaseIdentifier(target.Type, target.Name)
if target.Type == config.DatabaseTypeMySQL {
query += " CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
}
if target.Type == config.DatabaseTypePostgreSQL {
query += " TEMPLATE template0 ENCODING 'UTF8'"
}
if _, err := admin.ExecContext(ctx, query); err != nil {
if isDatabaseAlreadyExistsError(err, target.Type) {
return nil, ErrSQLTargetExists
}
return nil, ErrSQLConnectionUnavailable
}
}
// 失败目标保留供检查,后续重试必须换用未占用的名称,绝不删除旧库。
return e.connect(ctx, target)
}
func (e *PortableExecutor) Run(ctx context.Context, plan Plan, update func(Status) error) error {
if !e.mu.TryLock() {
return ErrSQLSourceBusy
}
defer e.mu.Unlock()
if e.pending {
return ErrSQLSourceBusy
}
if e.switchConfig == nil || update == nil {
return ErrSwitchUnavailable
}
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
if err := e.Preview(ctx, plan); err != nil {
return err
}
source, err := e.connect(ctx, plan.sourceConfig)
if err != nil {
return ErrSQLConnectionUnavailable
}
keepLock := false
defer func() {
if !keepLock {
_ = source.Close()
}
}()
connection, err := source.Conn(ctx)
if err != nil {
return ErrSQLConnectionUnavailable
}
defer func() {
if !keepLock {
_ = connection.Close()
}
}()
tables, err := portableTables(ctx, connection, plan.Source.Type)
if err != nil {
return err
}
if err := validatePortableObjects(ctx, connection, plan.Source.Type); err != nil {
return err
}
e.paused.Store(true)
defer func() {
if !keepLock {
e.paused.Store(false)
}
}()
release, err := lockPortableSource(ctx, connection, plan.Source.Type, tables)
if err != nil {
return err
}
defer func() {
if !keepLock {
release()
}
}()
target, err := e.createTarget(ctx, plan.targetConfig)
if err != nil {
return err
}
defer target.Close()
dialect := migration.Dialect(plan.Target.Type)
if plan.Target.Type == config.DatabaseTypePostgreSQL {
dialect = migration.DialectPostgres
}
if _, err := (migration.Runner{DB: target, Dialect: dialect, Migrations: migration.DefaultMigrations()}).Run(ctx); err != nil {
return ErrSQLUnsupportedObject
}
if err := copyPortableTables(ctx, connection, target, plan, tables); err != nil {
return err
}
if err := update(StatusVerifying); err != nil {
return err
}
if err := verifyPortableTables(ctx, connection, target, plan, tables); err != nil {
return err
}
if err := update(StatusSwitching); err != nil {
return err
}
err = e.switchConfig(ctx, plan.expectedGeneration, plan.targetConfig, plan.actor)
if err != nil && isKnownSwitchRejection(err) {
return err
}
// 切换结果未知也保留写锁和目标,避免在不确定状态下继续写入旧库。
keepLock = true
e.pending = true
e.releaseSource = func() { release(); _ = connection.Close(); _ = source.Close() }
if err != nil {
return errors.Join(ErrSwitchOutcomeUnknown, err)
}
return nil
}
// Close 只能在请求和邮件监听器停止后调用,不能提前解除旧库保护。
func (e *PortableExecutor) Close() {
e.mu.Lock()
defer e.mu.Unlock()
if e.releaseSource != nil {
e.releaseSource()
e.releaseSource = nil
}
}
func quoteDatabaseIdentifier(kind config.DatabaseType, name string) string {
if kind == config.DatabaseTypeMySQL {
return quoteMySQLIdentifier(name)
}
return quotePostgreSQLIdentifier(name)
}
func lockPortableSource(ctx context.Context, connection *sql.Conn, kind config.DatabaseType, tables []string) (func(), error) {
query, unlock := "BEGIN IMMEDIATE", "ROLLBACK"
if kind == config.DatabaseTypeMySQL {
parts := make([]string, len(tables))
for i, table := range tables {
parts[i] = quoteMySQLIdentifier(table) + " READ"
}
query, unlock = "LOCK TABLES "+strings.Join(parts, ","), "UNLOCK TABLES"
} else if kind == config.DatabaseTypePostgreSQL {
if _, err := connection.ExecContext(ctx, "BEGIN"); err != nil {
return nil, ErrSQLSourceBusy
}
parts := make([]string, len(tables))
for i, table := range tables {
parts[i] = quotePostgreSQLIdentifier(table)
}
query = "LOCK TABLE " + strings.Join(parts, ",") + " IN SHARE MODE NOWAIT"
}
release := func() {
cleanup, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, _ = connection.ExecContext(cleanup, unlock)
}
if _, err := connection.ExecContext(ctx, query); err != nil {
release()
return nil, ErrSQLSourceBusy
}
return release, nil
}
@@ -0,0 +1,104 @@
package dbmigration
import (
"context"
"encoding/json"
"os"
"strings"
"testing"
"time"
"git.pcux.cn/xuans/Smail/config"
)
// 集成测试只创建随机命名的新库,不修改现有业务库,也不自动删除测试库。
// 必须显式提供专用测试服务连接;缺少连接时跳过,不冒充实库验收成功。
func TestPortableSQLIntegration(t *testing.T) {
for _, item := range []struct {
kind config.DatabaseType
env string
}{
{config.DatabaseTypeMySQL, "SMAIL_TEST_MIGRATION_MYSQL"},
{config.DatabaseTypePostgreSQL, "SMAIL_TEST_MIGRATION_POSTGRES"},
} {
t.Run(string(item.kind), func(t *testing.T) {
input := portableIntegrationInput(t, item.env, item.kind)
runPortableIntegrationChain(t, []TargetInput{input, input})
})
}
t.Run("mysql_postgresql", func(t *testing.T) {
mysql := portableIntegrationInput(t, "SMAIL_TEST_MIGRATION_MYSQL", config.DatabaseTypeMySQL)
postgres := portableIntegrationInput(t, "SMAIL_TEST_MIGRATION_POSTGRES", config.DatabaseTypePostgreSQL)
runPortableIntegrationChain(t, []TargetInput{mysql, postgres, mysql})
})
}
func portableIntegrationInput(t *testing.T, name string, kind config.DatabaseType) TargetInput {
t.Helper()
raw := strings.TrimSpace(os.Getenv(name))
if raw == "" {
t.Skip(name + " is not configured")
}
var input TargetInput
decoder := json.NewDecoder(strings.NewReader(raw))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&input); err != nil || input.Type != kind || input.Name != "" || input.LocalRef != "" {
t.Fatal("integration connection must specify the expected type without a database name or local path")
}
return input
}
func runPortableIntegrationChain(t *testing.T, inputs []TargetInput) {
t.Helper()
root, source := portableFixture(t)
open, err := NewRuntimeSQLConnectionFactory(root)
if err != nil {
t.Fatal("integration connection factory unavailable")
}
inputs = append(inputs, TargetInput{Type: config.DatabaseTypeSQLite, LocalRef: "roundtrip.db"})
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
for index, input := range inputs {
if input.Type != config.DatabaseTypeSQLite {
input.Name, err = randomID("smail_migration_test_")
if err != nil {
t.Fatal("integration name generation failed")
}
}
target, err := input.Prepare(root)
if err != nil {
t.Fatal("integration connection configuration is invalid")
}
plan, err := buildTargetPlan(source, target)
if err != nil {
t.Fatal("integration plan validation failed")
}
switched := false
executor := &PortableExecutor{root: root, open: open, switchConfig: func(context.Context, uint64, config.DatabaseConfig, config.AuditActor) error {
switched = true
return nil
}}
// 路径只记录随机测试库名,不记录连接账号、密码或底层驱动错误。
t.Logf("hop %d: %s -> %s, retained database: %s%s", index+1, source.Type, target.Type, target.Name, target.LocalRef)
if err := executor.Run(ctx, plan, func(Status) error { return nil }); err != nil {
executor.Close()
t.Fatalf("migration failed: %s", migrationErrorCode(err))
}
executor.Close()
if !switched {
t.Fatal("configuration switch was not reached")
}
connection, err := executor.connect(ctx, target)
if err != nil {
t.Fatal("migrated target could not be reopened")
}
var subject string
queryErr := connection.QueryRowContext(ctx, "SELECT subject FROM email WHERE id=82").Scan(&subject)
_, insertErr := connection.ExecContext(ctx, "INSERT INTO "+quoteDatabaseIdentifier(target.Type, "user")+" (account,name,password) VALUES ('next_hop_"+string(rune('a'+index))+"','sequence','test-only')")
_ = connection.Close()
if queryErr != nil || subject != "中文主题" || insertErr != nil {
t.Fatal("target content or identity sequence was not preserved")
}
source = target
}
}
@@ -0,0 +1,43 @@
package dbmigration
import (
"context"
"git.pcux.cn/xuans/Smail/config"
)
// 仅迁移 Smail 版本化业务表;不能静默忽略自定义对象。
func validatePortableObjects(ctx context.Context, database portableQuery, kind config.DatabaseType) error {
queries := []string{"SELECT COUNT(*) FROM sqlite_master WHERE type IN ('view','trigger')"}
switch kind {
case config.DatabaseTypeMySQL:
queries = []string{
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE() AND (table_type <> 'BASE TABLE' OR engine <> 'InnoDB')",
"SELECT COUNT(*) FROM information_schema.triggers WHERE trigger_schema=DATABASE()",
"SELECT COUNT(*) FROM information_schema.events WHERE event_schema=DATABASE()",
"SELECT COUNT(*) FROM information_schema.routines WHERE routine_schema=DATABASE()",
"SELECT COUNT(*) FROM information_schema.table_constraints WHERE constraint_schema=DATABASE() AND constraint_type='FOREIGN KEY'",
}
case config.DatabaseTypePostgreSQL:
queries = []string{
"SELECT COUNT(*) FROM pg_namespace WHERE nspname NOT IN ('public','information_schema') AND nspname NOT LIKE 'pg_%'",
"SELECT COUNT(*) FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND c.relkind NOT IN ('r','i','S')",
"SELECT COUNT(*) FROM pg_trigger t JOIN pg_class c ON c.oid=t.tgrelid JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='public' AND NOT t.tgisinternal",
"SELECT COUNT(*) FROM pg_proc p JOIN pg_namespace n ON n.oid=p.pronamespace WHERE n.nspname='public'",
"SELECT COUNT(*) FROM pg_constraint c JOIN pg_namespace n ON n.oid=c.connamespace WHERE n.nspname='public' AND c.contype IN ('f','c','x')",
}
}
for _, query := range queries {
rows, err := database.QueryContext(ctx, query)
if err != nil {
return ErrSQLUnsupportedObject
}
var count int
valid := rows.Next() && rows.Scan(&count) == nil && count == 0 && rows.Err() == nil
_ = rows.Close()
if !valid {
return ErrSQLUnsupportedObject
}
}
return nil
}
@@ -0,0 +1,211 @@
package dbmigration
import (
"context"
"database/sql"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/db/migration"
"git.pcux.cn/xuans/Smail/utils/credential"
)
func portableFixture(t *testing.T) (string, config.DatabaseConfig) {
t.Helper()
root := t.TempDir()
if err := os.Mkdir(filepath.Join(root, "config"), 0700); err != nil {
t.Fatal(err)
}
database, err := sql.Open("sqlite", filepath.Join(root, "config", "source.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
if _, err := (migration.Runner{DB: database, Dialect: migration.DialectSQLite, Migrations: migration.DefaultMigrations()}).Run(context.Background()); err != nil {
t.Fatal(err)
}
for _, query := range []string{
`INSERT INTO "user" (id,account,name,password) VALUES (40,'review','测试','hash')`,
`INSERT INTO email (id,subject,text,create_time) VALUES (82,'中文主题','正文😀','2026-09-05 12:30:00.123456')`,
`INSERT INTO sessions (token,data,expiry) VALUES ('token',x'0001ff',julianday('2026-09-12 12:00:00'))`,
`INSERT INTO plugin_user_setting (plugin_id,user_id,schema_version,settings_ciphertext,settings_hash,enabled,created_at,updated_at) VALUES ('test',40,1,'cipher','hash',1,'2026-09-05 12:00:00','2026-09-05 12:00:00')`,
} {
if _, err := database.Exec(query); err != nil {
t.Fatal(err)
}
}
return root, config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "source.db"}
}
func TestPortableSQLiteCopyPreservesDataAndLocksUntilRestart(t *testing.T) {
root, source := portableFixture(t)
var switched bool
executor := &PortableExecutor{root: root, switchConfig: func(context.Context, uint64, config.DatabaseConfig, config.AuditActor) error {
switched = true
return nil
}}
plan, err := buildTargetPlan(source, config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "target.db"})
if err != nil {
t.Fatal(err)
}
if err := executor.Preview(context.Background(), plan); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(root, "config", "target.db")); !os.IsNotExist(err) {
t.Fatal("preview created target")
}
if err := executor.Run(context.Background(), plan, func(Status) error { return nil }); err != nil {
t.Fatal(err)
}
defer executor.releaseSource()
if !switched || !executor.pending {
t.Fatal("switch or restart state missing")
}
target, err := executor.connect(context.Background(), plan.targetConfig)
if err != nil {
t.Fatal(err)
}
defer target.Close()
var subject string
if err := target.QueryRow("SELECT subject FROM email WHERE id=82").Scan(&subject); err != nil || subject != "中文主题" {
t.Fatal("content not preserved")
}
var expiry float64
if err := target.QueryRow("SELECT expiry FROM sessions").Scan(&expiry); err != nil || expiry < 2400000 || expiry > 2500000 {
t.Fatal("session Julian date not preserved")
}
if _, err := target.Exec(`INSERT INTO "user" (account,name,password) VALUES ('next','新增','hash')`); err != nil {
t.Fatal(err)
}
var next int
if err := target.QueryRow(`SELECT id FROM "user" WHERE account='next'`).Scan(&next); err != nil || next <= 40 {
t.Fatal("identity sequence not advanced")
}
old, err := sql.Open("sqlite", filepath.Join(root, "config", "source.db")+"?_pragma=busy_timeout(1)")
if err != nil {
t.Fatal(err)
}
defer old.Close()
if _, err := old.Exec(`UPDATE "user" SET name='bad'`); err == nil {
t.Fatal("old source accepted writes after switch")
}
}
func TestPortableSwitchFailurePreservesSourceAndTarget(t *testing.T) {
root, source := portableFixture(t)
executor := &PortableExecutor{root: root, switchConfig: func(context.Context, uint64, config.DatabaseConfig, config.AuditActor) error {
return config.ErrGenerationConflict
}}
plan, err := buildTargetPlan(source, config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "target.db"})
if err != nil {
t.Fatal(err)
}
if err := executor.Run(context.Background(), plan, func(Status) error { return nil }); !errors.Is(err, config.ErrGenerationConflict) {
t.Fatalf("switch failure: %v", err)
}
if executor.pending {
t.Fatal("rejected switch kept write lock")
}
for _, name := range []string{"source.db", "target.db"} {
if _, err := os.Stat(filepath.Join(root, "config", name)); err != nil {
t.Fatal("database removed")
}
}
if err := executor.Preview(context.Background(), plan); !errors.Is(err, ErrTargetExists) {
t.Fatal("existing target accepted")
}
}
func TestTargetSupportsAllDatabasePairsWithoutSecretDisclosure(t *testing.T) {
root := t.TempDir()
inputs := []TargetInput{
{Type: config.DatabaseTypeSQLite, LocalRef: "next.db"},
{Type: config.DatabaseTypeMySQL, Host: "localhost", Username: "test", Name: "next", Password: &config.SecretInput{Plaintext: "private-test-value"}},
{Type: config.DatabaseTypePostgreSQL, Host: "localhost", Username: "test", Name: "next", Password: &config.SecretInput{Plaintext: "private-test-value"}},
}
for _, input := range inputs {
target, err := input.Prepare(root)
if err != nil {
t.Fatal(err)
}
for _, sourceInput := range inputs {
sourceInput.Name = ""
if sourceInput.Type == config.DatabaseTypeSQLite {
sourceInput.LocalRef = "source.db"
} else {
sourceInput.Name = "source"
}
source, err := sourceInput.Prepare(root)
if err != nil {
t.Fatal(err)
}
service := NewService(testRunner{})
record, _, err := service.PreviewTarget(context.Background(), source, 1, target)
if err != nil {
t.Fatal(err)
}
data, _ := json.Marshal(record)
if strings.Contains(string(data), "password") || strings.Contains(string(data), "private-test-value") || strings.Contains(string(data), "ciphertext") {
t.Fatal("preview leaked password")
}
}
if target.Type != config.DatabaseTypeSQLite {
vault, _ := credential.NewVault(root)
value, err := config.OpenStoredSecret(vault, target.Password)
if err != nil || value != "private-test-value" {
t.Fatal("target secret did not roundtrip")
}
}
}
}
func TestPortableConversions(t *testing.T) {
now := time.Date(2026, 9, 5, 12, 0, 0, 0, time.UTC)
julian, err := convertPortableValue(now, "sessions", "expiry", "REAL", config.DatabaseTypeSQLite)
if err != nil {
t.Fatal(err)
}
restored, err := convertPortableValue(julian, "sessions", "expiry", "TIMESTAMPTZ", config.DatabaseTypePostgreSQL)
if err != nil || restored.(time.Time).Sub(now).Abs() > time.Millisecond {
t.Fatal("session expiry did not roundtrip")
}
boolean, err := convertPortableValue(int64(1), "plugin_user_setting", "enabled", "BOOL", config.DatabaseTypePostgreSQL)
if err != nil || boolean != true {
t.Fatal("boolean conversion failed")
}
if _, err := convertPortableValue(int64(2), "plugin_user_setting", "enabled", "BOOL", config.DatabaseTypePostgreSQL); err == nil {
t.Fatal("invalid boolean accepted")
}
}
func TestPortableRejectsCustomObjectsBeforeCreatingTarget(t *testing.T) {
root, source := portableFixture(t)
executor := &PortableExecutor{root: root, switchConfig: func(context.Context, uint64, config.DatabaseConfig, config.AuditActor) error {
t.Error("unsupported schema switched configuration")
return nil
}}
database, err := executor.connect(context.Background(), source)
if err != nil {
t.Fatal(err)
}
if _, err := database.Exec(`CREATE VIEW custom_view AS SELECT id FROM "user"`); err != nil {
t.Fatal(err)
}
_ = database.Close()
plan, err := buildTargetPlan(source, config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "target.db"})
if err != nil {
t.Fatal(err)
}
if err := executor.Run(context.Background(), plan, func(Status) error { return nil }); !errors.Is(err, ErrSQLUnsupportedObject) {
t.Fatalf("custom view accepted: %v", err)
}
if _, err := os.Stat(filepath.Join(root, "config", "target.db")); !os.IsNotExist(err) {
t.Fatal("unsupported source created target")
}
}
+30 -10
View File
@@ -81,10 +81,11 @@ type Runner interface {
}
type Service struct {
mu sync.RWMutex
records map[string]*record
runner Runner
now func() time.Time
requests sync.RWMutex
mu sync.RWMutex
records map[string]*record
runner Runner
now func() time.Time
}
type record struct {
@@ -117,6 +118,15 @@ func Current() *Service {
}
func (s *Service) Preview(ctx context.Context, source config.DatabaseConfig, currentGeneration uint64, targetName string) (Record, string, error) {
plan, err := buildPlan(source, strings.TrimSpace(targetName))
if err != nil {
return Record{}, "", err
}
return s.PreviewTarget(ctx, source, currentGeneration, plan.targetConfig)
}
// PreviewTarget 固定完整目标连接;返回记录不包含密码或密文。
func (s *Service) PreviewTarget(ctx context.Context, source config.DatabaseConfig, currentGeneration uint64, target config.DatabaseConfig) (Record, string, error) {
if err := contextError(ctx); err != nil {
return Record{}, "", err
}
@@ -126,14 +136,17 @@ func (s *Service) Preview(ctx context.Context, source config.DatabaseConfig, cur
if err := config.ValidateDatabaseConfig(source); err != nil {
return Record{}, "", ErrSourceDatabase
}
targetName = strings.TrimSpace(targetName)
if (source.Type == config.DatabaseTypeSQLite && !sqliteTargetNamePattern.MatchString(targetName)) || (source.Type != config.DatabaseTypeSQLite && !targetNamePattern.MatchString(targetName)) {
return Record{}, "", ErrInvalidTargetName
}
plan, err := buildPlan(source, targetName)
plan, err := buildTargetPlan(source, target)
if err != nil {
return Record{}, "", err
}
if checker, ok := s.runner.(interface {
Preview(context.Context, Plan) error
}); ok {
if err := checker.Preview(ctx, plan); err != nil {
return Record{}, "", err
}
}
id, err := randomID("dbmig-")
if err != nil {
return Record{}, "", ErrInvalidRequest
@@ -189,8 +202,11 @@ func (s *Service) execute(ctx context.Context, id, token string, actor config.Au
item.Status = StatusFailed
item.ErrorCode = "PREVIEW_EXPIRED"
item.UpdatedAt = now
item.sourceConfig = config.DatabaseConfig{}
item.targetConfig = config.DatabaseConfig{}
snapshot := cloneRecord(item.Record)
s.mu.Unlock()
return cloneRecord(item.Record), ErrPreviewExpired
return snapshot, ErrPreviewExpired
}
hash := sha256.Sum256([]byte(token))
if subtle.ConstantTimeCompare(hash[:], item.tokenHash[:]) != 1 {
@@ -201,8 +217,12 @@ func (s *Service) execute(ctx context.Context, id, token string, actor config.Au
item.Status = StatusCreated
item.UpdatedAt = now
plan := Plan{Source: item.Source, Target: item.Target, sourceConfig: item.sourceConfig, targetConfig: item.targetConfig, expectedGeneration: item.CurrentGeneration, actor: actor}
item.sourceConfig = config.DatabaseConfig{}
item.targetConfig = config.DatabaseConfig{}
s.mu.Unlock()
releaseRequests := s.drainRequests(ctx)
defer releaseRequests()
if err := s.setStatus(id, StatusCopying); err != nil {
return s.fail(id, err)
}
+21 -12
View File
@@ -44,6 +44,7 @@ type StructuredDatabaseCleanup interface {
type DatabaseExecutor struct {
sqlite Runner
structured Runner
portable *PortableExecutor
}
func NewDatabaseExecutor(sqlite, structured Runner) Runner {
@@ -54,6 +55,9 @@ func (e *DatabaseExecutor) Run(ctx context.Context, plan Plan, update func(Statu
if e == nil {
return ErrExecutorUnavailable
}
if e.portable != nil {
return e.portable.Run(ctx, plan, update)
}
if plan.sourceConfig.Type == config.DatabaseTypeSQLite {
if e.sqlite == nil {
return ErrExecutorUnavailable
@@ -883,13 +887,17 @@ func runtimeSQLDSN(database config.DatabaseConfig, password, databaseName string
switch database.Type {
case config.DatabaseTypeMySQL:
settings := mysql.Config{
User: database.Username,
Passwd: password,
Net: "tcp",
Addr: address,
DBName: databaseName,
ParseTime: true,
Params: map[string]string{"charset": "utf8mb4"},
User: database.Username,
Passwd: password,
Net: "tcp",
Addr: address,
DBName: databaseName,
ParseTime: true,
AllowNativePasswords: true,
Timeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Params: map[string]string{"charset": "utf8mb4", "time_zone": "'+00:00'", "sql_mode": "'STRICT_ALL_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE'"},
}
return "mysql", settings.FormatDSN(), nil
case config.DatabaseTypePostgreSQL:
@@ -898,11 +906,12 @@ func runtimeSQLDSN(database config.DatabaseConfig, password, databaseName string
maintenanceDatabase = "postgres"
}
databaseURL := url.URL{
Scheme: "postgres",
User: url.UserPassword(database.Username, password),
Host: address,
Path: "/" + maintenanceDatabase,
RawQuery: "sslmode=prefer",
Scheme: "postgres",
User: url.UserPassword(database.Username, password),
Host: address,
Path: "/" + maintenanceDatabase,
// 与分区配置运行连接的默认模式一致;lib/pq 不支持 prefer。
RawQuery: "sslmode=require&connect_timeout=10&timezone=UTC&search_path=public",
}
return "postgres", databaseURL.String(), nil
default:
+85
View File
@@ -0,0 +1,85 @@
package dbmigration
import (
"context"
"strings"
"time"
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/utils/credential"
)
// TargetInput 只接受结构化连接字段,不接受任意 DSN、SQL 或密码密文。
type TargetInput struct {
Type config.DatabaseType `json:"type"`
Host string `json:"host,omitempty"`
Port uint16 `json:"port,omitempty"`
Username string `json:"username,omitempty"`
Name string `json:"name,omitempty"`
LocalRef string `json:"localRef,omitempty"`
Password *config.SecretInput `json:"password,omitempty"`
}
func (input TargetInput) Prepare(root string) (config.DatabaseConfig, error) {
target := config.NormalizeDatabaseConfig(config.DatabaseConfig{
Type: input.Type, Host: strings.TrimSpace(input.Host), Port: input.Port,
Username: strings.TrimSpace(input.Username), Name: strings.TrimSpace(input.Name),
LocalRef: strings.TrimSpace(input.LocalRef),
})
if target.Type == config.DatabaseTypeSQLite {
if input.Password != nil {
return config.DatabaseConfig{}, ErrInvalidRequest
}
} else if target.Type.Valid() {
vault, err := credential.NewVault(root)
if err != nil {
return config.DatabaseConfig{}, config.ErrSecretUnavailable
}
secret, err := config.ApplySecretChange(vault, config.StoredSecret{}, config.SecretChange{
Operation: config.SecretReplace, Value: input.Password,
}, config.SecretPolicy{Required: true, MaxLength: 4096}, time.Now().UTC())
if err != nil {
return config.DatabaseConfig{}, ErrInvalidRequest
}
target.Password = secret
}
if err := config.ValidateDatabaseConfig(target); err != nil {
return config.DatabaseConfig{}, ErrInvalidRequest
}
return target, nil
}
func databaseIdentity(database config.DatabaseConfig) DatabaseIdentity {
return DatabaseIdentity{Type: database.Type, Host: database.Host, Port: database.Port,
Username: database.Username, Name: database.Name, LocalRef: database.LocalRef}
}
func buildTargetPlan(source, target config.DatabaseConfig) (Plan, error) {
source = config.NormalizeDatabaseConfig(source)
target = config.NormalizeDatabaseConfig(target)
if err := config.ValidateDatabaseConfig(source); err != nil {
return Plan{}, ErrSourceDatabase
}
if err := config.ValidateDatabaseConfig(target); err != nil {
return Plan{}, ErrInvalidRequest
}
if target.Type == config.DatabaseTypeSQLite {
if !sqliteTargetNamePattern.MatchString(target.LocalRef) {
return Plan{}, ErrInvalidTargetName
}
} else if !targetNamePattern.MatchString(target.Name) {
return Plan{}, ErrInvalidTargetName
}
if config.SameDatabaseLocation(source, target) {
return Plan{}, ErrInvalidTargetName
}
return Plan{Source: databaseIdentity(source), Target: databaseIdentity(target), sourceConfig: source, targetConfig: target}, nil
}
// Preview 不创建目标库,只验证源库可读、目标服务可连接且名称未占用。
func (e *DatabaseExecutor) Preview(ctx context.Context, plan Plan) error {
if e.portable != nil {
return e.portable.Preview(ctx, plan)
}
return nil
}