add vendoring
This commit is contained in:
102
vendor/maunium.net/go/mautrix/util/dbutil/connlog.go
generated
vendored
Normal file
102
vendor/maunium.net/go/mautrix/util/dbutil/connlog.go
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
// Copyright (c) 2022 Tulir Asokan
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package dbutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LoggingExecable is a wrapper for anything with database Exec methods (i.e. sql.Conn, sql.DB and sql.Tx)
|
||||
// that can preprocess queries (e.g. replacing $ with ? on SQLite) and log query durations.
|
||||
type LoggingExecable struct {
|
||||
UnderlyingExecable Execable
|
||||
db *Database
|
||||
}
|
||||
|
||||
func (le *LoggingExecable) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
||||
start := time.Now()
|
||||
query = le.db.mutateQuery(query)
|
||||
res, err := le.UnderlyingExecable.ExecContext(ctx, query, args...)
|
||||
le.db.Log.QueryTiming(ctx, "Exec", query, args, time.Since(start))
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (le *LoggingExecable) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
|
||||
start := time.Now()
|
||||
query = le.db.mutateQuery(query)
|
||||
rows, err := le.UnderlyingExecable.QueryContext(ctx, query, args...)
|
||||
le.db.Log.QueryTiming(ctx, "Query", query, args, time.Since(start))
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (le *LoggingExecable) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
|
||||
start := time.Now()
|
||||
query = le.db.mutateQuery(query)
|
||||
row := le.UnderlyingExecable.QueryRowContext(ctx, query, args...)
|
||||
le.db.Log.QueryTiming(ctx, "QueryRow", query, args, time.Since(start))
|
||||
return row
|
||||
}
|
||||
|
||||
func (le *LoggingExecable) Exec(query string, args ...interface{}) (sql.Result, error) {
|
||||
return le.ExecContext(context.Background(), query, args...)
|
||||
}
|
||||
|
||||
func (le *LoggingExecable) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
||||
return le.QueryContext(context.Background(), query, args...)
|
||||
}
|
||||
|
||||
func (le *LoggingExecable) QueryRow(query string, args ...interface{}) *sql.Row {
|
||||
return le.QueryRowContext(context.Background(), query, args...)
|
||||
}
|
||||
|
||||
// loggingDB is a wrapper for LoggingExecable that allows access to BeginTx.
|
||||
//
|
||||
// While LoggingExecable has a pointer to the database and could use BeginTx, it's not technically safe since
|
||||
// the LoggingExecable could be for a transaction (where BeginTx wouldn't make sense).
|
||||
type loggingDB struct {
|
||||
LoggingExecable
|
||||
}
|
||||
|
||||
func (ld *loggingDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*LoggingTxn, error) {
|
||||
start := time.Now()
|
||||
tx, err := ld.db.RawDB.BeginTx(ctx, opts)
|
||||
ld.db.Log.QueryTiming(ctx, "Begin", "", nil, time.Since(start))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &LoggingTxn{
|
||||
LoggingExecable: LoggingExecable{UnderlyingExecable: tx, db: ld.db},
|
||||
UnderlyingTx: tx,
|
||||
ctx: ctx,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ld *loggingDB) Begin() (*LoggingTxn, error) {
|
||||
return ld.BeginTx(context.Background(), nil)
|
||||
}
|
||||
|
||||
type LoggingTxn struct {
|
||||
LoggingExecable
|
||||
UnderlyingTx *sql.Tx
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (lt *LoggingTxn) Commit() error {
|
||||
start := time.Now()
|
||||
err := lt.UnderlyingTx.Commit()
|
||||
lt.db.Log.QueryTiming(lt.ctx, "Commit", "", nil, time.Since(start))
|
||||
return err
|
||||
}
|
||||
|
||||
func (lt *LoggingTxn) Rollback() error {
|
||||
start := time.Now()
|
||||
err := lt.UnderlyingTx.Rollback()
|
||||
lt.db.Log.QueryTiming(lt.ctx, "Rollback", "", nil, time.Since(start))
|
||||
return err
|
||||
}
|
||||
206
vendor/maunium.net/go/mautrix/util/dbutil/database.go
generated
vendored
Normal file
206
vendor/maunium.net/go/mautrix/util/dbutil/database.go
generated
vendored
Normal file
@@ -0,0 +1,206 @@
|
||||
// Copyright (c) 2022 Tulir Asokan
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package dbutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
||||
)
|
||||
|
||||
type Dialect int
|
||||
|
||||
const (
|
||||
DialectUnknown Dialect = iota
|
||||
Postgres
|
||||
SQLite
|
||||
)
|
||||
|
||||
func (dialect Dialect) String() string {
|
||||
switch dialect {
|
||||
case Postgres:
|
||||
return "postgres"
|
||||
case SQLite:
|
||||
return "sqlite3"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func ParseDialect(engine string) (Dialect, error) {
|
||||
switch strings.ToLower(engine) {
|
||||
case "postgres", "postgresql":
|
||||
return Postgres, nil
|
||||
case "sqlite3", "sqlite", "litestream":
|
||||
return SQLite, nil
|
||||
default:
|
||||
return DialectUnknown, fmt.Errorf("unknown dialect '%s'", engine)
|
||||
}
|
||||
}
|
||||
|
||||
type Scannable interface {
|
||||
Scan(...interface{}) error
|
||||
}
|
||||
|
||||
// Expected implementations of Scannable
|
||||
var (
|
||||
_ Scannable = (*sql.Row)(nil)
|
||||
_ Scannable = (*sql.Rows)(nil)
|
||||
)
|
||||
|
||||
type ContextExecable interface {
|
||||
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
||||
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
||||
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
type Execable interface {
|
||||
ContextExecable
|
||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
QueryRow(query string, args ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
type Transaction interface {
|
||||
Execable
|
||||
Commit() error
|
||||
Rollback() error
|
||||
}
|
||||
|
||||
// Expected implementations of Execable
|
||||
var (
|
||||
_ Execable = (*sql.Tx)(nil)
|
||||
_ Execable = (*sql.DB)(nil)
|
||||
_ Execable = (*LoggingExecable)(nil)
|
||||
_ Transaction = (*LoggingTxn)(nil)
|
||||
_ ContextExecable = (*sql.Conn)(nil)
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
loggingDB
|
||||
RawDB *sql.DB
|
||||
Owner string
|
||||
VersionTable string
|
||||
Log DatabaseLogger
|
||||
Dialect Dialect
|
||||
UpgradeTable UpgradeTable
|
||||
|
||||
IgnoreForeignTables bool
|
||||
IgnoreUnsupportedDatabase bool
|
||||
}
|
||||
|
||||
var positionalParamPattern = regexp.MustCompile(`\$(\d+)`)
|
||||
|
||||
func (db *Database) mutateQuery(query string) string {
|
||||
switch db.Dialect {
|
||||
case SQLite:
|
||||
return positionalParamPattern.ReplaceAllString(query, "?$1")
|
||||
default:
|
||||
return query
|
||||
}
|
||||
}
|
||||
|
||||
func (db *Database) Child(versionTable string, upgradeTable UpgradeTable, log DatabaseLogger) *Database {
|
||||
if log == nil {
|
||||
log = db.Log
|
||||
}
|
||||
return &Database{
|
||||
RawDB: db.RawDB,
|
||||
loggingDB: db.loggingDB,
|
||||
Owner: "",
|
||||
VersionTable: versionTable,
|
||||
UpgradeTable: upgradeTable,
|
||||
Log: log,
|
||||
Dialect: db.Dialect,
|
||||
|
||||
IgnoreForeignTables: true,
|
||||
IgnoreUnsupportedDatabase: db.IgnoreUnsupportedDatabase,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWithDB(db *sql.DB, rawDialect string) (*Database, error) {
|
||||
dialect, err := ParseDialect(rawDialect)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wrappedDB := &Database{
|
||||
RawDB: db,
|
||||
Dialect: dialect,
|
||||
Log: NoopLogger,
|
||||
|
||||
IgnoreForeignTables: true,
|
||||
VersionTable: "version",
|
||||
}
|
||||
wrappedDB.loggingDB.UnderlyingExecable = db
|
||||
wrappedDB.loggingDB.db = wrappedDB
|
||||
return wrappedDB, nil
|
||||
}
|
||||
|
||||
func NewWithDialect(uri, rawDialect string) (*Database, error) {
|
||||
db, err := sql.Open(rawDialect, uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewWithDB(db, rawDialect)
|
||||
}
|
||||
|
||||
func (db *Database) Configure(cfg bridgeconfig.DatabaseConfig) error {
|
||||
db.RawDB.SetMaxOpenConns(cfg.MaxOpenConns)
|
||||
db.RawDB.SetMaxIdleConns(cfg.MaxIdleConns)
|
||||
if len(cfg.ConnMaxIdleTime) > 0 {
|
||||
maxIdleTimeDuration, err := time.ParseDuration(cfg.ConnMaxIdleTime)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
|
||||
}
|
||||
db.RawDB.SetConnMaxIdleTime(maxIdleTimeDuration)
|
||||
}
|
||||
if len(cfg.ConnMaxLifetime) > 0 {
|
||||
maxLifetimeDuration, err := time.ParseDuration(cfg.ConnMaxLifetime)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse max_conn_idle_time: %w", err)
|
||||
}
|
||||
db.RawDB.SetConnMaxLifetime(maxLifetimeDuration)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewFromConfig(owner string, cfg bridgeconfig.DatabaseConfig, logger DatabaseLogger) (*Database, error) {
|
||||
dialect, err := ParseDialect(cfg.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := sql.Open(cfg.Type, cfg.URI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if logger == nil {
|
||||
logger = NoopLogger
|
||||
}
|
||||
wrappedDB := &Database{
|
||||
RawDB: conn,
|
||||
|
||||
Owner: owner,
|
||||
Dialect: dialect,
|
||||
|
||||
Log: logger,
|
||||
|
||||
IgnoreForeignTables: true,
|
||||
VersionTable: "version",
|
||||
}
|
||||
err = wrappedDB.Configure(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wrappedDB.loggingDB.UnderlyingExecable = conn
|
||||
wrappedDB.loggingDB.db = wrappedDB
|
||||
return wrappedDB, nil
|
||||
}
|
||||
129
vendor/maunium.net/go/mautrix/util/dbutil/log.go
generated
vendored
Normal file
129
vendor/maunium.net/go/mautrix/util/dbutil/log.go
generated
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
package dbutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"maunium.net/go/maulogger/v2"
|
||||
)
|
||||
|
||||
type DatabaseLogger interface {
|
||||
QueryTiming(ctx context.Context, method, query string, args []interface{}, duration time.Duration)
|
||||
WarnUnsupportedVersion(current, latest int)
|
||||
PrepareUpgrade(current, latest int)
|
||||
DoUpgrade(from, to int, message string)
|
||||
// Deprecated: legacy warning method, return errors instead
|
||||
Warn(msg string, args ...interface{})
|
||||
}
|
||||
|
||||
type noopLogger struct{}
|
||||
|
||||
var NoopLogger DatabaseLogger = &noopLogger{}
|
||||
|
||||
func (n noopLogger) WarnUnsupportedVersion(_, _ int) {}
|
||||
func (n noopLogger) PrepareUpgrade(_, _ int) {}
|
||||
func (n noopLogger) DoUpgrade(_, _ int, _ string) {}
|
||||
func (n noopLogger) Warn(msg string, args ...interface{}) {}
|
||||
|
||||
func (n noopLogger) QueryTiming(_ context.Context, _, _ string, _ []interface{}, _ time.Duration) {}
|
||||
|
||||
type mauLogger struct {
|
||||
l maulogger.Logger
|
||||
}
|
||||
|
||||
func MauLogger(log maulogger.Logger) DatabaseLogger {
|
||||
return &mauLogger{l: log}
|
||||
}
|
||||
|
||||
func (m mauLogger) WarnUnsupportedVersion(current, latest int) {
|
||||
m.l.Warnfln("Unsupported database schema version: currently on v%d, latest known: v%d - continuing anyway", current, latest)
|
||||
}
|
||||
|
||||
func (m mauLogger) PrepareUpgrade(current, latest int) {
|
||||
m.l.Infofln("Database currently on v%d, latest: v%d", current, latest)
|
||||
}
|
||||
|
||||
func (m mauLogger) DoUpgrade(from, to int, message string) {
|
||||
m.l.Infofln("Upgrading database from v%d to v%d: %s", from, to, message)
|
||||
}
|
||||
|
||||
func (m mauLogger) QueryTiming(_ context.Context, method, query string, _ []interface{}, duration time.Duration) {
|
||||
if duration > 1*time.Second {
|
||||
m.l.Warnfln("%s(%s) took %.3f seconds", method, query, duration.Seconds())
|
||||
}
|
||||
}
|
||||
|
||||
func (m mauLogger) Warn(msg string, args ...interface{}) {
|
||||
m.l.Warnfln(msg, args...)
|
||||
}
|
||||
|
||||
type zeroLogger struct {
|
||||
l *zerolog.Logger
|
||||
}
|
||||
|
||||
func ZeroLogger(log zerolog.Logger) DatabaseLogger {
|
||||
return ZeroLoggerPtr(&log)
|
||||
}
|
||||
|
||||
func ZeroLoggerPtr(log *zerolog.Logger) DatabaseLogger {
|
||||
return &zeroLogger{l: log}
|
||||
}
|
||||
|
||||
func (z zeroLogger) WarnUnsupportedVersion(current, latest int) {
|
||||
z.l.Warn().
|
||||
Int("current_db_version", current).
|
||||
Int("latest_known_version", latest).
|
||||
Msg("Unsupported database schema version, continuing anyway")
|
||||
}
|
||||
|
||||
func (z zeroLogger) PrepareUpgrade(current, latest int) {
|
||||
evt := z.l.Info().
|
||||
Int("current_db_version", current).
|
||||
Int("latest_known_version", latest)
|
||||
if current == latest {
|
||||
evt.Msg("Database is up to date")
|
||||
} else {
|
||||
evt.Msg("Preparing to update database schema")
|
||||
}
|
||||
}
|
||||
|
||||
func (z zeroLogger) DoUpgrade(from, to int, message string) {
|
||||
z.l.Info().
|
||||
Int("from", from).
|
||||
Int("to", to).
|
||||
Str("description", message).
|
||||
Msg("Upgrading database")
|
||||
}
|
||||
|
||||
var whitespaceRegex = regexp.MustCompile(`\s+`)
|
||||
|
||||
func (z zeroLogger) QueryTiming(ctx context.Context, method, query string, args []interface{}, duration time.Duration) {
|
||||
log := zerolog.Ctx(ctx)
|
||||
if log.GetLevel() == zerolog.Disabled {
|
||||
log = z.l
|
||||
}
|
||||
if log.GetLevel() != zerolog.TraceLevel && duration < 1*time.Second {
|
||||
return
|
||||
}
|
||||
query = strings.TrimSpace(whitespaceRegex.ReplaceAllLiteralString(query, " "))
|
||||
log.Trace().
|
||||
Int64("duration_µs", duration.Microseconds()).
|
||||
Str("method", method).
|
||||
Str("query", query).
|
||||
Interface("query_args", args).
|
||||
Msg("Query")
|
||||
if duration >= 1*time.Second {
|
||||
log.Warn().
|
||||
Float64("duration_seconds", duration.Seconds()).
|
||||
Str("method", method).
|
||||
Str("query", query).
|
||||
Msg("Query took long")
|
||||
}
|
||||
}
|
||||
|
||||
func (z zeroLogger) Warn(msg string, args ...interface{}) {
|
||||
z.l.Warn().Msgf(msg, args...)
|
||||
}
|
||||
21
vendor/maunium.net/go/mautrix/util/dbutil/samples/01-sample.sql
generated
vendored
Normal file
21
vendor/maunium.net/go/mautrix/util/dbutil/samples/01-sample.sql
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
-- v0 -> v3: Sample revision jump
|
||||
CREATE TABLE foo (
|
||||
-- only: postgres
|
||||
key BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
||||
-- only: sqlite
|
||||
key INTEGER PRIMARY KEY,
|
||||
|
||||
data JSONB NOT NULL
|
||||
);
|
||||
|
||||
-- only: sqlite until "end only"
|
||||
CREATE TRIGGER test AFTER INSERT ON foo WHEN NEW.data->>'action' = 'delete' BEGIN
|
||||
DELETE FROM test WHERE key <= NEW.data->>'index';
|
||||
END;
|
||||
-- end only sqlite
|
||||
-- only: postgres until "end only"
|
||||
CREATE FUNCTION delete_data() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN
|
||||
DELETE FROM test WHERE key <= NEW.data->>'index';
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
-- end only postgres
|
||||
11
vendor/maunium.net/go/mautrix/util/dbutil/samples/output/01-postgres.sql
generated
vendored
Normal file
11
vendor/maunium.net/go/mautrix/util/dbutil/samples/output/01-postgres.sql
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE foo (
|
||||
key BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
||||
|
||||
data JSONB NOT NULL
|
||||
);
|
||||
|
||||
CREATE FUNCTION delete_data() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN
|
||||
DELETE FROM test WHERE key <= NEW.data->>'index';
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
-- end only postgres
|
||||
10
vendor/maunium.net/go/mautrix/util/dbutil/samples/output/01-sqlite3.sql
generated
vendored
Normal file
10
vendor/maunium.net/go/mautrix/util/dbutil/samples/output/01-sqlite3.sql
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE foo (
|
||||
key INTEGER PRIMARY KEY,
|
||||
|
||||
data JSONB NOT NULL
|
||||
);
|
||||
|
||||
CREATE TRIGGER test AFTER INSERT ON foo WHEN NEW.data->>'action' = 'delete' BEGIN
|
||||
DELETE FROM test WHERE key <= NEW.data->>'index';
|
||||
END;
|
||||
-- end only sqlite
|
||||
154
vendor/maunium.net/go/mautrix/util/dbutil/upgrades.go
generated
vendored
Normal file
154
vendor/maunium.net/go/mautrix/util/dbutil/upgrades.go
generated
vendored
Normal file
@@ -0,0 +1,154 @@
|
||||
// Copyright (c) 2022 Tulir Asokan
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package dbutil
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type upgradeFunc func(Transaction, *Database) error
|
||||
|
||||
type upgrade struct {
|
||||
message string
|
||||
fn upgradeFunc
|
||||
|
||||
upgradesTo int
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
func (db *Database) getVersion() (int, error) {
|
||||
_, err := db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (version INTEGER)", db.VersionTable))
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
version := 0
|
||||
err = db.QueryRow(fmt.Sprintf("SELECT version FROM %s LIMIT 1", db.VersionTable)).Scan(&version)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return -1, err
|
||||
}
|
||||
return version, nil
|
||||
}
|
||||
|
||||
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 tbl_name=$1)"
|
||||
|
||||
func (db *Database) tableExists(table string) (exists bool, err error) {
|
||||
if db.Dialect == SQLite {
|
||||
err = db.QueryRow(tableExistsSQLite, table).Scan(&exists)
|
||||
} else if db.Dialect == Postgres {
|
||||
err = db.QueryRow(tableExistsPostgres, table).Scan(&exists)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (db *Database) tableExistsNoError(table string) bool {
|
||||
exists, err := db.tableExists(table)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to check if table exists: %w", err))
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
const createOwnerTable = `
|
||||
CREATE TABLE IF NOT EXISTS database_owner (
|
||||
key INTEGER PRIMARY KEY DEFAULT 0,
|
||||
owner TEXT NOT NULL
|
||||
)
|
||||
`
|
||||
|
||||
func (db *Database) checkDatabaseOwner() error {
|
||||
var owner string
|
||||
if !db.IgnoreForeignTables {
|
||||
if db.tableExistsNoError("state_groups_state") {
|
||||
return fmt.Errorf("%w (found state_groups_state, likely belonging to Synapse)", ErrForeignTables)
|
||||
} else if db.tableExistsNoError("roomserver_rooms") {
|
||||
return fmt.Errorf("%w (found roomserver_rooms, likely belonging to Dendrite)", ErrForeignTables)
|
||||
}
|
||||
}
|
||||
if db.Owner == "" {
|
||||
return nil
|
||||
}
|
||||
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 (key, owner) VALUES (0, $1)", db.Owner)
|
||||
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 != db.Owner {
|
||||
return fmt.Errorf("%w %s", ErrNotOwned, owner)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *Database) setVersion(tx Transaction, version int) error {
|
||||
_, err := tx.Exec(fmt.Sprintf("DELETE FROM %s", db.VersionTable))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(fmt.Sprintf("INSERT INTO %s (version) VALUES ($1)", db.VersionTable), version)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) Upgrade() error {
|
||||
err := db.checkDatabaseOwner()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
version, err := db.getVersion()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if version > len(db.UpgradeTable) {
|
||||
if db.IgnoreUnsupportedDatabase {
|
||||
db.Log.WarnUnsupportedVersion(version, len(db.UpgradeTable))
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%w: currently on v%d, latest known: v%d", ErrUnsupportedDatabaseVersion, version, len(db.UpgradeTable))
|
||||
}
|
||||
|
||||
db.Log.PrepareUpgrade(version, len(db.UpgradeTable))
|
||||
logVersion := version
|
||||
for version < len(db.UpgradeTable) {
|
||||
upgradeItem := db.UpgradeTable[version]
|
||||
if upgradeItem.fn == nil {
|
||||
version++
|
||||
continue
|
||||
}
|
||||
db.Log.DoUpgrade(logVersion, upgradeItem.upgradesTo, upgradeItem.message)
|
||||
var tx Transaction
|
||||
tx, err = db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = upgradeItem.fn(tx, db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
version = upgradeItem.upgradesTo
|
||||
logVersion = version
|
||||
err = db.setVersion(tx, version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
255
vendor/maunium.net/go/mautrix/util/dbutil/upgradetable.go
generated
vendored
Normal file
255
vendor/maunium.net/go/mautrix/util/dbutil/upgradetable.go
generated
vendored
Normal file
@@ -0,0 +1,255 @@
|
||||
// Copyright (c) 2022 Tulir Asokan
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package dbutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UpgradeTable []upgrade
|
||||
|
||||
func (ut *UpgradeTable) extend(toSize int) {
|
||||
if cap(*ut) >= toSize {
|
||||
*ut = (*ut)[:toSize]
|
||||
} else {
|
||||
resized := make([]upgrade, toSize)
|
||||
copy(resized, *ut)
|
||||
*ut = resized
|
||||
}
|
||||
}
|
||||
|
||||
func (ut *UpgradeTable) Register(from, to int, message string, fn upgradeFunc) {
|
||||
if from < 0 {
|
||||
from += to
|
||||
}
|
||||
if from < 0 {
|
||||
panic("invalid from value in UpgradeTable.Register() call")
|
||||
}
|
||||
upg := upgrade{message: message, fn: fn, upgradesTo: to}
|
||||
if len(*ut) == from {
|
||||
*ut = append(*ut, upg)
|
||||
return
|
||||
} else if len(*ut) < from {
|
||||
ut.extend(from + 1)
|
||||
} else if (*ut)[from].fn != nil {
|
||||
panic(fmt.Errorf("tried to override upgrade at %d ('%s') with '%s'", from, (*ut)[from].message, upg.message))
|
||||
}
|
||||
(*ut)[from] = upg
|
||||
}
|
||||
|
||||
// Syntax is either
|
||||
//
|
||||
// -- v0 -> v1: Message
|
||||
//
|
||||
// or
|
||||
//
|
||||
// -- v1: Message
|
||||
var upgradeHeaderRegex = regexp.MustCompile(`^-- (?:v(\d+) -> )?v(\d+): (.+)$`)
|
||||
|
||||
func parseFileHeader(file []byte) (from, to int, message string, lines [][]byte, err error) {
|
||||
lines = bytes.Split(file, []byte("\n"))
|
||||
if len(lines) < 2 {
|
||||
err = errors.New("upgrade file too short")
|
||||
return
|
||||
}
|
||||
var maybeFrom int
|
||||
match := upgradeHeaderRegex.FindSubmatch(lines[0])
|
||||
lines = lines[1:]
|
||||
if match == nil {
|
||||
err = errors.New("header not found")
|
||||
} else if len(match) != 4 {
|
||||
err = errors.New("unexpected number of items in regex match")
|
||||
} else if maybeFrom, err = strconv.Atoi(string(match[1])); len(match[1]) > 0 && err != nil {
|
||||
err = fmt.Errorf("invalid source version: %w", err)
|
||||
} else if to, err = strconv.Atoi(string(match[2])); err != nil {
|
||||
err = fmt.Errorf("invalid target version: %w", err)
|
||||
} else {
|
||||
if len(match[1]) > 0 {
|
||||
from = maybeFrom
|
||||
} else {
|
||||
from = -1
|
||||
}
|
||||
message = string(match[3])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// To limit the next line to one dialect:
|
||||
//
|
||||
// -- only: postgres
|
||||
//
|
||||
// To limit the next N lines:
|
||||
//
|
||||
// -- only: sqlite for next 123 lines
|
||||
//
|
||||
// If the single-line limit is on the second line of the file, the whole file is limited to that dialect.
|
||||
var dialectLineFilter = regexp.MustCompile(`^\s*-- only: (postgres|sqlite)(?: for next (\d+) lines| until "(end) only")?`)
|
||||
|
||||
// Constants used to make parseDialectFilter clearer
|
||||
const (
|
||||
skipUntilEndTag = -1
|
||||
skipNothing = 0
|
||||
skipCurrentLine = 1
|
||||
skipNextLine = 2
|
||||
)
|
||||
|
||||
func (db *Database) parseDialectFilter(line []byte) (int, error) {
|
||||
match := dialectLineFilter.FindSubmatch(line)
|
||||
if match == nil {
|
||||
return skipNothing, nil
|
||||
}
|
||||
dialect, err := ParseDialect(string(match[1]))
|
||||
if err != nil {
|
||||
return skipNothing, err
|
||||
} else if dialect == db.Dialect {
|
||||
// Skip the dialect filter line
|
||||
return skipCurrentLine, nil
|
||||
} else if bytes.Equal(match[3], []byte("end")) {
|
||||
return skipUntilEndTag, nil
|
||||
} else if len(match[2]) == 0 {
|
||||
// Skip the dialect filter and the next line
|
||||
return skipNextLine, nil
|
||||
} else {
|
||||
// Parse number of lines to skip, add 1 for current line
|
||||
lineCount, err := strconv.Atoi(string(match[2]))
|
||||
if err != nil {
|
||||
return skipNothing, fmt.Errorf("invalid line count '%s': %w", match[2], err)
|
||||
}
|
||||
return skipCurrentLine + lineCount, nil
|
||||
}
|
||||
}
|
||||
|
||||
var endLineFilter = regexp.MustCompile(`^\s*-- end only (postgres|sqlite)$`)
|
||||
|
||||
func (db *Database) filterSQLUpgrade(lines [][]byte) (string, error) {
|
||||
output := make([][]byte, 0, len(lines))
|
||||
for i := 0; i < len(lines); i++ {
|
||||
skipLines, err := db.parseDialectFilter(lines[i])
|
||||
if err != nil {
|
||||
return "", err
|
||||
} else if skipLines > 0 {
|
||||
// Current line is implicitly skipped, so reduce one here
|
||||
i += skipLines - 1
|
||||
} else if skipLines == skipUntilEndTag {
|
||||
startedAt := i
|
||||
startedAtMatch := dialectLineFilter.FindSubmatch(lines[startedAt])
|
||||
for ; i < len(lines); i++ {
|
||||
if match := endLineFilter.FindSubmatch(lines[i]); match != nil {
|
||||
if !bytes.Equal(match[1], startedAtMatch[1]) {
|
||||
return "", fmt.Errorf(`unexpected end tag %q for %q start at line %d`, string(match[0]), string(startedAtMatch[1]), startedAt)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if i == len(lines) {
|
||||
return "", fmt.Errorf(`didn't get end tag matching start %q at line %d`, string(startedAtMatch[1]), startedAt)
|
||||
}
|
||||
} else {
|
||||
output = append(output, lines[i])
|
||||
}
|
||||
}
|
||||
return string(bytes.Join(output, []byte("\n"))), nil
|
||||
}
|
||||
|
||||
func sqlUpgradeFunc(fileName string, lines [][]byte) upgradeFunc {
|
||||
return func(tx Transaction, db *Database) error {
|
||||
if skip, err := db.parseDialectFilter(lines[0]); err == nil && skip == skipNextLine {
|
||||
return nil
|
||||
} else if upgradeSQL, err := db.filterSQLUpgrade(lines); err != nil {
|
||||
panic(fmt.Errorf("failed to parse upgrade %s: %w", fileName, err))
|
||||
} else {
|
||||
_, err = tx.Exec(upgradeSQL)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func splitSQLUpgradeFunc(sqliteData, postgresData string) upgradeFunc {
|
||||
return func(tx Transaction, database *Database) (err error) {
|
||||
switch database.Dialect {
|
||||
case SQLite:
|
||||
_, err = tx.Exec(sqliteData)
|
||||
case Postgres:
|
||||
_, err = tx.Exec(postgresData)
|
||||
default:
|
||||
err = fmt.Errorf("unknown dialect %s", database.Dialect)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func parseSplitSQLUpgrade(name string, fs fullFS, skipNames map[string]struct{}) (from, to int, message string, fn upgradeFunc) {
|
||||
postgresName := fmt.Sprintf("%s.postgres.sql", name)
|
||||
sqliteName := fmt.Sprintf("%s.sqlite.sql", name)
|
||||
skipNames[postgresName] = struct{}{}
|
||||
skipNames[sqliteName] = struct{}{}
|
||||
postgresData, err := fs.ReadFile(postgresName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sqliteData, err := fs.ReadFile(sqliteName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
from, to, message, _, err = parseFileHeader(postgresData)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to parse header in %s: %w", postgresName, err))
|
||||
}
|
||||
sqliteFrom, sqliteTo, sqliteMessage, _, err := parseFileHeader(sqliteData)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to parse header in %s: %w", sqliteName, err))
|
||||
}
|
||||
if from != sqliteFrom || to != sqliteTo {
|
||||
panic(fmt.Errorf("mismatching versions in postgres and sqlite versions of %s: %d/%d -> %d/%d", name, from, sqliteFrom, to, sqliteTo))
|
||||
} else if message != sqliteMessage {
|
||||
panic(fmt.Errorf("mismatching message in postgres and sqlite versions of %s: %q != %q", name, message, sqliteMessage))
|
||||
}
|
||||
fn = splitSQLUpgradeFunc(string(sqliteData), string(postgresData))
|
||||
return
|
||||
}
|
||||
|
||||
type fullFS interface {
|
||||
fs.ReadFileFS
|
||||
fs.ReadDirFS
|
||||
}
|
||||
|
||||
var splitFileNameRegex = regexp.MustCompile(`^(.+)\.(postgres|sqlite)\.sql$`)
|
||||
|
||||
func (ut *UpgradeTable) RegisterFS(fs fullFS) {
|
||||
ut.RegisterFSPath(fs, ".")
|
||||
}
|
||||
|
||||
func (ut *UpgradeTable) RegisterFSPath(fs fullFS, dir string) {
|
||||
files, err := fs.ReadDir(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
skipNames := map[string]struct{}{}
|
||||
for _, file := range files {
|
||||
if file.IsDir() || !strings.HasSuffix(file.Name(), ".sql") {
|
||||
// do nothing
|
||||
} else if _, skip := skipNames[file.Name()]; skip {
|
||||
// also do nothing
|
||||
} else if splitName := splitFileNameRegex.FindStringSubmatch(file.Name()); splitName != nil {
|
||||
from, to, message, fn := parseSplitSQLUpgrade(splitName[1], fs, skipNames)
|
||||
ut.Register(from, to, message, fn)
|
||||
} else if data, err := fs.ReadFile(filepath.Join(dir, file.Name())); err != nil {
|
||||
panic(err)
|
||||
} else if from, to, message, lines, err := parseFileHeader(data); err != nil {
|
||||
panic(fmt.Errorf("failed to parse header in %s: %w", file.Name(), err))
|
||||
} else {
|
||||
ut.Register(from, to, message, sqlUpgradeFunc(file.Name(), lines))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user