From f0d500bbaa2d96505be2043cc8ee89c84140bf9d Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 7 Mar 2022 13:44:43 +0800 Subject: [PATCH] add positions table migration --- .../mysql/20220307132917_add_positions.sql | 26 ++++++++++++++ .../sqlite3/20220307132917_add_positions.sql | 23 +++++++++++++ .../mysql/20220307132917_add_positions.go | 34 +++++++++++++++++++ .../sqlite3/20220307132917_add_positions.go | 34 +++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 migrations/mysql/20220307132917_add_positions.sql create mode 100644 migrations/sqlite3/20220307132917_add_positions.sql create mode 100644 pkg/migrations/mysql/20220307132917_add_positions.go create mode 100644 pkg/migrations/sqlite3/20220307132917_add_positions.go diff --git a/migrations/mysql/20220307132917_add_positions.sql b/migrations/mysql/20220307132917_add_positions.sql new file mode 100644 index 000000000..2431cef31 --- /dev/null +++ b/migrations/mysql/20220307132917_add_positions.sql @@ -0,0 +1,26 @@ +-- +up +CREATE TABLE `positions` +( + `gid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + + `strategy` VARCHAR(32) NOT NULL, + `strategy_instance_id` VARCHAR(64) NOT NULL, + + `symbol` VARCHAR(20) NOT NULL, + `quote_currency` VARCHAR(10) NOT NULL, + `base_currency` VARCHAR(10) NOT NULL, + + -- average_cost is the position average cost + `average_cost` DECIMAL(16, 8) UNSIGNED NOT NULL, + `base` DECIMAL(16, 8) NOT NULL, + `quote` DECIMAL(16, 8) NOT NULL, + + `trade_id` BIGINT UNSIGNED NOT NULL, + `traded_at` DATETIME(3) NOT NULL, + + PRIMARY KEY (`gid`), + UNIQUE KEY `trade_id` (`trade_id`) +); + +-- +down +DROP TABLE IF EXISTS `positions`; diff --git a/migrations/sqlite3/20220307132917_add_positions.sql b/migrations/sqlite3/20220307132917_add_positions.sql new file mode 100644 index 000000000..21a6521a9 --- /dev/null +++ b/migrations/sqlite3/20220307132917_add_positions.sql @@ -0,0 +1,23 @@ +-- +up +CREATE TABLE `positions` +( + `gid` INTEGER PRIMARY KEY AUTOINCREMENT, + + `strategy` VARCHAR(32) NOT NULL, + `strategy_instance_id` VARCHAR(64) NOT NULL, + + `symbol` VARCHAR(20) NOT NULL, + `quote_currency` VARCHAR(10) NOT NULL, + `base_currency` VARCHAR(10) NOT NULL, + + -- average_cost is the position average cost + `average_cost` DECIMAL(16, 8) NOT NULL, + `base` DECIMAL(16, 8) NOT NULL, + `quote` DECIMAL(16, 8) NOT NULL, + + `trade_id` BIGINT NOT NULL, + `traded_at` DATETIME(3) NOT NULL +); + +-- +down +DROP TABLE IF EXISTS `positions`; diff --git a/pkg/migrations/mysql/20220307132917_add_positions.go b/pkg/migrations/mysql/20220307132917_add_positions.go new file mode 100644 index 000000000..2a9c81423 --- /dev/null +++ b/pkg/migrations/mysql/20220307132917_add_positions.go @@ -0,0 +1,34 @@ +package mysql + +import ( + "context" + + "github.com/c9s/rockhopper" +) + +func init() { + AddMigration(upAddPositions, downAddPositions) + +} + +func upAddPositions(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + + _, err = tx.ExecContext(ctx, "CREATE TABLE `positions`\n(\n `gid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n `strategy` VARCHAR(32) NOT NULL,\n `strategy_instance_id` VARCHAR(64) NOT NULL,\n `symbol` VARCHAR(20) NOT NULL,\n `quote_currency` VARCHAR(10) NOT NULL,\n `base_currency` VARCHAR(10) NOT NULL,\n -- average_cost is the position average cost\n `average_cost` DECIMAL(16, 8) UNSIGNED NOT NULL,\n `base` DECIMAL(16, 8) NOT NULL,\n `quote` DECIMAL(16, 8) NOT NULL,\n `trade_id` BIGINT UNSIGNED NOT NULL,\n `traded_at` DATETIME(3) NOT NULL,\n PRIMARY KEY (`gid`),\n UNIQUE KEY `trade_id` (`trade_id`)\n);") + if err != nil { + return err + } + + return err +} + +func downAddPositions(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is rolled back. + + _, err = tx.ExecContext(ctx, "DROP TABLE IF EXISTS `positions`;") + if err != nil { + return err + } + + return err +} diff --git a/pkg/migrations/sqlite3/20220307132917_add_positions.go b/pkg/migrations/sqlite3/20220307132917_add_positions.go new file mode 100644 index 000000000..fb50c30c2 --- /dev/null +++ b/pkg/migrations/sqlite3/20220307132917_add_positions.go @@ -0,0 +1,34 @@ +package sqlite3 + +import ( + "context" + + "github.com/c9s/rockhopper" +) + +func init() { + AddMigration(upAddPositions, downAddPositions) + +} + +func upAddPositions(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + + _, err = tx.ExecContext(ctx, "CREATE TABLE `positions`\n(\n `gid` INTEGER PRIMARY KEY AUTOINCREMENT,\n `strategy` VARCHAR(32) NOT NULL,\n `strategy_instance_id` VARCHAR(64) NOT NULL,\n `symbol` VARCHAR(20) NOT NULL,\n `quote_currency` VARCHAR(10) NOT NULL,\n `base_currency` VARCHAR(10) NOT NULL,\n -- average_cost is the position average cost\n `average_cost` DECIMAL(16, 8) NOT NULL,\n `base` DECIMAL(16, 8) NOT NULL,\n `quote` DECIMAL(16, 8) NOT NULL,\n `trade_id` BIGINT NOT NULL,\n `traded_at` DATETIME(3) NOT NULL\n);") + if err != nil { + return err + } + + return err +} + +func downAddPositions(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is rolled back. + + _, err = tx.ExecContext(ctx, "DROP TABLE IF EXISTS `positions`;") + if err != nil { + return err + } + + return err +}