better trade endpoints
This commit is contained in:
@@ -19,7 +19,7 @@ func InitDB(db *sql.DB) {
|
||||
CREATE TABLE IF NOT EXISTS trades (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
currency_id INTEGER NOT NULL,
|
||||
currency_code TEXT NOT NULL,
|
||||
shares INTEGER NOT NULL,
|
||||
product INTEGER NOT NULL CHECK(product IN (0, 1, 2, 3)),
|
||||
type INTEGER NOT NULL CHECK(type IN (0, 1)),
|
||||
@@ -118,3 +118,34 @@ func MigrateAddUniqueToRevenueEntries(db *sql.DB) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func MigrateTradeCode(db *sql.DB) error {
|
||||
step :=
|
||||
// 1. copy existing data into a temp table with the new constraint
|
||||
`
|
||||
ALTER TABLE trades RENAME TO trades_old;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS trades (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
currency_code TEXT NOT NULL,
|
||||
shares INTEGER NOT NULL,
|
||||
product INTEGER NOT NULL CHECK(product IN (0, 1, 2, 3)),
|
||||
type INTEGER NOT NULL CHECK(type IN (0, 1)),
|
||||
price REAL NOT NULL,
|
||||
traded_at DATETIME NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO trades (id, company_id, currency_code, shares, product, type, price, traded_at)
|
||||
SELECT id, company_id, '', shares, product, type, price, traded_at
|
||||
FROM trades_old;
|
||||
|
||||
DROP TABLE trades_old;
|
||||
`
|
||||
|
||||
_, err := db.Exec(step)
|
||||
if err != nil {
|
||||
return fmt.Errorf("migration failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user