Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate existing data to new schema on startup #98

Merged
merged 24 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ FROM docker.io/golang:1.19-alpine AS base
WORKDIR /build

RUN apk --update --no-cache add build-base git
ARG BINARYNAME=syncv3
RUN --mount=target=. \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
GIT_COMMIT=$(git rev-list -1 HEAD) && \
go build -ldflags "-X main.GitCommit=$GIT_COMMIT" -trimpath -o /out/ ./cmd/syncv3
go build -ldflags "-X main.GitCommit=$GIT_COMMIT" -trimpath -o /out/syncv3 "./cmd/$BINARYNAME"

FROM alpine:3.17

Expand All @@ -18,4 +19,7 @@ ENV SYNCV3_BINDADDR="0.0.0.0:8008"
EXPOSE 8008

WORKDIR /usr/bin
# It would be nice if the binary we exec was called $BINARYNAME here, but build args
# aren't expanded in ENTRYPOINT directives. Instead, we always call the output binary
# "syncv3". (See https://github.com/moby/moby/issues/18492)
ENTRYPOINT /usr/bin/syncv3
5 changes: 5 additions & 0 deletions cmd/syncv3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] != "",
Expand Down
30 changes: 30 additions & 0 deletions cmd/testmigration/main.go
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful to keep this around---this gives us a way to run the migration manually without having to startup the proxy proper.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"github.com/matrix-org/sliding-sync/sync2"
"os"
)

const (
// Required fields
EnvServer = "SYNCV3_SERVER"
EnvDB = "SYNCV3_DB"
EnvSecret = "SYNCV3_SECRET"

// Migration test only
EnvMigrationCommit = "SYNCV3_TEST_MIGRATION_COMMIT"
)

func main() {
args := map[string]string{
EnvServer: os.Getenv(EnvServer),
EnvDB: os.Getenv(EnvDB),
EnvSecret: os.Getenv(EnvSecret),
EnvMigrationCommit: os.Getenv(EnvMigrationCommit),
}

err := sync2.MigrateDeviceIDs(args[EnvServer], args[EnvDB], args[EnvSecret], args[EnvMigrationCommit] != "")
if err != nil {
panic(err)
}
}
5 changes: 5 additions & 0 deletions state/to_device_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ func (t *ToDeviceTable) DeleteMessagesUpToAndIncluding(userID, deviceID string,
}

func (t *ToDeviceTable) DeleteAllMessagesForDevice(userID, deviceID string) error {
// TODO: should these deletes take place in a transaction?
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
_, err := t.db.Exec(`DELETE FROM syncv3_to_device_messages WHERE user_id = $1 AND device_id = $2`, userID, deviceID)
if err != nil {
return err
}
_, err = t.db.Exec(`DELETE FROM syncv3_to_device_ack_pos WHERE user_id = $1 AND device_id = $2`, userID, deviceID)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion state/txn_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewTransactionsTable(db *sqlx.DB) *TransactionsTable {
// make sure tables are made
db.MustExec(`
CREATE TABLE IF NOT EXISTS syncv3_txns (
user_id TEXT NOT NULL, -- was actually device_id before migration
user_id TEXT NOT NULL,
device_id TEXT NOT NULL,
event_id TEXT NOT NULL,
txn_id TEXT NOT NULL,
Expand Down
Loading