diff --git a/server/store/sqlstore/data_migrations.go b/server/store/sqlstore/data_migrations.go index ed92db46..65637e7a 100644 --- a/server/store/sqlstore/data_migrations.go +++ b/server/store/sqlstore/data_migrations.go @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } diff --git a/server/store/sqlstore/migrations/000015_users_add_lastchatsentat_lastchatreceivedat_columns.down.sql b/server/store/sqlstore/migrations/000015_users_add_lastchatsentat_lastchatreceivedat_columns.down.sql new file mode 100644 index 00000000..e0ac49d1 --- /dev/null +++ b/server/store/sqlstore/migrations/000015_users_add_lastchatsentat_lastchatreceivedat_columns.down.sql @@ -0,0 +1 @@ +SELECT 1; diff --git a/server/store/sqlstore/migrations/000015_users_add_lastchatsentat_lastchatreceivedat_columns.up.sql b/server/store/sqlstore/migrations/000015_users_add_lastchatsentat_lastchatreceivedat_columns.up.sql new file mode 100644 index 00000000..fcd0599b --- /dev/null +++ b/server/store/sqlstore/migrations/000015_users_add_lastchatsentat_lastchatreceivedat_columns.up.sql @@ -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; diff --git a/server/store/sqlstore/store.go b/server/store/sqlstore/store.go index 2f474df1..68f7aaf9 100644 --- a/server/store/sqlstore/store.go +++ b/server/store/sqlstore/store.go @@ -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}). @@ -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). @@ -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 }