From 007b67976829a53500bd97567eee167d542bfdb5 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 5 Feb 2021 14:15:28 +0800 Subject: [PATCH] add sqlite3 migrations --- migrations/20200721225616_trades.sql | 3 +- migrations/sqlite3/20200721225616_trades.sql | 20 +++++++++ .../sqlite3/20200819054742_trade_index.sql | 9 ++++ migrations/sqlite3/20201102222546_orders.sql | 25 +++++++++++ .../20201103173342_trades_add_order_id.sql | 5 +++ .../20201105092857_trades_index_fix.sql | 19 ++++++++ .../20201105093056_orders_add_index.sql | 7 +++ migrations/sqlite3/20201106114742_klines.sql | 44 +++++++++++++++++++ .../20201211175751_fix_symbol_length.sql | 5 +++ .../20210118163847_fix_unique_index.sql | 9 ++++ .../20210119232826_add_margin_columns.sql | 33 ++++++++++++++ ...10129182704_trade_price_quantity_index.sql | 10 +++++ 12 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 migrations/sqlite3/20200721225616_trades.sql create mode 100644 migrations/sqlite3/20200819054742_trade_index.sql create mode 100644 migrations/sqlite3/20201102222546_orders.sql create mode 100644 migrations/sqlite3/20201103173342_trades_add_order_id.sql create mode 100644 migrations/sqlite3/20201105092857_trades_index_fix.sql create mode 100644 migrations/sqlite3/20201105093056_orders_add_index.sql create mode 100644 migrations/sqlite3/20201106114742_klines.sql create mode 100644 migrations/sqlite3/20201211175751_fix_symbol_length.sql create mode 100644 migrations/sqlite3/20210118163847_fix_unique_index.sql create mode 100644 migrations/sqlite3/20210119232826_add_margin_columns.sql create mode 100644 migrations/sqlite3/20210129182704_trade_price_quantity_index.sql diff --git a/migrations/20200721225616_trades.sql b/migrations/20200721225616_trades.sql index 12d4e4c77..27760ac23 100644 --- a/migrations/20200721225616_trades.sql +++ b/migrations/20200721225616_trades.sql @@ -19,5 +19,6 @@ CREATE TABLE `trades` PRIMARY KEY (`gid`), UNIQUE KEY `id` (`id`) ); + -- +down -DROP TABLE `trades`; +DROP TABLE IF EXISTS `trades`; diff --git a/migrations/sqlite3/20200721225616_trades.sql b/migrations/sqlite3/20200721225616_trades.sql new file mode 100644 index 000000000..fcd5f8f50 --- /dev/null +++ b/migrations/sqlite3/20200721225616_trades.sql @@ -0,0 +1,20 @@ +-- +up +CREATE TABLE `trades` +( + `gid` INTEGER PRIMARY KEY AUTOINCREMENT, + `id` INTEGER, + `exchange` TEXT NOT NULL DEFAULT '', + `symbol` TEXT NOT NULL, + `price` DECIMAL(16, 8) NOT NULL, + `quantity` DECIMAL(16, 8) NOT NULL, + `quote_quantity` DECIMAL(16, 8) NOT NULL, + `fee` DECIMAL(16, 8) NOT NULL, + `fee_currency` VARCHAR(4) NOT NULL, + `is_buyer` BOOLEAN NOT NULL DEFAULT FALSE, + `is_maker` BOOLEAN NOT NULL DEFAULT FALSE, + `side` VARCHAR(4) NOT NULL DEFAULT '', + `traded_at` DATETIME(3) NOT NULL +); + +-- +down +DROP TABLE IF EXISTS `trades`; diff --git a/migrations/sqlite3/20200819054742_trade_index.sql b/migrations/sqlite3/20200819054742_trade_index.sql new file mode 100644 index 000000000..6ce90b1bd --- /dev/null +++ b/migrations/sqlite3/20200819054742_trade_index.sql @@ -0,0 +1,9 @@ +-- +up +CREATE INDEX trades_symbol ON trades(symbol); +CREATE INDEX trades_symbol_fee_currency ON trades(symbol, fee_currency, traded_at); +CREATE INDEX trades_traded_at_symbol ON trades(traded_at, symbol); + +-- +down +DROP INDEX trades_symbol ON trades; +DROP INDEX trades_symbol_fee_currency ON trades; +DROP INDEX trades_traded_at_symbol ON trades; diff --git a/migrations/sqlite3/20201102222546_orders.sql b/migrations/sqlite3/20201102222546_orders.sql new file mode 100644 index 000000000..561afd06a --- /dev/null +++ b/migrations/sqlite3/20201102222546_orders.sql @@ -0,0 +1,25 @@ +-- +up +CREATE TABLE `orders` +( + `gid` INTEGER PRIMARY KEY AUTOINCREMENT, + + `exchange` VARCHAR NOT NULL DEFAULT '', + -- order_id is the order id returned from the exchange + `order_id` INTEGER NOT NULL, + `client_order_id` VARCHAR NOT NULL DEFAULT '', + `order_type` VARCHAR NOT NULL, + `symbol` VARCHAR NOT NULL, + `status` VARCHAR NOT NULL, + `time_in_force` VARCHAR NOT NULL, + `price` DECIMAL(16, 8) NOT NULL, + `stop_price` DECIMAL(16, 8) NOT NULL, + `quantity` DECIMAL(16, 8) NOT NULL, + `executed_quantity` DECIMAL(16, 8) NOT NULL DEFAULT 0.0, + `side` VARCHAR NOT NULL DEFAULT '', + `is_working` BOOLEAN NOT NULL DEFAULT FALSE, + `created_at` DATETIME(3) NOT NULL, + `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +-- +down +DROP TABLE IF EXISTS `orders`; diff --git a/migrations/sqlite3/20201103173342_trades_add_order_id.sql b/migrations/sqlite3/20201103173342_trades_add_order_id.sql new file mode 100644 index 000000000..7ff284200 --- /dev/null +++ b/migrations/sqlite3/20201103173342_trades_add_order_id.sql @@ -0,0 +1,5 @@ +-- +up +ALTER TABLE `trades` ADD COLUMN `order_id` INTEGER NOT NULL; + +-- +down +ALTER TABLE `trades` RENAME COLUMN `order_id` TO `order_id_deleted`; diff --git a/migrations/sqlite3/20201105092857_trades_index_fix.sql b/migrations/sqlite3/20201105092857_trades_index_fix.sql new file mode 100644 index 000000000..44f58e768 --- /dev/null +++ b/migrations/sqlite3/20201105092857_trades_index_fix.sql @@ -0,0 +1,19 @@ +-- +up +DROP INDEX IF EXISTS trades_symbol; +DROP INDEX IF EXISTS trades_symbol_fee_currency; +DROP INDEX IF EXISTS trades_traded_at_symbol; + +CREATE INDEX trades_symbol ON trades (exchange, symbol); +CREATE INDEX trades_symbol_fee_currency ON trades (exchange, symbol, fee_currency, traded_at); +CREATE INDEX trades_traded_at_symbol ON trades (exchange, traded_at, symbol); + +-- +down +DROP INDEX IF EXISTS trades_symbol ON trades; +DROP INDEX IF EXISTS trades_symbol_fee_currency ON trades; +DROP INDEX IF EXISTS trades_traded_at_symbol ON trades; + +CREATE INDEX trades_symbol ON trades (symbol); +CREATE INDEX trades_symbol_fee_currency ON trades (symbol, fee_currency, traded_at); +CREATE INDEX trades_traded_at_symbol ON trades (traded_at, symbol); + + diff --git a/migrations/sqlite3/20201105093056_orders_add_index.sql b/migrations/sqlite3/20201105093056_orders_add_index.sql new file mode 100644 index 000000000..99834a551 --- /dev/null +++ b/migrations/sqlite3/20201105093056_orders_add_index.sql @@ -0,0 +1,7 @@ +-- +up +CREATE INDEX orders_symbol ON orders (exchange, symbol); +CREATE UNIQUE INDEX orders_order_id ON orders (order_id, exchange); + +-- +down +DROP INDEX IF EXISTS orders_symbol; +DROP INDEX IF EXISTS orders_order_id; diff --git a/migrations/sqlite3/20201106114742_klines.sql b/migrations/sqlite3/20201106114742_klines.sql new file mode 100644 index 000000000..72c679206 --- /dev/null +++ b/migrations/sqlite3/20201106114742_klines.sql @@ -0,0 +1,44 @@ +-- +up +-- +begin +CREATE TABLE `klines` +( + `gid` INTEGER PRIMARY KEY AUTOINCREMENT, + `exchange` VARCHAR(10) NOT NULL, + `start_time` DATETIME(3) NOT NULL, + `end_time` DATETIME(3) NOT NULL, + `interval` VARCHAR(3) NOT NULL, + `symbol` VARCHAR(7) NOT NULL, + `open` DECIMAL(16, 8) NOT NULL, + `high` DECIMAL(16, 8) NOT NULL, + `low` DECIMAL(16, 8) NOT NULL, + `close` DECIMAL(16, 8) NOT NULL DEFAULT 0.0, + `volume` DECIMAL(16, 8) NOT NULL DEFAULT 0.0, + `closed` BOOLEAN NOT NULL DEFAULT TRUE, + `last_trade_id` INT NOT NULL DEFAULT 0, + `num_trades` INT NOT NULL DEFAULT 0 +); +-- +end + +-- +begin +CREATE INDEX `klines_end_time_symbol_interval` ON klines (`end_time`, `symbol`, `interval`); +-- +end + +-- +begin +CREATE TABLE `okex_klines` AS SELECT * FROM `klines` WHERE 0 +-- +end + +-- +begin +CREATE TABLE `binance_klines` AS SELECT * FROM `klines` WHERE 0 +-- +end + +-- +begin +CREATE TABLE `max_klines` AS SELECT * FROM `klines` WHERE 0 +-- +end + +-- +down +DROP INDEX IF EXISTS `klines_end_time_symbol_interval`; +DROP TABLE IF EXISTS `binance_klines`; +DROP TABLE IF EXISTS `okex_klines`; +DROP TABLE IF EXISTS `max_klines`; +DROP TABLE IF EXISTS `klines`; + diff --git a/migrations/sqlite3/20201211175751_fix_symbol_length.sql b/migrations/sqlite3/20201211175751_fix_symbol_length.sql new file mode 100644 index 000000000..06569c667 --- /dev/null +++ b/migrations/sqlite3/20201211175751_fix_symbol_length.sql @@ -0,0 +1,5 @@ +-- +up +SELECT 1; + +-- +down +SELECT 1; diff --git a/migrations/sqlite3/20210118163847_fix_unique_index.sql b/migrations/sqlite3/20210118163847_fix_unique_index.sql new file mode 100644 index 000000000..60ada793e --- /dev/null +++ b/migrations/sqlite3/20210118163847_fix_unique_index.sql @@ -0,0 +1,9 @@ +-- +up +-- +begin +CREATE UNIQUE INDEX `trade_unique_id` ON `trades` (`exchange`,`symbol`, `side`, `id`); +-- +end + +-- +down +-- +begin +DROP INDEX IF EXISTS `trade_unique_id`; +-- +end diff --git a/migrations/sqlite3/20210119232826_add_margin_columns.sql b/migrations/sqlite3/20210119232826_add_margin_columns.sql new file mode 100644 index 000000000..eaff7b613 --- /dev/null +++ b/migrations/sqlite3/20210119232826_add_margin_columns.sql @@ -0,0 +1,33 @@ +-- +up +-- +begin +ALTER TABLE `trades` ADD COLUMN `is_margin` BOOLEAN NOT NULL DEFAULT FALSE; +-- +end +-- +begin +ALTER TABLE `trades` ADD COLUMN `is_isolated` BOOLEAN NOT NULL DEFAULT FALSE; +-- +end + +-- +begin +ALTER TABLE `orders` ADD COLUMN `is_margin` BOOLEAN NOT NULL DEFAULT FALSE; +-- +end + +-- +begin +ALTER TABLE `orders` ADD COLUMN `is_isolated` BOOLEAN NOT NULL DEFAULT FALSE; +-- +end + +-- +down + +-- +begin +ALTER TABLE `trades` RENAME COLUMN `is_margin` TO `is_margin_deleted`; +-- +end + +-- +begin +ALTER TABLE `trades` RENAME COLUMN `is_isolated` TO `is_isolated_deleted`; +-- +end + +-- +begin +ALTER TABLE `orders` RENAME COLUMN `is_margin` TO `is_margin_deleted`; +-- +end + +-- +begin +ALTER TABLE `orders` RENAME COLUMN `is_isolated` TO `is_isolated_deleted`; +-- +end diff --git a/migrations/sqlite3/20210129182704_trade_price_quantity_index.sql b/migrations/sqlite3/20210129182704_trade_price_quantity_index.sql new file mode 100644 index 000000000..196f7467d --- /dev/null +++ b/migrations/sqlite3/20210129182704_trade_price_quantity_index.sql @@ -0,0 +1,10 @@ +-- +up +-- +begin +CREATE INDEX trades_price_quantity ON trades (order_id,price,quantity); +-- +end + +-- +down + +-- +begin +DROP INDEX trades_price_quantity; +-- +end