From 46bd4a0ef8b8b4674d936bb57fd5ba13ad70ff15 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 18 Jun 2024 18:10:36 +0800 Subject: [PATCH] compile and update migration package --- .../main_20240531163411_trades_created.go | 33 +++++++++++++++++++ .../main_20240531163411_trades_created.go | 29 ++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 pkg/migrations/mysql/main_20240531163411_trades_created.go create mode 100644 pkg/migrations/sqlite3/main_20240531163411_trades_created.go diff --git a/pkg/migrations/mysql/main_20240531163411_trades_created.go b/pkg/migrations/mysql/main_20240531163411_trades_created.go new file mode 100644 index 000000000..121f56024 --- /dev/null +++ b/pkg/migrations/mysql/main_20240531163411_trades_created.go @@ -0,0 +1,33 @@ +package mysql + +import ( + "context" + + "github.com/c9s/rockhopper/v2" +) + +func init() { + AddMigration("main", up_main_tradesCreated, down_main_tradesCreated) +} + +func up_main_tradesCreated(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` ADD COLUMN `inserted_at` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL AFTER `traded_at`;") + if err != nil { + return err + } + _, err = tx.ExecContext(ctx, "UPDATE `trades` SET `inserted_at`=`traded_at`;") + if err != nil { + return err + } + return err +} + +func down_main_tradesCreated(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is rolled back. + _, err = tx.ExecContext(ctx, "ALTER TABLE `trades` DROP COLUMN `inserted_at`;") + if err != nil { + return err + } + return err +} diff --git a/pkg/migrations/sqlite3/main_20240531163411_trades_created.go b/pkg/migrations/sqlite3/main_20240531163411_trades_created.go new file mode 100644 index 000000000..00599df28 --- /dev/null +++ b/pkg/migrations/sqlite3/main_20240531163411_trades_created.go @@ -0,0 +1,29 @@ +package sqlite3 + +import ( + "context" + + "github.com/c9s/rockhopper/v2" +) + +func init() { + AddMigration("main", up_main_tradesCreated, down_main_tradesCreated) +} + +func up_main_tradesCreated(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + _, err = tx.ExecContext(ctx, "ALTER TABLE trades ADD COLUMN inserted_at TEXT;\nUPDATE trades SET inserted_at = traded_at;\nCREATE TRIGGER set_inserted_at\nAFTER INSERT ON trades\nFOR EACH ROW\nBEGIN\n UPDATE trades\n SET inserted_at = datetime('now')\n WHERE rowid = NEW.rowid;\nEND;") + if err != nil { + return err + } + return err +} + +func down_main_tradesCreated(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is rolled back. + _, err = tx.ExecContext(ctx, "DROP TRIGGER set_inserted_at;\nALTER TABLE trades DROP COLUMN inserted_at;") + if err != nil { + return err + } + return err +}