Stage 1, step 1. BREAKING: a 1.x database is not migrated.
Schema
- internal/store/migrations/ holds one file, 0001_init.sql, equal to the
2.0 schema; 0002..0010 are deleted (they stay in history at 1.9.5). No
admin table created and dropped, no ALTERs.
- users.email replaces users.dmarc_report_email: the address is the
user's own. The default DMARC report address is a user's setting
(dmarc_default_mode hosted/account/custom/none, dmarc_default_address);
the instance setting that mirrored the global user's field is gone.
- domains.dmarc_rua_user_id: a domain follows a named user's default, or
has an address of its own in dmarc_rua ('' = none). ON DELETE SET NULL
leaves it at "no reports", never at someone else's default.
- user_inbound_domains, users.all_domains, users.all_inbound_domains: in
the schema for the inbound delegation of the next step.
- users.role is 'global' / 'domain' (was 'domain_admin');
rate_limits.allowed_ips, unused since 1.9.0, is dropped.
Opening a database
- The baseline sets PRAGMA application_id ("SP20"). store.Open refuses a
file that has a schema version without it (ErrForeignSchema) and one
newer than the build. A 1.x file at user_version 10 would otherwise read
as already migrated. The refused file is left untouched.
Code
- Store.DomainDMARCRua is the one place a domain's report address is
resolved; dnscheck.ResolveDMARCRua and Store.GlobalDMARCReportEmail are
gone. dmarc.AllowedRecipients, the domain page, the dashboard and the
DNS re-check go through it.
- The old templates keep working on the new model until their pages are
restyled: the Settings form's address field is the account e-mail and
switches the user's default between it and none; a domain's "Same as
Settings" makes it follow the user who chose it; a new or imported
domain follows its creator.
- Domain export/import: a domain that follows a user exports no address.
Tests: the baseline pinned table by table (TestBaselineSchema), refusal of a
1.x and of a newer database, constraints and cascades of the new columns and
table, resolution in every default mode, the deleted-user case, and the three
bridged handlers.
docs/schema-migrations.md is rewritten for the baseline; roadmap 11/27,
schema-squash marked built.
go test ./... (Windows)
ok for every package except two that fail the same way before this commit:
internal/domain TestWriteLoadPrivateKeyRoundtrip (file mode on NTFS)
internal/logtail TestFollowTailsAndRotates (rename of an open file)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
4.1 KiB
Go
128 lines
4.1 KiB
Go
// Package store owns the SelfPost SQLite database: the single file under /data
|
|
// that persists panel users (global administrators and domain-admins), sending
|
|
// domains and applications, optional inbound-relay domains, the send log and
|
|
// rate-limit settings (architecture.md § Persistence). It exposes typed queries
|
|
// so the rest of the panel never builds SQL by hand.
|
|
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"embed"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"sort"
|
|
|
|
_ "modernc.org/sqlite" // pure-Go SQLite driver (no cgo), keeps the static build
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var migrationsFS embed.FS
|
|
|
|
// Store wraps the database connection pool.
|
|
type Store struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
// Open opens (creating if needed) the SQLite database at path, enables WAL and
|
|
// foreign keys, and applies any pending migrations. The caller owns Close.
|
|
func Open(path string) (*Store, error) {
|
|
// _pragma parameters are applied on every pooled connection by the driver,
|
|
// so foreign-key enforcement and WAL survive connection churn.
|
|
dsn := fmt.Sprintf("file:%s?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)&_pragma=foreign_keys(ON)", path)
|
|
db, err := sql.Open("sqlite", dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open database: %w", err)
|
|
}
|
|
// modernc's driver serializes writes anyway; a small pool avoids
|
|
// "database is locked" surprises under WAL.
|
|
db.SetMaxOpenConns(1)
|
|
|
|
s := &Store{db: db}
|
|
if err := s.migrate(); err != nil {
|
|
db.Close()
|
|
return nil, err
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// Close closes the underlying database.
|
|
func (s *Store) Close() error {
|
|
return s.db.Close()
|
|
}
|
|
|
|
// applicationID marks a database file as a 2.x SelfPost one ("SP20"). The
|
|
// baseline migration sets it.
|
|
const applicationID = 0x53503230
|
|
|
|
// ErrForeignSchema is returned by Open for a database that has a schema but
|
|
// was not created by SelfPost 2.x — in practice a 1.x data directory.
|
|
var ErrForeignSchema = errors.New("database was not created by SelfPost 2.x; 2.0 starts from an empty data directory (docs/schema-migrations.md)")
|
|
|
|
// migrate applies embedded migrations in filename order, tracking progress via
|
|
// SQLite's PRAGMA user_version so each migration runs at most once.
|
|
func (s *Store) migrate() error {
|
|
entries, err := fs.ReadDir(migrationsFS, "migrations")
|
|
if err != nil {
|
|
return fmt.Errorf("read migrations: %w", err)
|
|
}
|
|
names := make([]string, 0, len(entries))
|
|
for _, e := range entries {
|
|
if !e.IsDir() {
|
|
names = append(names, e.Name())
|
|
}
|
|
}
|
|
sort.Strings(names)
|
|
|
|
var version int
|
|
if err := s.db.QueryRow("PRAGMA user_version").Scan(&version); err != nil {
|
|
return fmt.Errorf("read schema version: %w", err)
|
|
}
|
|
|
|
// A 1.x file carries a user_version from the deleted 1.x chain. Counting it
|
|
// against today's files would skip the baseline and leave the panel running
|
|
// on a schema it does not know, so such a file is refused outright; nothing
|
|
// is migrated from 1.x (docs/schema-migrations.md).
|
|
if version > 0 {
|
|
var appID int64
|
|
if err := s.db.QueryRow("PRAGMA application_id").Scan(&appID); err != nil {
|
|
return fmt.Errorf("read application id: %w", err)
|
|
}
|
|
if appID != applicationID {
|
|
return fmt.Errorf("schema version %d: %w", version, ErrForeignSchema)
|
|
}
|
|
}
|
|
if version > len(names) {
|
|
return fmt.Errorf("database schema version %d is newer than this build knows (%d)", version, len(names))
|
|
}
|
|
|
|
for i, name := range names {
|
|
target := i + 1
|
|
if target <= version {
|
|
continue
|
|
}
|
|
sqlBytes, err := migrationsFS.ReadFile("migrations/" + name)
|
|
if err != nil {
|
|
return fmt.Errorf("read migration %s: %w", name, err)
|
|
}
|
|
tx, err := s.db.Begin()
|
|
if err != nil {
|
|
return fmt.Errorf("begin migration %s: %w", name, err)
|
|
}
|
|
if _, err := tx.Exec(string(sqlBytes)); err != nil {
|
|
tx.Rollback()
|
|
return fmt.Errorf("apply migration %s: %w", name, err)
|
|
}
|
|
// PRAGMA does not accept a bound parameter, and target is a trusted
|
|
// loop index, so formatting it in is safe.
|
|
if _, err := tx.Exec(fmt.Sprintf("PRAGMA user_version = %d", target)); err != nil {
|
|
tx.Rollback()
|
|
return fmt.Errorf("bump schema version for %s: %w", name, err)
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return fmt.Errorf("commit migration %s: %w", name, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|