Skip to content

Commit 1a8547c

Browse files
authored
Merge pull request #2107 from c9s/dboy/types-trade-add-uuid
FEATURE: [migrations] Add order uuid column in `orders` and `trades` tables
2 parents d09105f + 3b07ef0 commit 1a8547c

13 files changed

+176
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- +up
2+
-- +begin
3+
ALTER TABLE `orders` ADD COLUMN `uuid` VARBINARY(36) NOT NULL DEFAULT '';
4+
-- +end
5+
6+
-- +down
7+
-- +begin
8+
ALTER TABLE `orders` DROP COLUMN `uuid`;
9+
-- +end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- +up
2+
-- +begin
3+
ALTER TABLE `trades` ADD COLUMN `order_uuid` VARBINARY(16) NOT NULL DEFAULT '';
4+
-- +end
5+
6+
-- +down
7+
-- +begin
8+
ALTER TABLE `trades` DROP COLUMN `order_uuid`;
9+
-- +end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- +up
2+
-- +begin
3+
ALTER TABLE orders ADD COLUMN uuid TEXT NOT NULL DEFAULT '';
4+
-- +end
5+
6+
-- +down
7+
-- +begin
8+
ALTER TABLE orders DROP COLUMN uuid;
9+
-- +end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- +up
2+
-- +begin
3+
ALTER TABLE trades ADD COLUMN order_uuid TEXT NOT NULL DEFAULT '';
4+
-- +end
5+
6+
-- +down
7+
-- +begin
8+
ALTER TABLE trades DROP COLUMN order_uuid;
9+
-- +end

pkg/exchange/coinbase/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func toGlobalTrade(cbTrade *api.Trade) types.Trade {
7373
return types.Trade{
7474
ID: cbTrade.TradeID,
7575
OrderID: FNV64a(cbTrade.OrderID),
76+
OrderUUID: cbTrade.OrderID,
7677
Exchange: types.ExchangeCoinBase,
7778
Price: cbTrade.Price,
7879
Quantity: cbTrade.Size,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package mysql
2+
3+
import (
4+
"context"
5+
6+
"github.com/c9s/rockhopper/v2"
7+
)
8+
9+
func init() {
10+
AddMigration("main", up_main_ordersAddUuid, down_main_ordersAddUuid)
11+
}
12+
13+
func up_main_ordersAddUuid(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
14+
// This code is executed when the migration is applied.
15+
_, err = tx.ExecContext(ctx, "ALTER TABLE `orders` ADD COLUMN `uuid` VARBINARY(36) NOT NULL DEFAULT '';")
16+
if err != nil {
17+
return err
18+
}
19+
return err
20+
}
21+
22+
func down_main_ordersAddUuid(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
23+
// This code is executed when the migration is rolled back.
24+
_, err = tx.ExecContext(ctx, "ALTER TABLE `orders` DROP COLUMN `uuid`;")
25+
if err != nil {
26+
return err
27+
}
28+
return err
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package mysql
2+
3+
import (
4+
"context"
5+
6+
"github.com/c9s/rockhopper/v2"
7+
)
8+
9+
func init() {
10+
AddMigration("main", up_main_tradesAddOrderUuid, down_main_tradesAddOrderUuid)
11+
}
12+
13+
func up_main_tradesAddOrderUuid(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
14+
// This code is executed when the migration is applied.
15+
_, err = tx.ExecContext(ctx, "ALTER TABLE `trades` ADD COLUMN `order_uuid` VARBINARY(16) NOT NULL DEFAULT '';")
16+
if err != nil {
17+
return err
18+
}
19+
return err
20+
}
21+
22+
func down_main_tradesAddOrderUuid(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
23+
// This code is executed when the migration is rolled back.
24+
_, err = tx.ExecContext(ctx, "ALTER TABLE `trades` DROP COLUMN `order_uuid`;")
25+
if err != nil {
26+
return err
27+
}
28+
return err
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package sqlite3
2+
3+
import (
4+
"context"
5+
6+
"github.com/c9s/rockhopper/v2"
7+
)
8+
9+
func init() {
10+
AddMigration("main", up_main_tradesAddOrderUuid, down_main_tradesAddOrderUuid)
11+
}
12+
13+
func up_main_tradesAddOrderUuid(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
14+
// This code is executed when the migration is applied.
15+
_, err = tx.ExecContext(ctx, "ALTER TABLE orders ADD COLUMN uuid TEXT NOT NULL DEFAULT '';")
16+
if err != nil {
17+
return err
18+
}
19+
return err
20+
}
21+
22+
func down_main_tradesAddOrderUuid(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
23+
// This code is executed when the migration is rolled back.
24+
_, err = tx.ExecContext(ctx, "ALTER TABLE orders DROP COLUMN uuid;")
25+
if err != nil {
26+
return err
27+
}
28+
return err
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package sqlite3
2+
3+
import (
4+
"context"
5+
6+
"github.com/c9s/rockhopper/v2"
7+
)
8+
9+
func init() {
10+
AddMigration("main", up_main_ordersAddUuid, down_main_ordersAddUuid)
11+
}
12+
13+
func up_main_ordersAddUuid(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
14+
// This code is executed when the migration is applied.
15+
_, err = tx.ExecContext(ctx, "ALTER TABLE trades ADD COLUMN order_uuid TEXT NOT NULL DEFAULT '';")
16+
if err != nil {
17+
return err
18+
}
19+
return err
20+
}
21+
22+
func down_main_ordersAddUuid(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
23+
// This code is executed when the migration is rolled back.
24+
_, err = tx.ExecContext(ctx, "ALTER TABLE trades DROP COLUMN order_uuid;")
25+
if err != nil {
26+
return err
27+
}
28+
return err
29+
}

pkg/service/order.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@ func (s *OrderService) scanRows(rows *sqlx.Rows) (orders []types.Order, err erro
203203
func (s *OrderService) Insert(order types.Order) (err error) {
204204
if s.DB.DriverName() == "mysql" {
205205
_, err = s.DB.NamedExec(`
206-
INSERT INTO orders (exchange, order_id, client_order_id, order_type, status, symbol, price, stop_price, quantity, executed_quantity, side, is_working, time_in_force, created_at, updated_at, is_margin, is_futures, is_isolated)
207-
VALUES (:exchange, :order_id, :client_order_id, :order_type, :status, :symbol, :price, :stop_price, :quantity, :executed_quantity, :side, :is_working, :time_in_force, :created_at, :updated_at, :is_margin, :is_futures, :is_isolated)
206+
INSERT INTO orders (exchange, order_id, client_order_id, order_type, status, symbol, price, stop_price, quantity, executed_quantity, side, is_working, time_in_force, created_at, updated_at, is_margin, is_futures, is_isolated, uuid)
207+
VALUES (:exchange, :order_id, :client_order_id, :order_type, :status, :symbol, :price, :stop_price, :quantity, :executed_quantity, :side, :is_working, :time_in_force, :created_at, :updated_at, :is_margin, :is_futures, :is_isolated, UUID_TO_BIN(:uuid, true))
208208
ON DUPLICATE KEY UPDATE status=:status, executed_quantity=:executed_quantity, is_working=:is_working, updated_at=:updated_at`, order)
209209
return err
210210
}
211211

212212
_, err = s.DB.NamedExec(`
213213
INSERT INTO orders (exchange, order_id, client_order_id, order_type, status, symbol, price, stop_price, quantity, executed_quantity, side, is_working, time_in_force, created_at, updated_at, is_margin, is_futures, is_isolated)
214-
VALUES (:exchange, :order_id, :client_order_id, :order_type, :status, :symbol, :price, :stop_price, :quantity, :executed_quantity, :side, :is_working, :time_in_force, :created_at, :updated_at, :is_margin, :is_futures, :is_isolated)
214+
VALUES (:exchange, :order_id, :client_order_id, :order_type, :status, :symbol, :price, :stop_price, :quantity, :executed_quantity, :side, :is_working, :time_in_force, :created_at, :updated_at, :is_margin, :is_futures, :is_isolated, :uuid)
215215
`, order)
216216

217217
return err

0 commit comments

Comments
 (0)