From ad78f81b1056968190d8d44310f70366a5afc235 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 18 Jan 2021 16:48:47 +0800 Subject: [PATCH 1/2] fix unique index for binance self trade --- .../20210118163847_fix_unique_index.sql | 15 +++++++ .../20210118163847_fix_unique_index.go | 43 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 migrations/20210118163847_fix_unique_index.sql create mode 100644 pkg/migrations/20210118163847_fix_unique_index.go diff --git a/migrations/20210118163847_fix_unique_index.sql b/migrations/20210118163847_fix_unique_index.sql new file mode 100644 index 000000000..96b56e549 --- /dev/null +++ b/migrations/20210118163847_fix_unique_index.sql @@ -0,0 +1,15 @@ +-- +up +-- +begin +ALTER TABLE `trades` DROP INDEX `id`; +-- +end +-- +begin +ALTER TABLE `trades` ADD INDEX UNIQUE `id` ON (`exchange`,`symbol`, `side`, `id`); +-- +end + +-- +down +-- +begin +ALTER TABLE `trades` DROP INDEX `id`; +-- +end +-- +begin +ALTER TABLE `trades` ADD INDEX UNIQUE `id` ON (`id`); +-- +end diff --git a/pkg/migrations/20210118163847_fix_unique_index.go b/pkg/migrations/20210118163847_fix_unique_index.go new file mode 100644 index 000000000..bc00f7a93 --- /dev/null +++ b/pkg/migrations/20210118163847_fix_unique_index.go @@ -0,0 +1,43 @@ +package migrations + +import ( + "context" + + "github.com/c9s/rockhopper" +) + +func init() { + rockhopper.AddMigration(upFixUniqueIndex, downFixUniqueIndex) +} + +func upFixUniqueIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` DROP INDEX `id`;") + if err != nil { + return err + } + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` ADD INDEX UNIQUE `id` ON (`exchange`,`symbol`, `side`, `id`);") + if err != nil { + return err + } + + return err +} + +func downFixUniqueIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is rolled back. + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` DROP INDEX `id`;") + if err != nil { + return err + } + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` ADD INDEX UNIQUE `id` ON (`id`);") + if err != nil { + return err + } + + return err +} From 463a9bf1b7eb840a083f2a407b8ab45db751e9b5 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 18 Jan 2021 16:56:01 +0800 Subject: [PATCH 2/2] fix sql syntax --- migrations/20210118163847_fix_unique_index.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/migrations/20210118163847_fix_unique_index.sql b/migrations/20210118163847_fix_unique_index.sql index 96b56e549..d61819f07 100644 --- a/migrations/20210118163847_fix_unique_index.sql +++ b/migrations/20210118163847_fix_unique_index.sql @@ -3,7 +3,7 @@ ALTER TABLE `trades` DROP INDEX `id`; -- +end -- +begin -ALTER TABLE `trades` ADD INDEX UNIQUE `id` ON (`exchange`,`symbol`, `side`, `id`); +ALTER TABLE `trades` ADD UNIQUE INDEX `id` (`exchange`,`symbol`, `side`, `id`); -- +end -- +down @@ -11,5 +11,5 @@ ALTER TABLE `trades` ADD INDEX UNIQUE `id` ON (`exchange`,`symbol`, `side`, `id` ALTER TABLE `trades` DROP INDEX `id`; -- +end -- +begin -ALTER TABLE `trades` ADD INDEX UNIQUE `id` ON (`id`); +ALTER TABLE `trades` ADD UNIQUE INDEX `id` (`id`); -- +end