Skip to content

Commit

Permalink
Some self-review and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
bcspragu committed Jan 16, 2024
1 parent 69f4e13 commit d3780c8
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions cmd/tools/migratesqldb/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ var (
Use: "apply",
Short: "Apply migrations",
RunE: func(cmd *cobra.Command, args []string) error {
migrationsPath, err := runfiles.Rlocation("__main__/db/sqldb/migrations/0001_create_schema_migrations_history.down.sql")
if err != nil {
return fmt.Errorf("failed to get a path to migrations: %w", err)
}
migrationsPath = filepath.Dir(migrationsPath)
migrator, err := newMigrator(sqlDB, migrationsPath)
migrator, err := newMigrator(sqlDB)
if err != nil {
return fmt.Errorf("when creating the migrator: %w", err)
}
Expand All @@ -87,12 +82,7 @@ var (
Use: "rollback",
Short: "Rollback migrations",
RunE: func(cmd *cobra.Command, args []string) error {
migrationsPath, err := runfiles.Rlocation("__main__/db/sqldb/migrations/0001_create_schema_migrations_history.down.sql")
if err != nil {
return fmt.Errorf("failed to get a path to migrations: %w", err)
}
migrationsPath = filepath.Dir(migrationsPath)
migrator, err := newMigrator(sqlDB, migrationsPath)
migrator, err := newMigrator(sqlDB)
if err != nil {
return fmt.Errorf("when creating the migrator: %w", err)
}
Expand All @@ -110,7 +100,15 @@ func init() {
rootCmd.AddCommand(applyCmd, rollbackCmd)
}

func newMigrator(db *sql.DB, migrationsPath string) (*migrate.Migrate, error) {
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)
Expand Down

0 comments on commit d3780c8

Please sign in to comment.