From ea27a291dbfed26389eafafa3e8f4290b23c126f Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 17 Feb 2021 17:28:05 +0800 Subject: [PATCH] compile and update migration package --- pkg/migrations/20200721225616_trades.go | 33 -------- pkg/migrations/20200819054742_trade_index.go | 53 ------------ pkg/migrations/20201102222546_orders.go | 33 -------- .../20201103173342_trades_add_order_id.go | 33 -------- .../20201105092857_trades_index_fix.go | 83 ------------------- .../20201105093056_orders_add_index.go | 43 ---------- pkg/migrations/20201106114742_klines.go | 73 ---------------- .../20201211175751_fix_symbol_length.go | 43 ---------- .../20210118163847_fix_unique_index.go | 43 ---------- .../20210119232826_add_margin_columns.go | 43 ---------- ...210129182704_trade_price_quantity_index.go | 33 -------- pkg/migrations/mysql/20200721225616_trades.go | 3 +- .../mysql/20200819054742_trade_index.go | 3 +- pkg/migrations/mysql/20201102222546_orders.go | 3 +- .../20201103173342_trades_add_order_id.go | 3 +- .../mysql/20201105092857_trades_index_fix.go | 3 +- .../mysql/20201105093056_orders_add_index.go | 3 +- pkg/migrations/mysql/20201106114742_klines.go | 3 +- .../mysql/20201211175751_fix_symbol_length.go | 3 +- .../mysql/20210118163847_fix_unique_index.go | 3 +- .../20210119232826_add_margin_columns.go | 3 +- ...210129182704_trade_price_quantity_index.go | 3 +- .../mysql/20210215203116_add_pnl_column.go | 44 ++++++++++ pkg/migrations/mysql/migration_api.go | 50 +++++++++++ .../sqlite3/20200721225616_trades.go | 3 +- .../sqlite3/20200819054742_trade_index.go | 3 +- .../sqlite3/20201102222546_orders.go | 3 +- .../20201103173342_trades_add_order_id.go | 3 +- .../20201105092857_trades_index_fix.go | 3 +- .../20201105093056_orders_add_index.go | 3 +- .../sqlite3/20201106114742_klines.go | 3 +- .../20201211175751_fix_symbol_length.go | 3 +- .../20210118163847_fix_unique_index.go | 3 +- .../20210119232826_add_margin_columns.go | 3 +- ...210129182704_trade_price_quantity_index.go | 3 +- .../sqlite3/20210215203111_add_pnl_column.go | 44 ++++++++++ pkg/migrations/sqlite3/migration_api.go | 50 +++++++++++ 37 files changed, 232 insertions(+), 535 deletions(-) delete mode 100644 pkg/migrations/20200721225616_trades.go delete mode 100644 pkg/migrations/20200819054742_trade_index.go delete mode 100644 pkg/migrations/20201102222546_orders.go delete mode 100644 pkg/migrations/20201103173342_trades_add_order_id.go delete mode 100644 pkg/migrations/20201105092857_trades_index_fix.go delete mode 100644 pkg/migrations/20201105093056_orders_add_index.go delete mode 100644 pkg/migrations/20201106114742_klines.go delete mode 100644 pkg/migrations/20201211175751_fix_symbol_length.go delete mode 100644 pkg/migrations/20210118163847_fix_unique_index.go delete mode 100644 pkg/migrations/20210119232826_add_margin_columns.go delete mode 100644 pkg/migrations/20210129182704_trade_price_quantity_index.go create mode 100644 pkg/migrations/mysql/20210215203116_add_pnl_column.go create mode 100644 pkg/migrations/mysql/migration_api.go create mode 100644 pkg/migrations/sqlite3/20210215203111_add_pnl_column.go create mode 100644 pkg/migrations/sqlite3/migration_api.go diff --git a/pkg/migrations/20200721225616_trades.go b/pkg/migrations/20200721225616_trades.go deleted file mode 100644 index 12f449bbd..000000000 --- a/pkg/migrations/20200721225616_trades.go +++ /dev/null @@ -1,33 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upTrades, downTrades) -} - -func upTrades(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "CREATE TABLE `trades`\n(\n `gid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n `id` BIGINT UNSIGNED,\n `exchange` VARCHAR(24) NOT NULL DEFAULT '',\n `symbol` VARCHAR(8) NOT NULL,\n `price` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `quantity` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `quote_quantity` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `fee` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `fee_currency` VARCHAR(4) NOT NULL,\n `is_buyer` BOOLEAN NOT NULL DEFAULT FALSE,\n `is_maker` BOOLEAN NOT NULL DEFAULT FALSE,\n `side` VARCHAR(4) NOT NULL DEFAULT '',\n `traded_at` DATETIME(3) NOT NULL,\n PRIMARY KEY (`gid`),\n UNIQUE KEY `id` (`id`)\n);") - if err != nil { - return err - } - - return err -} - -func downTrades(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is rolled back. - - _, err = tx.ExecContext(ctx, "DROP TABLE `trades`;") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20200819054742_trade_index.go b/pkg/migrations/20200819054742_trade_index.go deleted file mode 100644 index 59c7b89ef..000000000 --- a/pkg/migrations/20200819054742_trade_index.go +++ /dev/null @@ -1,53 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upTradeIndex, downTradeIndex) -} - -func upTradeIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_symbol ON trades(symbol);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_symbol_fee_currency ON trades(symbol, fee_currency, traded_at);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_traded_at_symbol ON trades(traded_at, symbol);") - if err != nil { - return err - } - - return err -} - -func downTradeIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is rolled back. - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_symbol ON trades;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_symbol_fee_currency ON trades;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_traded_at_symbol ON trades;") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20201102222546_orders.go b/pkg/migrations/20201102222546_orders.go deleted file mode 100644 index 04af54150..000000000 --- a/pkg/migrations/20201102222546_orders.go +++ /dev/null @@ -1,33 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upOrders, downOrders) -} - -func upOrders(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "CREATE TABLE `orders`\n(\n `gid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n `exchange` VARCHAR(24) NOT NULL DEFAULT '',\n -- order_id is the order id returned from the exchange\n `order_id` BIGINT UNSIGNED NOT NULL,\n `client_order_id` VARCHAR(42) NOT NULL DEFAULT '',\n `order_type` VARCHAR(16) NOT NULL,\n `symbol` VARCHAR(8) NOT NULL,\n `status` VARCHAR(12) NOT NULL,\n `time_in_force` VARCHAR(4) NOT NULL,\n `price` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `stop_price` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `quantity` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `executed_quantity` DECIMAL(16, 8) UNSIGNED NOT NULL DEFAULT 0.0,\n `side` VARCHAR(4) NOT NULL DEFAULT '',\n `is_working` BOOL NOT NULL DEFAULT FALSE,\n `created_at` DATETIME(3) NOT NULL,\n `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),\n PRIMARY KEY (`gid`)\n);") - if err != nil { - return err - } - - return err -} - -func downOrders(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is rolled back. - - _, err = tx.ExecContext(ctx, "DROP TABLE `orders`;") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20201103173342_trades_add_order_id.go b/pkg/migrations/20201103173342_trades_add_order_id.go deleted file mode 100644 index 596624746..000000000 --- a/pkg/migrations/20201103173342_trades_add_order_id.go +++ /dev/null @@ -1,33 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upTradesAddOrderId, downTradesAddOrderId) -} - -func upTradesAddOrderId(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "ALTER TABLE `trades`\n ADD COLUMN `order_id` BIGINT UNSIGNED NOT NULL;") - if err != nil { - return err - } - - return err -} - -func downTradesAddOrderId(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`\n DROP COLUMN `order_id`;") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20201105092857_trades_index_fix.go b/pkg/migrations/20201105092857_trades_index_fix.go deleted file mode 100644 index 72b90f321..000000000 --- a/pkg/migrations/20201105092857_trades_index_fix.go +++ /dev/null @@ -1,83 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upTradesIndexFix, downTradesIndexFix) -} - -func upTradesIndexFix(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_symbol ON trades;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_symbol_fee_currency ON trades;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_traded_at_symbol ON trades;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_symbol ON trades (exchange, symbol);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_symbol_fee_currency ON trades (exchange, symbol, fee_currency, traded_at);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_traded_at_symbol ON trades (exchange, traded_at, symbol);") - if err != nil { - return err - } - - return err -} - -func downTradesIndexFix(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is rolled back. - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_symbol ON trades;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_symbol_fee_currency ON trades;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_traded_at_symbol ON trades;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_symbol ON trades (symbol);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_symbol_fee_currency ON trades (symbol, fee_currency, traded_at);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_traded_at_symbol ON trades (traded_at, symbol);") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20201105093056_orders_add_index.go b/pkg/migrations/20201105093056_orders_add_index.go deleted file mode 100644 index 9d1251169..000000000 --- a/pkg/migrations/20201105093056_orders_add_index.go +++ /dev/null @@ -1,43 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upOrdersAddIndex, downOrdersAddIndex) -} - -func upOrdersAddIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "CREATE INDEX orders_symbol ON orders (exchange, symbol);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE UNIQUE INDEX orders_order_id ON orders (order_id, exchange);") - if err != nil { - return err - } - - return err -} - -func downOrdersAddIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is rolled back. - - _, err = tx.ExecContext(ctx, "DROP INDEX orders_symbol ON orders;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP INDEX orders_order_id ON orders;") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20201106114742_klines.go b/pkg/migrations/20201106114742_klines.go deleted file mode 100644 index bbb4ca9ac..000000000 --- a/pkg/migrations/20201106114742_klines.go +++ /dev/null @@ -1,73 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upKlines, downKlines) -} - -func upKlines(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "CREATE TABLE `klines`\n(\n `gid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n `exchange` VARCHAR(10) NOT NULL,\n `start_time` DATETIME(3) NOT NULL,\n `end_time` DATETIME(3) NOT NULL,\n `interval` VARCHAR(3) NOT NULL,\n `symbol` VARCHAR(7) NOT NULL,\n `open` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `high` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `low` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `close` DECIMAL(16, 8) UNSIGNED NOT NULL DEFAULT 0.0,\n `volume` DECIMAL(16, 8) UNSIGNED NOT NULL DEFAULT 0.0,\n `closed` BOOL NOT NULL DEFAULT TRUE,\n `last_trade_id` INT UNSIGNED NOT NULL DEFAULT 0,\n `num_trades` INT UNSIGNED NOT NULL DEFAULT 0,\n PRIMARY KEY (`gid`)\n);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE INDEX `klines_end_time_symbol_interval` ON klines (`end_time`, `symbol`, `interval`);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE TABLE `okex_klines` LIKE `klines`;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE TABLE `binance_klines` LIKE `klines`;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "CREATE TABLE `max_klines` LIKE `klines`;") - if err != nil { - return err - } - - return err -} - -func downKlines(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is rolled back. - - _, err = tx.ExecContext(ctx, "DROP INDEX `klines_end_time_symbol_interval` ON `klines`;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP TABLE `binance_klines`;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP TABLE `okex_klines`;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP TABLE `max_klines`;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "DROP TABLE `klines`;") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20201211175751_fix_symbol_length.go b/pkg/migrations/20201211175751_fix_symbol_length.go deleted file mode 100644 index 6349d3763..000000000 --- a/pkg/migrations/20201211175751_fix_symbol_length.go +++ /dev/null @@ -1,43 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upFixSymbolLength, downFixSymbolLength) -} - -func upFixSymbolLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "ALTER TABLE trades MODIFY COLUMN symbol VARCHAR(9);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "ALTER TABLE orders MODIFY COLUMN symbol VARCHAR(9);") - if err != nil { - return err - } - - return err -} - -func downFixSymbolLength(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 MODIFY COLUMN symbol VARCHAR(8);") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "ALTER TABLE orders MODIFY COLUMN symbol VARCHAR(8);") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20210118163847_fix_unique_index.go b/pkg/migrations/20210118163847_fix_unique_index.go deleted file mode 100644 index 8e31b5964..000000000 --- a/pkg/migrations/20210118163847_fix_unique_index.go +++ /dev/null @@ -1,43 +0,0 @@ -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 UNIQUE INDEX `id` (`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 UNIQUE INDEX `id` (`id`);") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20210119232826_add_margin_columns.go b/pkg/migrations/20210119232826_add_margin_columns.go deleted file mode 100644 index 778876cad..000000000 --- a/pkg/migrations/20210119232826_add_margin_columns.go +++ /dev/null @@ -1,43 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upAddMarginColumns, downAddMarginColumns) -} - -func upAddMarginColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "ALTER TABLE `trades`\n ADD COLUMN `is_margin` BOOLEAN NOT NULL DEFAULT FALSE,\n ADD COLUMN `is_isolated` BOOLEAN NOT NULL DEFAULT FALSE\n ;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "ALTER TABLE `orders`\n ADD COLUMN `is_margin` BOOLEAN NOT NULL DEFAULT FALSE,\n ADD COLUMN `is_isolated` BOOLEAN NOT NULL DEFAULT FALSE\n ;") - if err != nil { - return err - } - - return err -} - -func downAddMarginColumns(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`\n DROP COLUMN `is_margin`,\n DROP COLUMN `is_isolated`;") - if err != nil { - return err - } - - _, err = tx.ExecContext(ctx, "ALTER TABLE `orders`\n DROP COLUMN `is_margin`,\n DROP COLUMN `is_isolated`;") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/20210129182704_trade_price_quantity_index.go b/pkg/migrations/20210129182704_trade_price_quantity_index.go deleted file mode 100644 index 6fa66ea76..000000000 --- a/pkg/migrations/20210129182704_trade_price_quantity_index.go +++ /dev/null @@ -1,33 +0,0 @@ -package migrations - -import ( - "context" - - "github.com/c9s/rockhopper" -) - -func init() { - rockhopper.AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex) -} - -func upTradePriceQuantityIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is applied. - - _, err = tx.ExecContext(ctx, "CREATE INDEX trades_price_quantity ON trades (order_id,price,quantity);") - if err != nil { - return err - } - - return err -} - -func downTradePriceQuantityIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { - // This code is executed when the migration is rolled back. - - _, err = tx.ExecContext(ctx, "DROP INDEX trades_price_quantity ON trades") - if err != nil { - return err - } - - return err -} diff --git a/pkg/migrations/mysql/20200721225616_trades.go b/pkg/migrations/mysql/20200721225616_trades.go index 094b205ea..5db543f65 100644 --- a/pkg/migrations/mysql/20200721225616_trades.go +++ b/pkg/migrations/mysql/20200721225616_trades.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTrades, downTrades) + AddMigration(upTrades, downTrades) + } func upTrades(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20200819054742_trade_index.go b/pkg/migrations/mysql/20200819054742_trade_index.go index e734dd88c..c56a8cacd 100644 --- a/pkg/migrations/mysql/20200819054742_trade_index.go +++ b/pkg/migrations/mysql/20200819054742_trade_index.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTradeIndex, downTradeIndex) + AddMigration(upTradeIndex, downTradeIndex) + } func upTradeIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20201102222546_orders.go b/pkg/migrations/mysql/20201102222546_orders.go index 04aaa0653..f5bf0734a 100644 --- a/pkg/migrations/mysql/20201102222546_orders.go +++ b/pkg/migrations/mysql/20201102222546_orders.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upOrders, downOrders) + AddMigration(upOrders, downOrders) + } func upOrders(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20201103173342_trades_add_order_id.go b/pkg/migrations/mysql/20201103173342_trades_add_order_id.go index 6b6dea539..b0b1e855a 100644 --- a/pkg/migrations/mysql/20201103173342_trades_add_order_id.go +++ b/pkg/migrations/mysql/20201103173342_trades_add_order_id.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTradesAddOrderId, downTradesAddOrderId) + AddMigration(upTradesAddOrderId, downTradesAddOrderId) + } func upTradesAddOrderId(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20201105092857_trades_index_fix.go b/pkg/migrations/mysql/20201105092857_trades_index_fix.go index d5a0917f1..24a7ff96e 100644 --- a/pkg/migrations/mysql/20201105092857_trades_index_fix.go +++ b/pkg/migrations/mysql/20201105092857_trades_index_fix.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTradesIndexFix, downTradesIndexFix) + AddMigration(upTradesIndexFix, downTradesIndexFix) + } func upTradesIndexFix(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20201105093056_orders_add_index.go b/pkg/migrations/mysql/20201105093056_orders_add_index.go index 5e13521c5..65ffd6fcd 100644 --- a/pkg/migrations/mysql/20201105093056_orders_add_index.go +++ b/pkg/migrations/mysql/20201105093056_orders_add_index.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upOrdersAddIndex, downOrdersAddIndex) + AddMigration(upOrdersAddIndex, downOrdersAddIndex) + } func upOrdersAddIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20201106114742_klines.go b/pkg/migrations/mysql/20201106114742_klines.go index 0f5a8e756..007dcc3f9 100644 --- a/pkg/migrations/mysql/20201106114742_klines.go +++ b/pkg/migrations/mysql/20201106114742_klines.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upKlines, downKlines) + AddMigration(upKlines, downKlines) + } func upKlines(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20201211175751_fix_symbol_length.go b/pkg/migrations/mysql/20201211175751_fix_symbol_length.go index 4bc7b15db..97e4e3d3c 100644 --- a/pkg/migrations/mysql/20201211175751_fix_symbol_length.go +++ b/pkg/migrations/mysql/20201211175751_fix_symbol_length.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upFixSymbolLength, downFixSymbolLength) + AddMigration(upFixSymbolLength, downFixSymbolLength) + } func upFixSymbolLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20210118163847_fix_unique_index.go b/pkg/migrations/mysql/20210118163847_fix_unique_index.go index 141fdf44e..006af2141 100644 --- a/pkg/migrations/mysql/20210118163847_fix_unique_index.go +++ b/pkg/migrations/mysql/20210118163847_fix_unique_index.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upFixUniqueIndex, downFixUniqueIndex) + AddMigration(upFixUniqueIndex, downFixUniqueIndex) + } func upFixUniqueIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20210119232826_add_margin_columns.go b/pkg/migrations/mysql/20210119232826_add_margin_columns.go index 48a096f00..01ef31aa0 100644 --- a/pkg/migrations/mysql/20210119232826_add_margin_columns.go +++ b/pkg/migrations/mysql/20210119232826_add_margin_columns.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upAddMarginColumns, downAddMarginColumns) + AddMigration(upAddMarginColumns, downAddMarginColumns) + } func upAddMarginColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20210129182704_trade_price_quantity_index.go b/pkg/migrations/mysql/20210129182704_trade_price_quantity_index.go index 22e5570aa..9b6a5753b 100644 --- a/pkg/migrations/mysql/20210129182704_trade_price_quantity_index.go +++ b/pkg/migrations/mysql/20210129182704_trade_price_quantity_index.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex) + AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex) + } func upTradePriceQuantityIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/mysql/20210215203116_add_pnl_column.go b/pkg/migrations/mysql/20210215203116_add_pnl_column.go new file mode 100644 index 000000000..b36fd3138 --- /dev/null +++ b/pkg/migrations/mysql/20210215203116_add_pnl_column.go @@ -0,0 +1,44 @@ +package mysql + +import ( + "context" + + "github.com/c9s/rockhopper" +) + +func init() { + AddMigration(upAddPnlColumn, downAddPnlColumn) + +} + +func upAddPnlColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` ADD COLUMN `pnl` DECIMAL NULL;") + if err != nil { + return err + } + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` ADD COLUMN `strategy` VARCHAR(32) NULL;") + if err != nil { + return err + } + + return err +} + +func downAddPnlColumn(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 COLUMN `pnl`;") + if err != nil { + return err + } + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` DROP COLUMN `strategy`;") + if err != nil { + return err + } + + return err +} diff --git a/pkg/migrations/mysql/migration_api.go b/pkg/migrations/mysql/migration_api.go new file mode 100644 index 000000000..b4cbebc2b --- /dev/null +++ b/pkg/migrations/mysql/migration_api.go @@ -0,0 +1,50 @@ +package mysql + +import ( + "github.com/c9s/rockhopper" + + "fmt" + "runtime" + "strings" +) + +var registeredGoMigrations map[int64]*rockhopper.Migration + +// AddMigration adds a migration. +func AddMigration(up, down rockhopper.TransactionHandler) { + pc, filename, _, _ := runtime.Caller(1) + + funcName := runtime.FuncForPC(pc).Name() + lastSlash := strings.LastIndexByte(funcName, '/') + if lastSlash < 0 { + lastSlash = 0 + } + lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash + packageName := funcName[:lastDot] + AddNamedMigration(packageName, filename, up, down) +} + +// AddNamedMigration : Add a named migration. +func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) { + if registeredGoMigrations == nil { + registeredGoMigrations = make(map[int64]*rockhopper.Migration) + } + + v, _ := rockhopper.FileNumericComponent(filename) + + migration := &rockhopper.Migration{ + Package: packageName, + Registered: true, + + Version: v, + UpFn: up, + DownFn: down, + Source: filename, + UseTx: true, + } + + if existing, ok := registeredGoMigrations[v]; ok { + panic(fmt.Sprintf("failed to add migration %q: version conflicts with %q", filename, existing.Source)) + } + registeredGoMigrations[v] = migration +} diff --git a/pkg/migrations/sqlite3/20200721225616_trades.go b/pkg/migrations/sqlite3/20200721225616_trades.go index d061db626..dbc677683 100644 --- a/pkg/migrations/sqlite3/20200721225616_trades.go +++ b/pkg/migrations/sqlite3/20200721225616_trades.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTrades, downTrades) + AddMigration(upTrades, downTrades) + } func upTrades(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20200819054742_trade_index.go b/pkg/migrations/sqlite3/20200819054742_trade_index.go index c22577348..50d3a9514 100644 --- a/pkg/migrations/sqlite3/20200819054742_trade_index.go +++ b/pkg/migrations/sqlite3/20200819054742_trade_index.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTradeIndex, downTradeIndex) + AddMigration(upTradeIndex, downTradeIndex) + } func upTradeIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20201102222546_orders.go b/pkg/migrations/sqlite3/20201102222546_orders.go index b360c575d..1930783d5 100644 --- a/pkg/migrations/sqlite3/20201102222546_orders.go +++ b/pkg/migrations/sqlite3/20201102222546_orders.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upOrders, downOrders) + AddMigration(upOrders, downOrders) + } func upOrders(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20201103173342_trades_add_order_id.go b/pkg/migrations/sqlite3/20201103173342_trades_add_order_id.go index 7130b78b6..05608ffb5 100644 --- a/pkg/migrations/sqlite3/20201103173342_trades_add_order_id.go +++ b/pkg/migrations/sqlite3/20201103173342_trades_add_order_id.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTradesAddOrderId, downTradesAddOrderId) + AddMigration(upTradesAddOrderId, downTradesAddOrderId) + } func upTradesAddOrderId(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20201105092857_trades_index_fix.go b/pkg/migrations/sqlite3/20201105092857_trades_index_fix.go index 05112484d..969449027 100644 --- a/pkg/migrations/sqlite3/20201105092857_trades_index_fix.go +++ b/pkg/migrations/sqlite3/20201105092857_trades_index_fix.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTradesIndexFix, downTradesIndexFix) + AddMigration(upTradesIndexFix, downTradesIndexFix) + } func upTradesIndexFix(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20201105093056_orders_add_index.go b/pkg/migrations/sqlite3/20201105093056_orders_add_index.go index d880d2d0a..fcb730bb5 100644 --- a/pkg/migrations/sqlite3/20201105093056_orders_add_index.go +++ b/pkg/migrations/sqlite3/20201105093056_orders_add_index.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upOrdersAddIndex, downOrdersAddIndex) + AddMigration(upOrdersAddIndex, downOrdersAddIndex) + } func upOrdersAddIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20201106114742_klines.go b/pkg/migrations/sqlite3/20201106114742_klines.go index 792cc7183..ab015892d 100644 --- a/pkg/migrations/sqlite3/20201106114742_klines.go +++ b/pkg/migrations/sqlite3/20201106114742_klines.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upKlines, downKlines) + AddMigration(upKlines, downKlines) + } func upKlines(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20201211175751_fix_symbol_length.go b/pkg/migrations/sqlite3/20201211175751_fix_symbol_length.go index 562967063..3b029b2ca 100644 --- a/pkg/migrations/sqlite3/20201211175751_fix_symbol_length.go +++ b/pkg/migrations/sqlite3/20201211175751_fix_symbol_length.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upFixSymbolLength, downFixSymbolLength) + AddMigration(upFixSymbolLength, downFixSymbolLength) + } func upFixSymbolLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20210118163847_fix_unique_index.go b/pkg/migrations/sqlite3/20210118163847_fix_unique_index.go index 87b1a8a91..649e9cd7f 100644 --- a/pkg/migrations/sqlite3/20210118163847_fix_unique_index.go +++ b/pkg/migrations/sqlite3/20210118163847_fix_unique_index.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upFixUniqueIndex, downFixUniqueIndex) + AddMigration(upFixUniqueIndex, downFixUniqueIndex) + } func upFixUniqueIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20210119232826_add_margin_columns.go b/pkg/migrations/sqlite3/20210119232826_add_margin_columns.go index f403e5ad6..a04974d78 100644 --- a/pkg/migrations/sqlite3/20210119232826_add_margin_columns.go +++ b/pkg/migrations/sqlite3/20210119232826_add_margin_columns.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upAddMarginColumns, downAddMarginColumns) + AddMigration(upAddMarginColumns, downAddMarginColumns) + } func upAddMarginColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20210129182704_trade_price_quantity_index.go b/pkg/migrations/sqlite3/20210129182704_trade_price_quantity_index.go index 73c608a61..33c06c155 100644 --- a/pkg/migrations/sqlite3/20210129182704_trade_price_quantity_index.go +++ b/pkg/migrations/sqlite3/20210129182704_trade_price_quantity_index.go @@ -7,7 +7,8 @@ import ( ) func init() { - rockhopper.AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex) + AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex) + } func upTradePriceQuantityIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { diff --git a/pkg/migrations/sqlite3/20210215203111_add_pnl_column.go b/pkg/migrations/sqlite3/20210215203111_add_pnl_column.go new file mode 100644 index 000000000..a752d057e --- /dev/null +++ b/pkg/migrations/sqlite3/20210215203111_add_pnl_column.go @@ -0,0 +1,44 @@ +package sqlite3 + +import ( + "context" + + "github.com/c9s/rockhopper" +) + +func init() { + AddMigration(upAddPnlColumn, downAddPnlColumn) + +} + +func upAddPnlColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` ADD COLUMN `pnl` DECIMAL NULL;") + if err != nil { + return err + } + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` ADD COLUMN `strategy` TEXT;") + if err != nil { + return err + } + + return err +} + +func downAddPnlColumn(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` RENAME COLUMN `pnl` TO `pnl_deleted`;") + if err != nil { + return err + } + + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` RENAME COLUMN `strategy` TO `strategy_deleted`;") + if err != nil { + return err + } + + return err +} diff --git a/pkg/migrations/sqlite3/migration_api.go b/pkg/migrations/sqlite3/migration_api.go new file mode 100644 index 000000000..8b874a63d --- /dev/null +++ b/pkg/migrations/sqlite3/migration_api.go @@ -0,0 +1,50 @@ +package sqlite3 + +import ( + "github.com/c9s/rockhopper" + + "fmt" + "runtime" + "strings" +) + +var registeredGoMigrations map[int64]*rockhopper.Migration + +// AddMigration adds a migration. +func AddMigration(up, down rockhopper.TransactionHandler) { + pc, filename, _, _ := runtime.Caller(1) + + funcName := runtime.FuncForPC(pc).Name() + lastSlash := strings.LastIndexByte(funcName, '/') + if lastSlash < 0 { + lastSlash = 0 + } + lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash + packageName := funcName[:lastDot] + AddNamedMigration(packageName, filename, up, down) +} + +// AddNamedMigration : Add a named migration. +func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) { + if registeredGoMigrations == nil { + registeredGoMigrations = make(map[int64]*rockhopper.Migration) + } + + v, _ := rockhopper.FileNumericComponent(filename) + + migration := &rockhopper.Migration{ + Package: packageName, + Registered: true, + + Version: v, + UpFn: up, + DownFn: down, + Source: filename, + UseTx: true, + } + + if existing, ok := registeredGoMigrations[v]; ok { + panic(fmt.Sprintf("failed to add migration %q: version conflicts with %q", filename, existing.Source)) + } + registeredGoMigrations[v] = migration +}