trade: add inserted_at column

A trade may be missed initially and fetched after it has occurred.
This commit is contained in:
Yu-Cheng 2024-05-31 16:52:12 +08:00
parent 5098c3ac35
commit 49d567c8f2
3 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,14 @@
-- +up
-- +begin
ALTER TABLE `trades` ADD COLUMN `inserted_at` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL AFTER `traded_at`;
-- +end
-- +begin
UPDATE `trades` SET `inserted_at`=`traded_at`;
-- +end
-- +down
-- +begin
ALTER TABLE `trades` DROP COLUMN `inserted_at`;
-- +end

View File

@ -0,0 +1,23 @@
-- +up
-- +begin
ALTER TABLE trades ADD COLUMN inserted_at TEXT;
UPDATE trades SET inserted_at = traded_at;
CREATE TRIGGER set_inserted_at
AFTER INSERT ON trades
FOR EACH ROW
BEGIN
UPDATE trades
SET inserted_at = datetime('now')
WHERE rowid = NEW.rowid;
END;
-- +end
-- +down
-- +begin
DROP TRIGGER set_inserted_at;
ALTER TABLE trades DROP COLUMN inserted_at;
-- +end

View File

@ -95,6 +95,8 @@ type Trade struct {
// PnL is the profit and loss value of the executed trade // PnL is the profit and loss value of the executed trade
PnL sql.NullFloat64 `json:"pnl" db:"pnl"` PnL sql.NullFloat64 `json:"pnl" db:"pnl"`
InsertedAt Time `db:"inserted_at"`
} }
func (trade Trade) CsvHeader() []string { func (trade Trade) CsvHeader() []string {