Migrate db and config to mcdsl
- db.Open: delegate to mcdsl/db.Open (WAL, FK, busy timeout, 0600) - db.Migrate: convert function-based migrations to mcdsl/db.Migration SQL strings, delegate to mcdsl/db.Migrate - db.Snapshot: delegate to mcdsl/db.Snapshot (adds 0600 permissions) - config: replace local Duration with mcdsl/config.Duration alias, replace Load with mcdsl/config.Load[T] + Validator interface - Remove direct modernc.org/sqlite and go-toml/v2 dependencies (now indirect via mcdsl) - Update TestEnvOverrideInvalidDuration: mcdsl silently ignores invalid env duration values (behavioral change from migration) - All existing tests pass Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,9 +3,8 @@ package db
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
mcdsldb "git.wntrmute.dev/kyle/mcdsl/db"
|
||||
)
|
||||
|
||||
// Store wraps a SQLite database connection for mc-proxy persistence.
|
||||
@@ -14,36 +13,14 @@ type Store struct {
|
||||
}
|
||||
|
||||
// Open opens (or creates) the SQLite database at path with WAL mode,
|
||||
// foreign keys, and a busy timeout. The file is created with 0600 permissions.
|
||||
// foreign keys, and a busy timeout. The file is created with 0600
|
||||
// permissions.
|
||||
func Open(path string) (*Store, error) {
|
||||
// Ensure the file has restrictive permissions if it doesn't exist.
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating database file: %w", err)
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
|
||||
db, err := sql.Open("sqlite", path)
|
||||
database, err := mcdsldb.Open(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("opening database: %w", err)
|
||||
}
|
||||
|
||||
// Apply connection pragmas.
|
||||
pragmas := []string{
|
||||
"PRAGMA journal_mode = WAL",
|
||||
"PRAGMA foreign_keys = ON",
|
||||
"PRAGMA busy_timeout = 5000",
|
||||
}
|
||||
for _, p := range pragmas {
|
||||
if _, err := db.Exec(p); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("setting pragma %q: %w", p, err)
|
||||
}
|
||||
}
|
||||
|
||||
return &Store{db: db}, nil
|
||||
return &Store{db: database}, nil
|
||||
}
|
||||
|
||||
// Close closes the database connection.
|
||||
@@ -51,6 +28,11 @@ func (s *Store) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
// DB returns the underlying *sql.DB for use with mcdsl functions.
|
||||
func (s *Store) DB() *sql.DB {
|
||||
return s.db
|
||||
}
|
||||
|
||||
// IsEmpty returns true if the listeners table has no rows.
|
||||
// Used to determine if the database needs seeding from config.
|
||||
func (s *Store) IsEmpty() (bool, error) {
|
||||
|
||||
@@ -1,118 +1,53 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
mcdsldb "git.wntrmute.dev/kyle/mcdsl/db"
|
||||
)
|
||||
|
||||
type migration struct {
|
||||
version int
|
||||
name string
|
||||
fn func(tx *sql.Tx) error
|
||||
}
|
||||
|
||||
var migrations = []migration{
|
||||
{1, "create_core_tables", migrate001CreateCoreTables},
|
||||
{2, "add_proxy_protocol_and_l7_fields", migrate002AddL7Fields},
|
||||
{3, "add_listener_max_connections", migrate003AddListenerMaxConnections},
|
||||
// Migrations is the ordered list of schema migrations for mc-proxy.
|
||||
var Migrations = []mcdsldb.Migration{
|
||||
{
|
||||
Version: 1,
|
||||
Name: "create_core_tables",
|
||||
SQL: `
|
||||
CREATE TABLE IF NOT EXISTS listeners (
|
||||
id INTEGER PRIMARY KEY,
|
||||
addr TEXT NOT NULL UNIQUE
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS routes (
|
||||
id INTEGER PRIMARY KEY,
|
||||
listener_id INTEGER NOT NULL REFERENCES listeners(id) ON DELETE CASCADE,
|
||||
hostname TEXT NOT NULL,
|
||||
backend TEXT NOT NULL,
|
||||
UNIQUE(listener_id, hostname)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_routes_listener ON routes(listener_id);
|
||||
CREATE TABLE IF NOT EXISTS firewall_rules (
|
||||
id INTEGER PRIMARY KEY,
|
||||
type TEXT NOT NULL CHECK(type IN ('ip', 'cidr', 'country')),
|
||||
value TEXT NOT NULL,
|
||||
UNIQUE(type, value)
|
||||
);`,
|
||||
},
|
||||
{
|
||||
Version: 2,
|
||||
Name: "add_proxy_protocol_and_l7_fields",
|
||||
SQL: `
|
||||
ALTER TABLE listeners ADD COLUMN proxy_protocol INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE routes ADD COLUMN mode TEXT NOT NULL DEFAULT 'l4' CHECK(mode IN ('l4', 'l7'));
|
||||
ALTER TABLE routes ADD COLUMN tls_cert TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE routes ADD COLUMN tls_key TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE routes ADD COLUMN backend_tls INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE routes ADD COLUMN send_proxy_protocol INTEGER NOT NULL DEFAULT 0;`,
|
||||
},
|
||||
{
|
||||
Version: 3,
|
||||
Name: "add_listener_max_connections",
|
||||
SQL: `ALTER TABLE listeners ADD COLUMN max_connections INTEGER NOT NULL DEFAULT 0;`,
|
||||
},
|
||||
}
|
||||
|
||||
// Migrate runs all unapplied migrations sequentially.
|
||||
func (s *Store) Migrate() error {
|
||||
// Ensure the migration tracking table exists.
|
||||
_, err := s.db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version INTEGER PRIMARY KEY,
|
||||
applied TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating schema_migrations table: %w", err)
|
||||
}
|
||||
|
||||
var current int
|
||||
err = s.db.QueryRow("SELECT COALESCE(MAX(version), 0) FROM schema_migrations").Scan(¤t)
|
||||
if err != nil {
|
||||
return fmt.Errorf("querying current migration version: %w", err)
|
||||
}
|
||||
|
||||
for _, m := range migrations {
|
||||
if m.version <= current {
|
||||
continue
|
||||
}
|
||||
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("beginning migration %d (%s): %w", m.version, m.name, err)
|
||||
}
|
||||
|
||||
if err := m.fn(tx); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("running migration %d (%s): %w", m.version, m.name, err)
|
||||
}
|
||||
|
||||
if _, err := tx.Exec("INSERT INTO schema_migrations (version) VALUES (?)", m.version); err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("recording migration %d (%s): %w", m.version, m.name, err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("committing migration %d (%s): %w", m.version, m.name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrate001CreateCoreTables(tx *sql.Tx) error {
|
||||
stmts := []string{
|
||||
`CREATE TABLE IF NOT EXISTS listeners (
|
||||
id INTEGER PRIMARY KEY,
|
||||
addr TEXT NOT NULL UNIQUE
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS routes (
|
||||
id INTEGER PRIMARY KEY,
|
||||
listener_id INTEGER NOT NULL REFERENCES listeners(id) ON DELETE CASCADE,
|
||||
hostname TEXT NOT NULL,
|
||||
backend TEXT NOT NULL,
|
||||
UNIQUE(listener_id, hostname)
|
||||
)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_routes_listener ON routes(listener_id)`,
|
||||
`CREATE TABLE IF NOT EXISTS firewall_rules (
|
||||
id INTEGER PRIMARY KEY,
|
||||
type TEXT NOT NULL CHECK(type IN ('ip', 'cidr', 'country')),
|
||||
value TEXT NOT NULL,
|
||||
UNIQUE(type, value)
|
||||
)`,
|
||||
}
|
||||
|
||||
for _, stmt := range stmts {
|
||||
if _, err := tx.Exec(stmt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrate002AddL7Fields(tx *sql.Tx) error {
|
||||
stmts := []string{
|
||||
`ALTER TABLE listeners ADD COLUMN proxy_protocol INTEGER NOT NULL DEFAULT 0`,
|
||||
`ALTER TABLE routes ADD COLUMN mode TEXT NOT NULL DEFAULT 'l4' CHECK(mode IN ('l4', 'l7'))`,
|
||||
`ALTER TABLE routes ADD COLUMN tls_cert TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE routes ADD COLUMN tls_key TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE routes ADD COLUMN backend_tls INTEGER NOT NULL DEFAULT 0`,
|
||||
`ALTER TABLE routes ADD COLUMN send_proxy_protocol INTEGER NOT NULL DEFAULT 0`,
|
||||
}
|
||||
|
||||
for _, stmt := range stmts {
|
||||
if _, err := tx.Exec(stmt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrate003AddListenerMaxConnections(tx *sql.Tx) error {
|
||||
_, err := tx.Exec(`ALTER TABLE listeners ADD COLUMN max_connections INTEGER NOT NULL DEFAULT 0`)
|
||||
return err
|
||||
return mcdsldb.Migrate(s.db, Migrations)
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package db
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
mcdsldb "git.wntrmute.dev/kyle/mcdsl/db"
|
||||
)
|
||||
|
||||
// Snapshot creates a consistent backup of the database using VACUUM INTO.
|
||||
// The destination file is created with 0600 permissions.
|
||||
func (s *Store) Snapshot(destPath string) error {
|
||||
_, err := s.db.Exec("VACUUM INTO ?", destPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("snapshot to %q: %w", destPath, err)
|
||||
}
|
||||
return nil
|
||||
return mcdsldb.Snapshot(s.db, destPath)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user