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

Add DB replica support #602

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 6 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,14 @@ func (p *Plugin) onActivate() error {
return dbErr
}

replica, repErr := p.apiClient.Store.GetReplicaDB()
if repErr != nil {
return repErr
}

store := sqlstore.New(
db,
replica,
p.API,
func() []string { return strings.Split(p.configuration.EnabledTeams, ",") },
func() []byte { return []byte(p.configuration.EncryptionKey) },
Expand Down
1 change: 1 addition & 0 deletions server/store/sqlstore/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func setupTestStore(t *testing.T) (*SQLStore, *plugintest.API) {
store := &SQLStore{}
store.api = api
store.db = db
store.replica = db

err := store.createTable("Teams", "Id VARCHAR(255), DisplayName VARCHAR(255)")
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions server/store/sqlstore/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (s *SQLStore) runMigrationRemoteID(remoteID string) error {
_, err := s.getQueryBuilder().Update("Users").Set("RemoteID", remoteID).Where(sq.And{
_, err := s.getMasterQueryBuilder().Update("Users").Set("RemoteID", remoteID).Where(sq.And{
sq.NotEq{"RemoteID": nil},
sq.NotEq{"RemoteID": ""},
sq.Expr("RemoteID NOT IN (SELECT remoteid FROM remoteclusters)"),
Expand All @@ -19,7 +19,7 @@ func (s *SQLStore) runMigrationRemoteID(remoteID string) error {
}

func (s *SQLStore) runSetEmailVerifiedToTrueForRemoteUsers(remoteID string) error {
_, err := s.getQueryBuilder().
_, err := s.getMasterQueryBuilder().
Update("Users").
Set("EmailVerified", true).
Where(sq.And{
Expand All @@ -37,7 +37,7 @@ const (

func (s *SQLStore) runMSTeamUserIDDedup() error {
// get all users with duplicate msteamsuserid
rows, err := s.getQueryBuilder().Select(
rows, err := s.getReplicaQueryBuilder().Select(
"mmuserid",
"msteamsuserid",
"remoteid",
Expand Down Expand Up @@ -103,7 +103,7 @@ func (s *SQLStore) runMSTeamUserIDDedup() error {
}

s.api.LogInfo("Deleting duplicates")
_, err = s.getQueryBuilder().Delete(usersTableName).
_, err = s.getMasterQueryBuilder().Delete(usersTableName).
Where(orCond).
Exec()

Expand All @@ -129,7 +129,7 @@ func (s *SQLStore) ensureMigrationWhitelistedUsers() error {
// as being added to the old whitelist only happened after successful connection.

// has-connected users (presently and previously)
_, err = s.getQueryBuilder().
_, err = s.getMasterQueryBuilder().
Update(usersTableName).
Set("lastConnectAt", now.UnixMicro()).
Where(sq.Or{
Expand All @@ -142,7 +142,7 @@ func (s *SQLStore) ensureMigrationWhitelistedUsers() error {
}

// only previously-connected
_, err = s.getQueryBuilder().
_, err = s.getMasterQueryBuilder().
Update(usersTableName).
Set("lastDisconnectAt", now.UnixMicro()).
Where(sq.And{
Expand Down
6 changes: 3 additions & 3 deletions server/store/sqlstore/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestRunMSTeamUserIDDedup(t *testing.T) {
assert.NoError(err)
defer func() { _ = store.createMSTeamsUserIDUniqueIndex() }()

res, err := store.getQueryBuilder().Insert("users").
res, err := store.getMasterQueryBuilder().Insert("users").
Columns("id", "createat", "remoteid").
Values(userID1Remote, 100, "remote-id").
Values(userID2, 200, "").
Expand All @@ -62,7 +62,7 @@ func TestRunMSTeamUserIDDedup(t *testing.T) {

// ms teams user 1 will have all 3 users;
// user1 and 3 should be removed after the dedup because user 2 is a real user
res, err = store.getQueryBuilder().Insert(usersTableName).Columns("mmuserid", "msteamsuserid").
res, err = store.getMasterQueryBuilder().Insert(usersTableName).Columns("mmuserid", "msteamsuserid").
Values(userID1Remote, teamsUserA).
Values(userID2, teamsUserA).
Values(userID3Remote, teamsUserA).
Expand All @@ -82,7 +82,7 @@ func TestRunMSTeamUserIDDedup(t *testing.T) {
err = store.runMSTeamUserIDDedup()
assert.NoError(err)

rows, err := store.getQueryBuilder().Select("mmuserid", "msteamsuserid").From(usersTableName).Query()
rows, err := store.getMasterQueryBuilder().Select("mmuserid", "msteamsuserid").From(usersTableName).Query()
assert.NoError(err)
var found int
for rows.Next() {
Expand Down
Loading
Loading