From 3e85a9bb3ac71480ec3e8b0ffd2c322159a644e7 Mon Sep 17 00:00:00 2001 From: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com> Date: Mon, 27 Mar 2023 13:23:05 +0530 Subject: [PATCH] Updated query to support old mysql version (#22606) * Updated query to support old mysql version * Added tests * Using foundation for tests * Removed unused override params * Removed unused override params --- .../store/sqlstore/boards_migrator.go | 3 +++ .../store/sqlstore/data_migrations.go | 6 ++--- .../store/sqlstore/data_migrations_test.go | 23 +++++++++++++++++++ ...testDeDuplicateCategoryBoardsMigration.sql | 9 ++++++++ .../{helpers_test.go => helpers.go} | 4 ++++ .../boards/services/store/sqlstore/testlib.go | 1 + 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 server/boards/services/store/sqlstore/fixtures/testDeDuplicateCategoryBoardsMigration.sql rename server/boards/services/store/sqlstore/migrationstests/{helpers_test.go => helpers.go} (90%) diff --git a/server/boards/services/store/sqlstore/boards_migrator.go b/server/boards/services/store/sqlstore/boards_migrator.go index 99395e9e46..79afeddc66 100644 --- a/server/boards/services/store/sqlstore/boards_migrator.go +++ b/server/boards/services/store/sqlstore/boards_migrator.go @@ -231,6 +231,9 @@ func (bm *BoardsMigrator) MigrateToStep(step int) error { func (bm *BoardsMigrator) Interceptors() map[int]foundation.Interceptor { return map[int]foundation.Interceptor{ 18: bm.store.RunDeletedMembershipBoardsMigration, + 35: func() error { + return bm.store.RunDeDuplicateCategoryBoardsMigration(35) + }, } } diff --git a/server/boards/services/store/sqlstore/data_migrations.go b/server/boards/services/store/sqlstore/data_migrations.go index a1404afac8..8e18022319 100644 --- a/server/boards/services/store/sqlstore/data_migrations.go +++ b/server/boards/services/store/sqlstore/data_migrations.go @@ -863,10 +863,8 @@ func (s *SQLStore) doesDuplicateCategoryBoardsExist() (bool, error) { } func (s *SQLStore) runMySQLDeDuplicateCategoryBoardsMigration() error { - query := "WITH duplicates AS (SELECT id, ROW_NUMBER() OVER(PARTITION BY user_id, board_id) AS rownum " + - "FROM " + s.tablePrefix + "category_boards) " + - "DELETE " + s.tablePrefix + "category_boards FROM " + s.tablePrefix + "category_boards " + - "JOIN duplicates USING(id) WHERE duplicates.rownum > 1;" + query := "DELETE FROM " + s.tablePrefix + "category_boards WHERE id NOT IN " + + "(SELECT * FROM ( SELECT min(id) FROM " + s.tablePrefix + "category_boards GROUP BY user_id, board_id ) as data)" if _, err := s.db.Exec(query); err != nil { s.logger.Error("Failed to de-duplicate data in category_boards table", mlog.Err(err)) } diff --git a/server/boards/services/store/sqlstore/data_migrations_test.go b/server/boards/services/store/sqlstore/data_migrations_test.go index e5aae4de52..5a44f9ca2e 100644 --- a/server/boards/services/store/sqlstore/data_migrations_test.go +++ b/server/boards/services/store/sqlstore/data_migrations_test.go @@ -7,6 +7,9 @@ import ( "testing" "time" + "github.com/mattermost/mattermost-server/v6/server/boards/services/store/sqlstore/migrationstests" + "github.com/mgdelacroix/foundation" + "github.com/mattermost/mattermost-server/v6/server/boards/model" "github.com/stretchr/testify/assert" @@ -263,3 +266,23 @@ func TestCheckForMismatchedCollation(t *testing.T) { } }) } + +func TestRunDeDuplicateCategoryBoardsMigration(t *testing.T) { + RunStoreTestsWithFoundation(t, func(t *testing.T, f *foundation.Foundation) { + th, tearDown := migrationstests.SetupTestHelper(t, f) + defer tearDown() + + th.F().MigrateToStepSkippingLastInterceptor(35). + ExecFile("./fixtures/testDeDuplicateCategoryBoardsMigration.sql") + + th.F().RunInterceptor(35) + + // verifying count of rows + var count int + countQuery := "SELECT COUNT(*) FROM focalboard_category_boards" + row := th.F().DB().QueryRow(countQuery) + err := row.Scan(&count) + assert.NoError(t, err) + assert.Equal(t, 4, count) + }) +} diff --git a/server/boards/services/store/sqlstore/fixtures/testDeDuplicateCategoryBoardsMigration.sql b/server/boards/services/store/sqlstore/fixtures/testDeDuplicateCategoryBoardsMigration.sql new file mode 100644 index 0000000000..69a7dc9bde --- /dev/null +++ b/server/boards/services/store/sqlstore/fixtures/testDeDuplicateCategoryBoardsMigration.sql @@ -0,0 +1,9 @@ +INSERT INTO focalboard_category_boards(id, user_id, category_id, board_id, create_at, update_at, sort_order) +VALUES + ('id_1', 'user_id_1', 'category_id_1', 'board_id_1', 0, 0, 0), + ('id_2', 'user_id_1', 'category_id_2', 'board_id_1', 0, 0, 0), + ('id_3', 'user_id_1', 'category_id_3', 'board_id_1', 0, 0, 0), + ('id_4', 'user_id_2', 'category_id_4', 'board_id_2', 0, 0, 0), + ('id_5', 'user_id_2', 'category_id_5', 'board_id_2', 0, 0, 0), + ('id_6', 'user_id_3', 'category_id_6', 'board_id_3', 0, 0, 0), + ('id_7', 'user_id_4', 'category_id_6', 'board_id_4', 0, 0, 0); diff --git a/server/boards/services/store/sqlstore/migrationstests/helpers_test.go b/server/boards/services/store/sqlstore/migrationstests/helpers.go similarity index 90% rename from server/boards/services/store/sqlstore/migrationstests/helpers_test.go rename to server/boards/services/store/sqlstore/migrationstests/helpers.go index a6d4696f14..a674d5f988 100644 --- a/server/boards/services/store/sqlstore/migrationstests/helpers_test.go +++ b/server/boards/services/store/sqlstore/migrationstests/helpers.go @@ -22,6 +22,10 @@ func (th *TestHelper) IsMySQL() bool { return th.f.DB().DriverName() == "mysql" } +func (th *TestHelper) F() *foundation.Foundation { + return th.f +} + func SetupTestHelper(t *testing.T, f *foundation.Foundation) (*TestHelper, func()) { th := &TestHelper{t, f} diff --git a/server/boards/services/store/sqlstore/testlib.go b/server/boards/services/store/sqlstore/testlib.go index 9ea7de4301..a79b2a1643 100644 --- a/server/boards/services/store/sqlstore/testlib.go +++ b/server/boards/services/store/sqlstore/testlib.go @@ -50,6 +50,7 @@ func NewStoreType(name string, driver string, skipMigrations bool) *storeType { DB: sqlDB, IsPlugin: false, // ToDo: to be removed } + store, err := New(storeParams) if err != nil { panic(fmt.Sprintf("cannot create store: %s", err))