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
Этот коммит содержится в:
Harshil Sharma
2023-03-27 13:23:05 +05:30
коммит произвёл GitHub
родитель ee068726bc
Коммит 3e85a9bb3a
6 изменённых файлов: 42 добавлений и 4 удалений

Просмотреть файл

@@ -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)
},
}
}

Просмотреть файл

@@ -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))
}

Просмотреть файл

@@ -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)
})
}

Просмотреть файл

@@ -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);

Просмотреть файл

@@ -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}

Просмотреть файл

@@ -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))