2019-05-16 19:14:32 +02:00
|
|
|
package upgrades
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-01-07 15:45:20 +01:00
|
|
|
"errors"
|
2019-05-16 19:14:32 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Dialect int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Postgres Dialect = iota
|
|
|
|
SQLite
|
|
|
|
)
|
|
|
|
|
2020-07-10 14:56:45 +02:00
|
|
|
func (dialect Dialect) String() string {
|
|
|
|
switch dialect {
|
|
|
|
case Postgres:
|
|
|
|
return "postgres"
|
|
|
|
case SQLite:
|
|
|
|
return "sqlite3"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 16:25:19 +02:00
|
|
|
type upgradeFunc func(*sql.Tx, context) error
|
|
|
|
|
|
|
|
type context struct {
|
|
|
|
dialect Dialect
|
|
|
|
db *sql.DB
|
|
|
|
log log.Logger
|
|
|
|
}
|
2019-05-16 19:14:32 +02:00
|
|
|
|
|
|
|
type upgrade struct {
|
|
|
|
message string
|
2019-08-25 16:25:19 +02:00
|
|
|
fn upgradeFunc
|
2019-05-16 19:14:32 +02:00
|
|
|
}
|
|
|
|
|
2022-04-29 09:44:22 +02:00
|
|
|
const NumberOfUpgrades = 42
|
2019-05-24 01:33:26 +02:00
|
|
|
|
|
|
|
var upgrades [NumberOfUpgrades]upgrade
|
2019-05-16 19:14:32 +02:00
|
|
|
|
2022-03-16 00:15:13 +01:00
|
|
|
var ErrUnsupportedDatabaseVersion = fmt.Errorf("unsupported database schema version")
|
|
|
|
var ErrForeignTables = fmt.Errorf("the database contains foreign tables")
|
|
|
|
var ErrNotOwned = fmt.Errorf("the database is owned by")
|
|
|
|
var IgnoreForeignTables = false
|
|
|
|
|
|
|
|
const databaseOwner = "mautrix-whatsapp"
|
2019-05-28 20:29:43 +02:00
|
|
|
|
2019-08-25 16:25:19 +02:00
|
|
|
func GetVersion(db *sql.DB) (int, error) {
|
2019-05-16 19:14:32 +02:00
|
|
|
_, err := db.Exec("CREATE TABLE IF NOT EXISTS version (version INTEGER)")
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
version := 0
|
2022-01-07 15:45:20 +01:00
|
|
|
err = db.QueryRow("SELECT version FROM version LIMIT 1").Scan(&version)
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return -1, err
|
2019-05-16 19:14:32 +02:00
|
|
|
}
|
|
|
|
return version, nil
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:15:13 +01:00
|
|
|
const tableExistsPostgres = "SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name=$1)"
|
|
|
|
const tableExistsSQLite = "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND table_name=$1)"
|
|
|
|
|
|
|
|
func tableExists(dialect Dialect, db *sql.DB, table string) (exists bool) {
|
|
|
|
if dialect == SQLite {
|
|
|
|
_ = db.QueryRow(tableExistsSQLite, table).Scan(&exists)
|
|
|
|
} else if dialect == Postgres {
|
|
|
|
_ = db.QueryRow(tableExistsPostgres, table).Scan(&exists)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const createOwnerTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS database_owner (
|
|
|
|
key INTEGER PRIMARY KEY DEFAULT 0,
|
|
|
|
owner TEXT NOT NULL
|
|
|
|
)
|
|
|
|
`
|
|
|
|
|
|
|
|
func CheckDatabaseOwner(dialect Dialect, db *sql.DB) error {
|
|
|
|
var owner string
|
|
|
|
if !IgnoreForeignTables {
|
|
|
|
if tableExists(dialect, db, "state_groups_state") {
|
|
|
|
return fmt.Errorf("%w (found state_groups_state, likely belonging to Synapse)", ErrForeignTables)
|
|
|
|
} else if tableExists(dialect, db, "goose_db_version") {
|
|
|
|
return fmt.Errorf("%w (found goose_db_version, possibly belonging to Dendrite)", ErrForeignTables)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(createOwnerTable); err != nil {
|
|
|
|
return fmt.Errorf("failed to ensure database owner table exists: %w", err)
|
|
|
|
} else if err = db.QueryRow("SELECT owner FROM database_owner WHERE key=0").Scan(&owner); errors.Is(err, sql.ErrNoRows) {
|
|
|
|
_, err = db.Exec("INSERT INTO database_owner (owner) VALUES ($1)", databaseOwner)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to insert database owner: %w", err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to check database owner: %w", err)
|
|
|
|
} else if owner != databaseOwner {
|
|
|
|
return fmt.Errorf("%w %s", ErrNotOwned, owner)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-25 16:25:19 +02:00
|
|
|
func SetVersion(tx *sql.Tx, version int) error {
|
2019-05-16 19:14:32 +02:00
|
|
|
_, err := tx.Exec("DELETE FROM version")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.Exec("INSERT INTO version (version) VALUES ($1)", version)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-19 18:17:19 +02:00
|
|
|
func execMany(tx *sql.Tx, queries ...string) error {
|
|
|
|
for _, query := range queries {
|
|
|
|
_, err := tx.Exec(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-16 19:14:32 +02:00
|
|
|
func Run(log log.Logger, dialectName string, db *sql.DB) error {
|
|
|
|
var dialect Dialect
|
|
|
|
switch strings.ToLower(dialectName) {
|
|
|
|
case "postgres":
|
|
|
|
dialect = Postgres
|
|
|
|
case "sqlite3":
|
|
|
|
dialect = SQLite
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown dialect %s", dialectName)
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:15:13 +01:00
|
|
|
err := CheckDatabaseOwner(dialect, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-25 16:25:19 +02:00
|
|
|
version, err := GetVersion(db)
|
2019-05-16 19:14:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:29:43 +02:00
|
|
|
if version > NumberOfUpgrades {
|
2022-03-16 00:15:13 +01:00
|
|
|
return fmt.Errorf("%w: currently on v%d, latest known: v%d", ErrUnsupportedDatabaseVersion, version, NumberOfUpgrades)
|
2019-05-28 20:29:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 01:33:26 +02:00
|
|
|
log.Infofln("Database currently on v%d, latest: v%d", version, NumberOfUpgrades)
|
2021-08-19 18:17:19 +02:00
|
|
|
for i, upgradeItem := range upgrades[version:] {
|
2021-10-30 22:40:38 +02:00
|
|
|
if upgradeItem.fn == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-19 18:17:19 +02:00
|
|
|
log.Infofln("Upgrading database to v%d: %s", version+i+1, upgradeItem.message)
|
|
|
|
var tx *sql.Tx
|
|
|
|
tx, err = db.Begin()
|
2019-05-16 19:14:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-19 18:17:19 +02:00
|
|
|
err = upgradeItem.fn(tx, context{dialect, db, log})
|
2019-05-16 19:14:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-25 16:25:19 +02:00
|
|
|
err = SetVersion(tx, version+i+1)
|
2019-05-16 19:14:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|