Merge pull request #1657 from c9s/c9s/trades-created-mgr

MINOR: compile and update migration package for trades.inserted_at
This commit is contained in:
c9s 2024-06-18 18:22:27 +08:00 committed by GitHub
commit 6b6bf2f722
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 0 deletions

View File

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

View File

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