mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-25 15:53:09 -06:00
Support up/downgrading from cmd/goose
This commit is contained in:
parent
dd79f69974
commit
5a63ec5895
|
|
@ -8,17 +8,35 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
// Example complex Go migration import:
|
pgaccounts "github.com/matrix-org/dendrite/userapi/storage/accounts/postgres/deltas"
|
||||||
// _ "github.com/matrix-org/dendrite/serverkeyapi/storage/postgres/deltas"
|
slaccounts "github.com/matrix-org/dendrite/userapi/storage/accounts/sqlite3/deltas"
|
||||||
|
pgdevices "github.com/matrix-org/dendrite/userapi/storage/devices/postgres/deltas"
|
||||||
|
sldevices "github.com/matrix-org/dendrite/userapi/storage/devices/sqlite3/deltas"
|
||||||
"github.com/pressly/goose"
|
"github.com/pressly/goose"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
AppService = "appservice"
|
||||||
|
FederationSender = "federationsender"
|
||||||
|
KeyServer = "keyserver"
|
||||||
|
MediaAPI = "mediaapi"
|
||||||
|
RoomServer = "roomserver"
|
||||||
|
SigningKeyServer = "signingkeyserver"
|
||||||
|
SyncAPI = "syncapi"
|
||||||
|
UserAPIAccounts = "userapi_accounts"
|
||||||
|
UserAPIDevices = "userapi_devices"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
flags = flag.NewFlagSet("goose", flag.ExitOnError)
|
dir = flags.String("dir", "", "directory with migration files")
|
||||||
dir = flags.String("dir", ".", "directory with migration files")
|
flags = flag.NewFlagSet("goose", flag.ExitOnError)
|
||||||
|
component = flags.String("component", "", "dendrite component name")
|
||||||
|
knownDBs = []string{
|
||||||
|
AppService, FederationSender, KeyServer, MediaAPI, RoomServer, SigningKeyServer, SyncAPI, UserAPIAccounts, UserAPIDevices,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -37,19 +55,20 @@ Drivers:
|
||||||
sqlite3
|
sqlite3
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
goose -d roomserver/storage/sqlite3/deltas sqlite3 ./roomserver.db status
|
goose -component roomserver sqlite3 ./roomserver.db status
|
||||||
goose -d roomserver/storage/sqlite3/deltas sqlite3 ./roomserver.db up
|
goose -component roomserver sqlite3 ./roomserver.db up
|
||||||
|
|
||||||
goose -d roomserver/storage/postgres/deltas postgres "user=dendrite dbname=dendrite sslmode=disable" status
|
goose -component roomserver postgres "user=dendrite dbname=dendrite sslmode=disable" status
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
-component string
|
||||||
-dir string
|
Dendrite component name e.g roomserver, signingkeyserver, clientapi, syncapi
|
||||||
directory with migration files (default ".")
|
|
||||||
-table string
|
-table string
|
||||||
migrations table name (default "goose_db_version")
|
migrations table name (default "goose_db_version")
|
||||||
-h print help
|
-h print help
|
||||||
-v enable verbose mode
|
-v enable verbose mode
|
||||||
|
-dir string
|
||||||
|
directory with migration files, only relevant when creating new migrations.
|
||||||
-version
|
-version
|
||||||
print version
|
print version
|
||||||
|
|
||||||
|
|
@ -74,6 +93,25 @@ Commands:
|
||||||
fmt.Println("engine must be one of 'sqlite3' or 'postgres'")
|
fmt.Println("engine must be one of 'sqlite3' or 'postgres'")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
knownComponent := false
|
||||||
|
for _, c := range knownDBs {
|
||||||
|
if c == *component {
|
||||||
|
knownComponent = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !knownComponent {
|
||||||
|
fmt.Printf("component must be one of %v\n", knownDBs)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if engine == "sqlite3" {
|
||||||
|
loadSQLiteDeltas(*component)
|
||||||
|
} else {
|
||||||
|
loadPostgresDeltas(*component)
|
||||||
|
}
|
||||||
|
|
||||||
dbstring, command := args[1], args[2]
|
dbstring, command := args[1], args[2]
|
||||||
|
|
||||||
db, err := goose.OpenDBWithDriver(engine, dbstring)
|
db, err := goose.OpenDBWithDriver(engine, dbstring)
|
||||||
|
|
@ -92,7 +130,30 @@ Commands:
|
||||||
arguments = append(arguments, args[3:]...)
|
arguments = append(arguments, args[3:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := goose.Run(command, db, *dir, arguments...); err != nil {
|
// goose demands a directory even though we don't use it for upgrades
|
||||||
|
d := *dir
|
||||||
|
if d == "" {
|
||||||
|
d = os.TempDir()
|
||||||
|
}
|
||||||
|
if err := goose.Run(command, db, d, arguments...); err != nil {
|
||||||
log.Fatalf("goose %v: %v", command, err)
|
log.Fatalf("goose %v: %v", command, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadSQLiteDeltas(component string) {
|
||||||
|
switch component {
|
||||||
|
case UserAPIAccounts:
|
||||||
|
slaccounts.LoadFromGoose()
|
||||||
|
case UserAPIDevices:
|
||||||
|
sldevices.LoadFromGoose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadPostgresDeltas(component string) {
|
||||||
|
switch component {
|
||||||
|
case UserAPIAccounts:
|
||||||
|
pgaccounts.LoadFromGoose()
|
||||||
|
case UserAPIDevices:
|
||||||
|
pgdevices.LoadFromGoose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
"github.com/pressly/goose"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func LoadFromGoose() {
|
||||||
|
goose.AddMigration(UpIsActive, DownIsActive)
|
||||||
|
}
|
||||||
|
|
||||||
func LoadIsActive(m *sqlutil.Migrations) {
|
func LoadIsActive(m *sqlutil.Migrations) {
|
||||||
m.AddMigration(UpIsActive, DownIsActive)
|
m.AddMigration(UpIsActive, DownIsActive)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
"github.com/pressly/goose"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func LoadFromGoose() {
|
||||||
|
goose.AddMigration(UpIsActive, DownIsActive)
|
||||||
|
}
|
||||||
|
|
||||||
func LoadIsActive(m *sqlutil.Migrations) {
|
func LoadIsActive(m *sqlutil.Migrations) {
|
||||||
m.AddMigration(UpIsActive, DownIsActive)
|
m.AddMigration(UpIsActive, DownIsActive)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
"github.com/pressly/goose"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func LoadFromGoose() {
|
||||||
|
goose.AddMigration(UpLastSeenTSIP, DownLastSeenTSIP)
|
||||||
|
}
|
||||||
|
|
||||||
func LoadLastSeenTSIP(m *sqlutil.Migrations) {
|
func LoadLastSeenTSIP(m *sqlutil.Migrations) {
|
||||||
m.AddMigration(UpLastSeenTSIP, DownLastSeenTSIP)
|
m.AddMigration(UpLastSeenTSIP, DownLastSeenTSIP)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
"github.com/pressly/goose"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func LoadFromGoose() {
|
||||||
|
goose.AddMigration(UpLastSeenTSIP, DownLastSeenTSIP)
|
||||||
|
}
|
||||||
|
|
||||||
func LoadLastSeenTSIP(m *sqlutil.Migrations) {
|
func LoadLastSeenTSIP(m *sqlutil.Migrations) {
|
||||||
m.AddMigration(UpLastSeenTSIP, DownLastSeenTSIP)
|
m.AddMigration(UpLastSeenTSIP, DownLastSeenTSIP)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue