From c38564dcc73d8043ef0224eb818b753aa3f71ac4 Mon Sep 17 00:00:00 2001 From: TonyQ Date: Sat, 11 Dec 2021 10:40:11 +0800 Subject: [PATCH 1/2] compile and update migration package --- ...211211103657_update_fee_currency_length.go | 34 +++++++++++++++++++ ...211211103657_update_fee_currency_length.go | 34 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 pkg/migrations/mysql/20211211103657_update_fee_currency_length.go create mode 100644 pkg/migrations/sqlite3/20211211103657_update_fee_currency_length.go diff --git a/pkg/migrations/mysql/20211211103657_update_fee_currency_length.go b/pkg/migrations/mysql/20211211103657_update_fee_currency_length.go new file mode 100644 index 000000000..887307586 --- /dev/null +++ b/pkg/migrations/mysql/20211211103657_update_fee_currency_length.go @@ -0,0 +1,34 @@ +package mysql + +import ( + "context" + + "github.com/c9s/rockhopper" +) + +func init() { + AddMigration(upUpdateFeeCurrencyLength, downUpdateFeeCurrencyLength) + +} + +func upUpdateFeeCurrencyLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + + _, err = tx.ExecContext(ctx, "ALTER TABLE trades CHANGE fee_currency fee_currency varchar(10) NOT NULL;") + if err != nil { + return err + } + + return err +} + +func downUpdateFeeCurrencyLength(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 CHANGE fee_currency fee_currency varchar(4) NOT NULL;") + if err != nil { + return err + } + + return err +} diff --git a/pkg/migrations/sqlite3/20211211103657_update_fee_currency_length.go b/pkg/migrations/sqlite3/20211211103657_update_fee_currency_length.go new file mode 100644 index 000000000..ec7373937 --- /dev/null +++ b/pkg/migrations/sqlite3/20211211103657_update_fee_currency_length.go @@ -0,0 +1,34 @@ +package sqlite3 + +import ( + "context" + + "github.com/c9s/rockhopper" +) + +func init() { + AddMigration(upUpdateFeeCurrencyLength, downUpdateFeeCurrencyLength) + +} + +func upUpdateFeeCurrencyLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is applied. + + _, err = tx.ExecContext(ctx, "") + if err != nil { + return err + } + + return err +} + +func downUpdateFeeCurrencyLength(ctx context.Context, tx rockhopper.SQLExecutor) (err error) { + // This code is executed when the migration is rolled back. + + _, err = tx.ExecContext(ctx, "") + if err != nil { + return err + } + + return err +} From 1118453b9637b02333e7dbe497b7511e185276e7 Mon Sep 17 00:00:00 2001 From: TonyQ Date: Sat, 11 Dec 2021 10:40:57 +0800 Subject: [PATCH 2/2] doc: update readme document for migration --- README.md | 8 ++++++++ .../mysql/20211211103657_update_fee_currency_length.sql | 9 +++++++++ .../20211211103657_update_fee_currency_length.sql | 8 ++++++++ 3 files changed, 25 insertions(+) create mode 100644 migrations/mysql/20211211103657_update_fee_currency_length.sql create mode 100644 migrations/sqlite3/20211211103657_update_fee_currency_length.sql diff --git a/README.md b/README.md index be2696f24..e3d7fd9e5 100644 --- a/README.md +++ b/README.md @@ -562,6 +562,14 @@ Then run the following command to compile the migration files into go files: make migrations ``` +or + +```shell + rockhopper compile --config rockhopper_mysql.yaml --output pkg/migrations/mysql + rockhopper compile --config rockhopper_sqlite.yaml --output pkg/migrations/sqlite3 + git add -v pkg/migrations && git commit -m "compile and update migration package" pkg/migrations || true +``` + If you want to override the DSN and the Driver defined in the YAML config file, you can add some env vars in your dotenv file like this: diff --git a/migrations/mysql/20211211103657_update_fee_currency_length.sql b/migrations/mysql/20211211103657_update_fee_currency_length.sql new file mode 100644 index 000000000..89bda824b --- /dev/null +++ b/migrations/mysql/20211211103657_update_fee_currency_length.sql @@ -0,0 +1,9 @@ +-- +up +-- +begin +ALTER TABLE trades CHANGE fee_currency fee_currency varchar(10) NOT NULL; +-- +end + +-- +down +-- +begin +ALTER TABLE trades CHANGE fee_currency fee_currency varchar(4) NOT NULL; +-- +end diff --git a/migrations/sqlite3/20211211103657_update_fee_currency_length.sql b/migrations/sqlite3/20211211103657_update_fee_currency_length.sql new file mode 100644 index 000000000..a88ad32d4 --- /dev/null +++ b/migrations/sqlite3/20211211103657_update_fee_currency_length.sql @@ -0,0 +1,8 @@ +-- +up +-- +begin +-- +end + +-- +down + +-- +begin +-- +end