-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
database.go
141 lines (128 loc) · 4.27 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices.
// Copyright (C) 2021 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"database/sql"
"fmt"
"net/url"
"strings"
_ "github.com/jackc/pgx/v4/stdlib"
_ "github.com/mattn/go-sqlite3"
log "maunium.net/go/maulogger/v2"
)
type Database struct {
conn *sql.DB
scheme string
}
var knownSchemes = map[string]string{
"sqlite": "sqlite3",
"sqlite3": "sqlite3",
"postgres": "pgx",
"postgresql": "pgx",
"pgx": "pgx",
}
type DatabaseOpts struct {
MaxOpenConns int `yaml:"max_open_conns"`
MaxIdleConns int `yaml:"max_idle_conns"`
}
// Connect creates a new pgx connection pool.
func Connect(dbURL string, opts DatabaseOpts) (*Database, error) {
var localDB Database
parsedURL, err := url.Parse(dbURL)
if err != nil {
return nil, err
}
var ok bool
localDB.scheme, ok = knownSchemes[parsedURL.Scheme]
if !ok {
return nil, fmt.Errorf("unsupported database scheme '%s'", parsedURL.Scheme)
}
if localDB.scheme == "sqlite3" {
newDBURL := strings.TrimPrefix(parsedURL.Path, "/")
if len(newDBURL) == 0 {
return nil, fmt.Errorf("invalid database URL '%s', missing a slash?", dbURL)
}
dbURL = newDBURL
}
localDB.conn, err = sql.Open(localDB.scheme, dbURL)
localDB.conn.SetMaxOpenConns(opts.MaxOpenConns)
localDB.conn.SetMaxIdleConns(opts.MaxIdleConns)
return &localDB, err
}
type Upgrade struct {
Message string
Func func(conn *sql.Tx) error
}
var upgrades = []Upgrade{{
"Initial version",
func(conn *sql.Tx) error {
_, err := conn.Exec(`
CREATE TABLE targets (
appservice_id TEXT PRIMARY KEY,
bot_access_token TEXT NOT NULL,
hs_token TEXT NOT NULL,
address TEXT NOT NULL,
user_id TEXT NOT NULL,
device_id TEXT NOT NULL,
is_proxy BOOLEAN NOT NULL,
next_batch TEXT NOT NULL,
active BOOLEAN DEFAULT false
);
`)
return err
},
}}
func setVersion(conn *sql.Tx, version int) error {
_, err := conn.Exec("DELETE FROM version")
if err != nil {
return fmt.Errorf("failed to delete current version row: %w", err)
}
_, err = conn.Exec("INSERT INTO version VALUES ($1)", version)
if err != nil {
return fmt.Errorf("failed to insert new version row: %w", err)
}
return nil
}
// Upgrade updates the database schema to the latest version.
func (db *Database) Upgrade() error {
_, err := db.conn.Exec("CREATE TABLE IF NOT EXISTS version (version INTEGER PRIMARY KEY)")
if err != nil {
return fmt.Errorf("failed to ensure version table exists: %w", err)
}
var version int
err = db.conn.QueryRow("SELECT version FROM version").Scan(&version)
if err != nil && err != sql.ErrNoRows {
return fmt.Errorf("failed to get current database schema version: %w", err)
}
if len(upgrades) > version {
for index, upgrade := range upgrades[version:] {
newVersion := version + index + 1
log.Infofln("Updating database schema to v%d: %s", newVersion, upgrade.Message)
var tx *sql.Tx
if tx, err = db.conn.Begin(); err != nil {
return fmt.Errorf("failed to begin transaction to upgrade database schema to v%d: %w", newVersion, err)
} else if err = upgrade.Func(tx); err != nil {
return fmt.Errorf("failed to upgrade database schema to v%d: %w", newVersion, err)
} else if err = setVersion(tx, newVersion); err != nil {
return fmt.Errorf("failed to store new version v%d in database: %w", newVersion, err)
} else if err = tx.Commit(); err != nil {
return fmt.Errorf("failed to commit upgrade of database schema to v%d: %w", newVersion, err)
}
}
log.Infofln("Database schema update to v%d", len(upgrades))
}
return nil
}