Skip to content

Commit

Permalink
Fix system settings functions and move last migration to morph
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdelacroix committed May 17, 2024
1 parent c3e6f8b commit 49aa79c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
18 changes: 9 additions & 9 deletions server/store/sqlstore/data_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
)

func (s *SQLStore) runMigrationRemoteID(remoteID string) error {
setting, err := s.getSystemSetting(RemoteIDMigrationKey)
setting, err := s.getSystemSetting(s.db, RemoteIDMigrationKey)
if err != nil {
return fmt.Errorf("cannot get Remote ID migration state: %w", err)
}
Expand All @@ -43,7 +43,7 @@ func (s *SQLStore) runMigrationRemoteID(remoteID string) error {
return qErr
}

if err := s.setSystemSetting(RemoteIDMigrationKey, strconv.FormatBool(true)); err != nil {
if err := s.setSystemSetting(s.db, RemoteIDMigrationKey, strconv.FormatBool(true)); err != nil {
return fmt.Errorf("cannot mark Remote ID migration as completed: %w", err)
}

Expand All @@ -53,7 +53,7 @@ func (s *SQLStore) runMigrationRemoteID(remoteID string) error {
}

func (s *SQLStore) runSetEmailVerifiedToTrueForRemoteUsers(remoteID string) error {
setting, err := s.getSystemSetting(SetEmailVerifiedToTrueForRemoteUsersMigrationKey)
setting, err := s.getSystemSetting(s.db, SetEmailVerifiedToTrueForRemoteUsersMigrationKey)
if err != nil {
return fmt.Errorf("cannot get Set Email Verified to True for Remote Users migration state: %w", err)
}
Expand All @@ -77,7 +77,7 @@ func (s *SQLStore) runSetEmailVerifiedToTrueForRemoteUsers(remoteID string) erro
return qErr
}

if err := s.setSystemSetting(SetEmailVerifiedToTrueForRemoteUsersMigrationKey, strconv.FormatBool(true)); err != nil {
if err := s.setSystemSetting(s.db, SetEmailVerifiedToTrueForRemoteUsersMigrationKey, strconv.FormatBool(true)); err != nil {
return fmt.Errorf("cannot mark Set Email Verified to True for Remote Users migration as completed: %w", err)
}

Expand All @@ -87,7 +87,7 @@ func (s *SQLStore) runSetEmailVerifiedToTrueForRemoteUsers(remoteID string) erro
}

func (s *SQLStore) runMSTeamUserIDDedup() error {
setting, err := s.getSystemSetting(MSTeamUserIDDedupMigrationKey)
setting, err := s.getSystemSetting(s.db, MSTeamUserIDDedupMigrationKey)
if err != nil {
return fmt.Errorf("cannot get MSTeam User ID Dedup migration state: %w", err)
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func (s *SQLStore) runMSTeamUserIDDedup() error {
return err
}

if err := s.setSystemSetting(MSTeamUserIDDedupMigrationKey, strconv.FormatBool(true)); err != nil {
if err := s.setSystemSetting(s.db, MSTeamUserIDDedupMigrationKey, strconv.FormatBool(true)); err != nil {
return fmt.Errorf("cannot mark MSTeam User ID Dedup migration as completed: %w", err)
}

Expand All @@ -182,7 +182,7 @@ func (s *SQLStore) runMSTeamUserIDDedup() error {
}

func (s *SQLStore) runWhitelistedUsersMigration() error {
setting, err := s.getSystemSetting(WhitelistedUsersMigrationKey)
setting, err := s.getSystemSetting(s.db, WhitelistedUsersMigrationKey)
if err != nil {
return fmt.Errorf("cannot get Whitelisted Users migration state: %w", err)
}
Expand All @@ -198,7 +198,7 @@ func (s *SQLStore) runWhitelistedUsersMigration() error {

if !oldWhitelistToProcess {
// migration already done, no rows to process
if sErr := s.setSystemSetting(WhitelistedUsersMigrationKey, strconv.FormatBool(true)); sErr != nil {
if sErr := s.setSystemSetting(s.db, WhitelistedUsersMigrationKey, strconv.FormatBool(true)); sErr != nil {
return fmt.Errorf("cannot mark Whitelisted Users migration as completed: %w", sErr)
}

Expand Down Expand Up @@ -238,7 +238,7 @@ func (s *SQLStore) runWhitelistedUsersMigration() error {
return err
}

if err := s.setSystemSetting(WhitelistedUsersMigrationKey, strconv.FormatBool(true)); err != nil {
if err := s.setSystemSetting(s.db, WhitelistedUsersMigrationKey, strconv.FormatBool(true)); err != nil {
return fmt.Errorf("cannot mark Whitelisted Users migration as completed: %w", err)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE msteamssync_users ADD COLUMN IF NOT EXISTS LastChatSentAt BIGINT NOT NULL DEFAULT 0;
ALTER TABLE msteamssync_users ADD COLUMN IF NOT EXISTS LastChatReceivedAt BIGINT NOT NULL DEFAULT 0;
16 changes: 4 additions & 12 deletions server/store/sqlstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (s *SQLStore) Init(remoteID string) error {
return nil
}

func (s *SQLStore) getSystemSetting(key string) (string, error) {
scanner := s.getQueryBuilder().
func (s *SQLStore) getSystemSetting(db sq.BaseRunner, key string) (string, error) {
scanner := s.getQueryBuilder(db).
Select("value").
From(systemSettingsTableName).
Where(sq.Eq{"id": key}).
Expand All @@ -80,8 +80,8 @@ func (s *SQLStore) getSystemSetting(key string) (string, error) {
return result, nil
}

func (s *SQLStore) setSystemSetting(id, value string) error {
_, err := s.getQueryBuilder().
func (s *SQLStore) setSystemSetting(db sq.BaseRunner, id, value string) error {
_, err := s.getQueryBuilder(db).
Insert(systemSettingsTableName).
Columns("id", "value").
Values(id, value).
Expand All @@ -91,14 +91,6 @@ func (s *SQLStore) setSystemSetting(id, value string) error {
return err
}

if err := s.addColumn(usersTableName, "LastChatSentAt", "BIGINT NOT NULL DEFAULT 0"); err != nil {
return err
}

if err := s.addColumn(usersTableName, "LastChatReceivedAt", "BIGINT NOT NULL DEFAULT 0"); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit 49aa79c

Please sign in to comment.