/
migrate_down.go
50 lines (41 loc) · 1.1 KB
/
migrate_down.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cmd
import (
"fmt"
"os"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"github.com/rs/zerolog"
migrate "github.com/rubenv/sql-migrate"
"github.com/spf13/cobra"
)
// migrateDownCmd represents the down command
var migrateDownCmd = &cobra.Command{
Use: "down",
Short: "delete migration from PG",
Run: func(cmd *cobra.Command, args []string) {
log := zerolog.New(os.Stdout).With().Caller().Logger().With().Str("command", "migrate down").Logger()
connStr := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&pool_max_conns=%d",
Conf.PG.User,
Conf.PG.Password,
Conf.PG.Host,
Conf.PG.Port,
Conf.PG.DatabaseName,
Conf.PG.SSL,
Conf.PG.MaxPoolSize,
)
config, err := pgx.ParseConfig(connStr)
if err != nil {
log.Fatal().Err(err).Msg("parse postgres connection string")
}
db := stdlib.OpenDB(*config)
var n int
n, err = migrate.Exec(db, "postgres", Migrations, migrate.Down)
if err != nil {
log.Fatal().Err(err).Msg("delete Migrations")
}
log.Info().Msgf("deleted %d Migrations", n)
},
}
func init() {
migrateCmd.AddCommand(migrateDownCmd)
}