Migrate on startup

This commit is contained in:
David Robertson 2023-05-10 12:12:05 +01:00
parent ce284370b0
commit d3a81adea2
No known key found for this signature in database
GPG Key ID: 903ECE108A39DEDD
2 changed files with 20 additions and 1 deletions

View File

@ -131,6 +131,11 @@ func main() {
}
}
err := sync2.MigrateDeviceIDs(args[EnvServer], args[EnvDB], args[EnvSecret], true)
if err != nil {
panic(err)
}
h2, h3 := syncv3.Setup(args[EnvServer], args[EnvDB], args[EnvSecret], syncv3.Opts{
Debug: args[EnvDebug] == "1",
AddPrometheusMetrics: args[EnvPrometheus] != "",

View File

@ -3,8 +3,10 @@ package sync2
import (
"crypto/sha256"
"fmt"
"github.com/getsentry/sentry-go"
"github.com/jmoiron/sqlx"
"github.com/matrix-org/sliding-sync/sqlutil"
"net/http"
"time"
)
@ -14,7 +16,19 @@ import (
// a no-op.
//
// This code will be removed in a future version of the proxy.
func MigrateDeviceIDs(db *sqlx.DB, secret string, whoamiClient Client, commit bool) error {
func MigrateDeviceIDs(destHomeserver, postgresURI, secret string, commit bool) error {
whoamiClient := &HTTPClient{
Client: &http.Client{
Timeout: 5 * time.Minute,
},
DestinationServer: destHomeserver,
}
db, err := sqlx.Open("postgres", postgresURI)
if err != nil {
sentry.CaptureException(err)
logger.Panic().Err(err).Str("uri", postgresURI).Msg("failed to open SQL DB")
}
return sqlutil.WithTransaction(db, func(txn *sqlx.Tx) (err error) {
migrated, err := isMigrated(txn)
if err != nil {