Skip to content

Commit 80b982a

Browse files
committed
cmd/relui: add migrate-down-up flag
Adds a migrate-down-up flag that re-runs the down and up steps of the last migration. This is useful when developing migrations, making it easier to make changes. For golang/go#47401 Change-Id: I21369bc6792220a02641109edf0f628f9dc18981 Reviewed-on: https://go-review.googlesource.com/c/build/+/353409 Trust: Alexander Rakoczy <[email protected]> Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 79bf688 commit 80b982a

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

cmd/relui/Makefile

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ VERSION := $(shell ../coordinator/version.sh)
66
DOCKER_TAG := golang/relui:$(VERSION)
77

88
INTERNAL_PATH := ../../internal/relui
9-
MIGRATION_FILES := $(wildcard $(INTERNAL_PATH)/migrations/*.sql)
10-
GO_FILES := $(shell find $(INTERNAL_PATH) -name "*.go")
119

1210
POSTGRES_DATA_DEV := "postgres-data-dev:/var/lib/postgresql/data"
1311
POSTGRES_RUN_DEV := "postgres-run-dev:/var/run/postgresql"
@@ -31,9 +29,13 @@ postgres-dev: $(DEV_CFG)/pgpass
3129
-e POSTGRES_PASSWORD_FILE=/run/secrets/pgpass \
3230
postgres:13
3331

34-
schema.sql: $(MIGRATION_FILES) $(GO_FILES)
35-
docker build -f Dockerfile -t golang/relui:$(VERSION) ../..
36-
docker run --rm --name=relui-dev-migrate -v $(POSTGRES_RUN_DEV) -e PGUSER=$(POSTGRES_USER) -e PGDATABASE=relui-dev $(DOCKER_TAG) --only-migrate
32+
migrate: docker
33+
docker run --rm --name=relui-dev-migrate -v $(POSTGRES_RUN_DEV) -e PGUSER=$(POSTGRES_USER) -e PGDATABASE=relui-dev $(DOCKER_TAG) --migrate-only
34+
35+
migrate-down-up: docker
36+
docker run --rm --name=relui-dev-migrate -v $(POSTGRES_RUN_DEV) -e PGUSER=$(POSTGRES_USER) -e PGDATABASE=relui-dev $(DOCKER_TAG) --migrate-down-up
37+
38+
schema.sql: migrate
3739
docker exec postgres-dev pg_dump --username=$(POSTGRES_USER) --schema-only relui-dev > schema.sql
3840

3941
.PHONY: test
@@ -47,11 +49,3 @@ docker:
4749
.PHONY: docker-test
4850
docker-test:
4951
docker build -f Dockerfile.test -t golang/relui-test:$(VERSION) ../..
50-
51-
$(DEV_CFG):
52-
mkdir -p $(DEV_CFG)
53-
54-
$(DEV_CFG)/pgpass: $(DEV_CFG)
55-
touch $(DEV_CFG)/pgpass
56-
chmod 0600 $(DEV_CFG)/pgpass
57-
openssl rand -hex -out $(DEV_CFG)/pgpass 32

cmd/relui/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ go run -tags pgx github.com/golang-migrate/migrate/v4/cmd/migrate \
5555
Migrations are automatically ran on application launch. Updating
5656
schema.sql, as documented below, will also run migrations. "Down"
5757
migrations are not automatically run and must be manually invoked in
58-
`psql`.
58+
`psql`, or by the `--migrate-down-up` flag or `make migrate-down-up`.
5959

6060
#### cmd/relui/schema.sql
6161

cmd/relui/main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import (
1717

1818
var (
1919
pgConnect = flag.String("pg-connect", "", "Postgres connection string or URI. If empty, libpq connection defaults are used.")
20-
onlyMigrate = flag.Bool("only-migrate", false, "Exit after running migrations. Migrations are run by default.")
20+
migrateOnly = flag.Bool("migrate-only", false, "Exit after running migrations. Migrations are run by default.")
21+
downUp = flag.Bool("migrate-down-up", false, "Run all Up migration steps, then the last down migration step, followed by the final up migration. Exits after completion.")
2122
)
2223

2324
func main() {
@@ -26,7 +27,13 @@ func main() {
2627
if err := relui.InitDB(ctx, *pgConnect); err != nil {
2728
log.Fatalf("relui.InitDB() = %v", err)
2829
}
29-
if *onlyMigrate {
30+
if *migrateOnly {
31+
return
32+
}
33+
if *downUp {
34+
if err := relui.MigrateDB(*pgConnect, true); err != nil {
35+
log.Fatalf("relui.MigrateDB() = %v", err)
36+
}
3037
return
3138
}
3239
db, err := pgxpool.Connect(ctx, *pgConnect)

internal/relui/store.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ func InitDB(ctx context.Context, conn string) error {
3333
if err := CreateDBIfNotExists(ctx, cfg); err != nil {
3434
return err
3535
}
36-
if err := MigrateDB(conn); err != nil {
36+
if err := MigrateDB(conn, false); err != nil {
3737
return err
3838
}
3939
return nil
4040
}
4141

4242
// MigrateDB applies all migrations to the database specified in conn.
4343
//
44-
// Any key/value or URI string compatible with libpq is valid.
45-
func MigrateDB(conn string) error {
44+
// Any key/value or URI string compatible with libpq is a valid conn.
45+
// If downUp is true, all migrations will be run, then the down and up
46+
// migrations of the final migration are run.
47+
func MigrateDB(conn string, downUp bool) error {
4648
cfg, err := pgx.ParseConfig(conn)
4749
if err != nil {
4850
return fmt.Errorf("pgx.ParseConfig() = %w", err)
@@ -70,6 +72,14 @@ func MigrateDB(conn string) error {
7072
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
7173
return fmt.Errorf("m.Up() = %w", err)
7274
}
75+
if downUp {
76+
if err := m.Steps(-1); err != nil {
77+
return fmt.Errorf("m.Steps(%d) = %w", -1, err)
78+
}
79+
if err := m.Up(); err != nil {
80+
return fmt.Errorf("m.Up() = %w", err)
81+
}
82+
}
7383
db.Close()
7484
return nil
7585
}

0 commit comments

Comments
 (0)