Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MINOR: compile and update migration package for trades.inserted_at #1657

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pkg/migrations/mysql/main_20240531163411_trades_created.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions pkg/migrations/sqlite3/main_20240531163411_trades_created.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading