compile and update migration package

This commit is contained in:
c9s 2021-02-17 17:28:05 +08:00
parent a1cb3859c3
commit ea27a291db
37 changed files with 232 additions and 535 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTrades, downTrades) AddMigration(upTrades, downTrades)
} }
func upTrades(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTrades(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTradeIndex, downTradeIndex) AddMigration(upTradeIndex, downTradeIndex)
} }
func upTradeIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTradeIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upOrders, downOrders) AddMigration(upOrders, downOrders)
} }
func upOrders(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upOrders(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTradesAddOrderId, downTradesAddOrderId) AddMigration(upTradesAddOrderId, downTradesAddOrderId)
} }
func upTradesAddOrderId(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTradesAddOrderId(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTradesIndexFix, downTradesIndexFix) AddMigration(upTradesIndexFix, downTradesIndexFix)
} }
func upTradesIndexFix(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTradesIndexFix(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upOrdersAddIndex, downOrdersAddIndex) AddMigration(upOrdersAddIndex, downOrdersAddIndex)
} }
func upOrdersAddIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upOrdersAddIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upKlines, downKlines) AddMigration(upKlines, downKlines)
} }
func upKlines(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upKlines(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upFixSymbolLength, downFixSymbolLength) AddMigration(upFixSymbolLength, downFixSymbolLength)
} }
func upFixSymbolLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upFixSymbolLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upFixUniqueIndex, downFixUniqueIndex) AddMigration(upFixUniqueIndex, downFixUniqueIndex)
} }
func upFixUniqueIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upFixUniqueIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upAddMarginColumns, downAddMarginColumns) AddMigration(upAddMarginColumns, downAddMarginColumns)
} }
func upAddMarginColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upAddMarginColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex) AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex)
} }
func upTradePriceQuantityIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTradePriceQuantityIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

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

View File

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

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTrades, downTrades) AddMigration(upTrades, downTrades)
} }
func upTrades(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTrades(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTradeIndex, downTradeIndex) AddMigration(upTradeIndex, downTradeIndex)
} }
func upTradeIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTradeIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upOrders, downOrders) AddMigration(upOrders, downOrders)
} }
func upOrders(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upOrders(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTradesAddOrderId, downTradesAddOrderId) AddMigration(upTradesAddOrderId, downTradesAddOrderId)
} }
func upTradesAddOrderId(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTradesAddOrderId(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTradesIndexFix, downTradesIndexFix) AddMigration(upTradesIndexFix, downTradesIndexFix)
} }
func upTradesIndexFix(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTradesIndexFix(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upOrdersAddIndex, downOrdersAddIndex) AddMigration(upOrdersAddIndex, downOrdersAddIndex)
} }
func upOrdersAddIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upOrdersAddIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upKlines, downKlines) AddMigration(upKlines, downKlines)
} }
func upKlines(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upKlines(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upFixSymbolLength, downFixSymbolLength) AddMigration(upFixSymbolLength, downFixSymbolLength)
} }
func upFixSymbolLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upFixSymbolLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upFixUniqueIndex, downFixUniqueIndex) AddMigration(upFixUniqueIndex, downFixUniqueIndex)
} }
func upFixUniqueIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upFixUniqueIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upAddMarginColumns, downAddMarginColumns) AddMigration(upAddMarginColumns, downAddMarginColumns)
} }
func upAddMarginColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upAddMarginColumns(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

@ -7,7 +7,8 @@ import (
) )
func init() { func init() {
rockhopper.AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex) AddMigration(upTradePriceQuantityIndex, downTradePriceQuantityIndex)
} }
func upTradePriceQuantityIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { func upTradePriceQuantityIndex(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {

View File

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

View File

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