add sqlite3 migrations

This commit is contained in:
c9s 2021-02-05 14:15:28 +08:00
parent 8cb77075d0
commit 007b679768
12 changed files with 188 additions and 1 deletions

View File

@ -19,5 +19,6 @@ CREATE TABLE `trades`
PRIMARY KEY (`gid`), PRIMARY KEY (`gid`),
UNIQUE KEY `id` (`id`) UNIQUE KEY `id` (`id`)
); );
-- +down -- +down
DROP TABLE `trades`; DROP TABLE IF EXISTS `trades`;

View File

@ -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`;

View File

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

View File

@ -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`;

View File

@ -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`;

View File

@ -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);

View File

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

View File

@ -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`;

View File

@ -0,0 +1,5 @@
-- +up
SELECT 1;
-- +down
SELECT 1;

View File

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

View File

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

View File

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