Skip to content

Commit

Permalink
Add rollback support to migratedb and update deps (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcspragu authored Jan 16, 2024
1 parent 6ccd470 commit ae9b78e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 210 deletions.
14 changes: 7 additions & 7 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Start of Go + Gazelle + gRPC
http_archive(
name = "io_bazel_rules_go",
sha256 = "278b7ff5a826f3dc10f04feaf0b70d48b68748ccd512d7f98bf442077f043fe3",
sha256 = "de7974538c31f76658e0d333086c69efdf6679dbc6a466ac29e65434bf47076d",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.45.0/rules_go-v0.45.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.45.0/rules_go-v0.45.0.zip",
],
)

http_archive(
name = "bazel_gazelle",
sha256 = "d3fa66a39028e97d76f9e2db8f1b0c11c099e8e01bf363a923074784e451f809",
integrity = "sha256-MpOL2hbmcABjA1R5Bj2dJMYO2o15/Uc5Vj9Q0zHLMgk=",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.33.0/bazel-gazelle-v0.33.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.33.0/bazel-gazelle-v0.33.0.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.35.0/bazel-gazelle-v0.35.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.35.0/bazel-gazelle-v0.35.0.tar.gz",
],
)

Expand All @@ -39,7 +39,7 @@ oapi_dependencies()
go_rules_dependencies()
go_register_toolchains(
nogo = "@//:nogo",
version = "1.20.6"
version = "1.21.6"
)
gazelle_dependencies()

Expand Down
6 changes: 4 additions & 2 deletions cmd/tools/migratesqldb/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ go_library(
importpath = "github.com/RMI/pacta/cmd/tools/migratesqldb/cmd",
visibility = ["//visibility:public"],
deps = [
"@com_github_golang_migrate_migrate_v4//:migrate",
"@com_github_golang_migrate_migrate_v4//database/pgx/v5:pgx",
"@com_github_golang_migrate_migrate_v4//source/file",
"@com_github_jackc_pgx_v5//pgxpool",
"@com_github_silicon_ally_testpgx//migrate",
"@com_github_spf13_cobra//:cobra",
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
"@io_bazel_rules_go//go/runfiles:go_default_library",
],
)
63 changes: 55 additions & 8 deletions cmd/tools/migratesqldb/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/Silicon-Ally/testpgx/migrate"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/bazelbuild/rules_go/go/runfiles"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/spf13/cobra"

_ "github.com/golang-migrate/migrate/v4/source/file"
)

func Execute() {
Expand Down Expand Up @@ -63,16 +67,28 @@ var (
Use: "apply",
Short: "Apply migrations",
RunE: func(cmd *cobra.Command, args []string) error {
migrationsPath, err := bazel.Runfile("db/sqldb/migrations")
migrator, err := newMigrator(sqlDB)
if err != nil {
return fmt.Errorf("failed to get a path to migrations: %w", err)
return fmt.Errorf("when creating the migrator: %w", err)
}
if err := migrator.Up(); err != nil {
return fmt.Errorf("while applying the migration(s): %w", err)
}
migrator, err := migrate.New(migrationsPath)
return nil
},
}

rollbackCmd = &cobra.Command{
Use: "rollback",
Short: "Rollback migrations",
RunE: func(cmd *cobra.Command, args []string) error {
migrator, err := newMigrator(sqlDB)
if err != nil {
return fmt.Errorf("when creating the migrator: %w", err)
}
if err := migrator.Migrate(sqlDB); err != nil {
return fmt.Errorf("while applying the migration(s): %w", err)
if err := migrator.Steps(-1); err != nil {
return fmt.Errorf("while rolling back the migration(s): %w", err)

}
return nil
},
Expand All @@ -81,5 +97,36 @@ var (

func init() {
rootCmd.PersistentFlags().StringVar(&dsn, "dsn", "", "A Postgres DSN, parsable by pgx.ParseConfig")
rootCmd.AddCommand(applyCmd)
rootCmd.AddCommand(applyCmd, rollbackCmd)
}

func newMigrator(db *sql.DB) (*migrate.Migrate, error) {
// For a description of why this is needed, see:
// https://github.com/bazelbuild/rules_go/issues/3830
migrationsPath, err := runfiles.Rlocation("__main__/db/sqldb/migrations/0001_create_schema_migrations_history.down.sql")
if err != nil {
return nil, fmt.Errorf("failed to get a path to migrations: %w", err)
}
migrationsPath = filepath.Dir(migrationsPath)

// Pings the database to distinguish between migration and connection errors
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("failed to ping database: %w", err)
}

driver, err := pgx.WithInstance(db, &pgx.Config{})
if err != nil {
return nil, fmt.Errorf("failed to init driver instance: %w", err)
}

mgr, err := migrate.NewWithDatabaseInstance(
"file://"+migrationsPath,
"pgx",
driver,
)
if err != nil {
return nil, fmt.Errorf("failed to init migrate instance: %w", err)
}

return mgr, nil
}
Loading

0 comments on commit ae9b78e

Please sign in to comment.