cobble/cobble.go

53 lines
1 KiB
Go

package main
import (
"github.com/spf13/cobra"
"log"
"os"
"strconv"
)
var cli = &cobra.Command{
Use: "cobble",
Short: "Cobble is a simple SQL migration utility.",
}
var up = &cobra.Command{
Use: "up",
Short: "Applies migrations",
Run: func(cmd *cobra.Command, args []string) {
dbPort, _ := strconv.Atoi(os.Getenv("DB_PORT"))
dbConnection, err := ConnectToDatabase(DatabaseConfiguration{
User: os.Getenv("DB_APP_USER"),
Password: os.Getenv("DB_APP_PASSWORD"),
DatabaseName: os.Getenv("DB_NAME"),
Host: os.Getenv("DB_HOST"),
Port: dbPort,
})
if err != nil {
log.Printf("Failed to connect to the database: %s", err)
os.Exit(1)
}
migrationRoot := args[0]
migrationHistory, err := getMigrations(migrationRoot)
for _, migration := range migrationHistory {
log.Printf("%s", migration.Name)
if err = migrate(dbConnection, migration.Path); err != nil {
os.Exit(1)
}
}
},
}
func main() {
cli.AddCommand(up)
if err := cli.Execute(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}