| 1 |
// Package schema tracks and migrates database schemas, and Migrate creates or upgrades a |
| 2 |
// store's schema inside a single transaction. On PostgreSQL, all stores share one database, so |
| 3 |
// versions live in a shared schema_version table keyed by store name. On SQLite, every store is |
| 4 |
// its own database file, so the version lives in the schemaVersion table keyed by id = 1. |
| 5 |
package schema |
| 6 |
|
| 7 |
import ( |
| 8 |
"database/sql" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
|
| 12 |
"heckel.io/ntfy/v2/db/pg" |
| 13 |
"heckel.io/ntfy/v2/log" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
tag = "schema" |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
sqliteCreateVersionTableQuery = `CREATE TABLE IF NOT EXISTS schemaVersion (id INT PRIMARY KEY, version INT NOT NULL)` |
| 22 |
sqliteSelectVersionQuery = `SELECT version FROM schemaVersion WHERE id = 1` |
| 23 |
sqliteUpsertVersionQuery = `INSERT INTO schemaVersion (id, version) VALUES (1, ?) ON CONFLICT (id) DO UPDATE SET version = excluded.version` |
| 24 |
|
| 25 |
postgresCreateVersionTableQuery = `CREATE TABLE IF NOT EXISTS schema_version (store TEXT PRIMARY KEY, version INT NOT NULL)` |
| 26 |
postgresSelectVersionQuery = `SELECT version FROM schema_version WHERE store = $1` |
| 27 |
postgresUpsertVersionQuery = `INSERT INTO schema_version (store, version) VALUES ($1, $2) ON CONFLICT (store) DO UPDATE SET version = EXCLUDED.version` |
| 28 |
postgresAdvisoryLockQuery = `SELECT pg_advisory_xact_lock($1)` // Transaction-scoped lock to avoid migration races |
| 29 |
) |
| 30 |
|
| 31 |
// Migrate creates or upgrades the named store's schema to targetVersion in one transaction, or |
| 32 |
// creates a new database using the "create" function. |
| 33 |
func Migrate(db *sql.DB, dialect Dialect, store string, targetVersion int, create MigrateFunc, migrations map[int]MigrateFunc) error { |
| 34 |
if dialect != Postgres && dialect != SQLite { |
| 35 |
return fmt.Errorf("unsupported schema dialect %d", dialect) |
| 36 |
} |
| 37 |
tx, err := db.Begin() |
| 38 |
if err != nil { |
| 39 |
return fmt.Errorf("cannot begin %s schema transaction: %w", store, err) |
| 40 |
} |
| 41 |
defer tx.Rollback() |
| 42 |
if dialect == Postgres { |
| 43 |
// Serialize setup across nodes: CREATE TABLE IF NOT EXISTS is not atomic, and |
| 44 |
// concurrently cold-booting nodes would otherwise race on DDL and crash |
| 45 |
if _, err := tx.Exec(postgresAdvisoryLockQuery, pg.SchemaLockKey); err != nil { |
| 46 |
return fmt.Errorf("cannot acquire %s schema advisory lock: %w", store, err) |
| 47 |
} |
| 48 |
} |
| 49 |
if _, err := tx.Exec(createVersionTableQuery(dialect)); err != nil { |
| 50 |
return fmt.Errorf("cannot create schema version table: %w", err) |
| 51 |
} |
| 52 |
version, err := readVersion(tx, dialect, store) |
| 53 |
if errors.Is(err, sql.ErrNoRows) { |
| 54 |
// Fresh database: create the store's tables at the target version |
| 55 |
if err := create(tx); err != nil { |
| 56 |
return fmt.Errorf("cannot create %s schema: %w", store, err) |
| 57 |
} |
| 58 |
if err := writeVersion(tx, dialect, store, targetVersion); err != nil { |
| 59 |
return fmt.Errorf("cannot write %s schema version: %w", store, err) |
| 60 |
} |
| 61 |
return tx.Commit() |
| 62 |
} else if err != nil { |
| 63 |
return fmt.Errorf("cannot read %s schema version: %w", store, err) |
| 64 |
} |
| 65 |
if version == targetVersion { |
| 66 |
return tx.Commit() |
| 67 |
} |
| 68 |
if version > targetVersion { |
| 69 |
return fmt.Errorf("unexpected %s schema version %d, this version of ntfy supports up to %d", store, version, targetVersion) |
| 70 |
} |
| 71 |
for v := version; v < targetVersion; v++ { |
| 72 |
migrate, ok := migrations[v] |
| 73 |
if !ok { |
| 74 |
return fmt.Errorf("cannot find %s migration step from version %d to %d", store, v, v+1) |
| 75 |
} |
| 76 |
log.Tag(tag).Info("Migrating %s database schema: from %d to %d", store, v, v+1) |
| 77 |
if err := migrate(tx); err != nil { |
| 78 |
return fmt.Errorf("%s migration step from version %d to %d failed: %w", store, v, v+1, err) |
| 79 |
} |
| 80 |
} |
| 81 |
if err := writeVersion(tx, dialect, store, targetVersion); err != nil { |
| 82 |
return fmt.Errorf("cannot write %s schema version: %w", store, err) |
| 83 |
} |
| 84 |
return tx.Commit() |
| 85 |
} |
| 86 |
|
| 87 |
func createVersionTableQuery(dialect Dialect) string { |
| 88 |
if dialect == Postgres { |
| 89 |
return postgresCreateVersionTableQuery |
| 90 |
} |
| 91 |
return sqliteCreateVersionTableQuery |
| 92 |
} |
| 93 |
|
| 94 |
func readVersion(tx *sql.Tx, dialect Dialect, store string) (version int, err error) { |
| 95 |
if dialect == Postgres { |
| 96 |
err = tx.QueryRow(postgresSelectVersionQuery, store).Scan(&version) |
| 97 |
} else { |
| 98 |
err = tx.QueryRow(sqliteSelectVersionQuery).Scan(&version) |
| 99 |
} |
| 100 |
return |
| 101 |
} |
| 102 |
|
| 103 |
func writeVersion(tx *sql.Tx, dialect Dialect, store string, version int) error { |
| 104 |
var err error |
| 105 |
if dialect == Postgres { |
| 106 |
_, err = tx.Exec(postgresUpsertVersionQuery, store, version) |
| 107 |
} else { |
| 108 |
_, err = tx.Exec(sqliteUpsertVersionQuery, version) |
| 109 |
} |
| 110 |
return err |
| 111 |
} |