Skip to content

Commit

Permalink
Basic integration tests for migrate up
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 24, 2021
1 parent 0079139 commit 0fed02a
Show file tree
Hide file tree
Showing 18 changed files with 384 additions and 208 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install:
cp bin/krab /usr/local/bin

test:
DATABASE_URL="postgres://krab:secret@localhost:5432/krab?sslmode=disable&prefer_simple_protocol=true" go test -v ./...
DATABASE_URL="postgres://krab:secret@localhost:5432/krab?sslmode=disable&prefer_simple_protocol=true" go test -v ./... && echo -e "\e[32mok\e[0m"

docker_test:
docker run --rm -e DATABASE_URL="postgres://krab:secret@localhost:5432/krab?sslmode=disable" \
Expand Down
34 changes: 0 additions & 34 deletions cli/app.go

This file was deleted.

8 changes: 6 additions & 2 deletions cli/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type UI interface {
mcli.Ui
}

func DefaultUI() UI {
func New(errorWriter io.Writer, writer io.Writer) UI {
ui := &mcli.ColoredUi{
Ui: &mcli.BasicUi{ErrorWriter: os.Stderr, Writer: os.Stdout},
Ui: &mcli.BasicUi{ErrorWriter: errorWriter, Writer: writer},
WarnColor: mcli.UiColorYellow,
ErrorColor: mcli.UiColorRed,
InfoColor: mcli.UiColorGreen,
Expand All @@ -23,6 +23,10 @@ func DefaultUI() UI {
return ui
}

func DefaultUI() UI {
return New(os.Stderr, os.Stdout)
}

func NullUI() UI {
ui := &mcli.BasicUi{ErrorWriter: io.Discard, Writer: io.Discard}

Expand Down
1 change: 1 addition & 0 deletions krab/action_migrate_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

// ActionMigrateDown keeps data needed to perform this action.
type ActionMigrateDown struct {
Ui cli.UI
Set *MigrationSet
DownMigration SchemaMigration
Arguments Arguments
Expand Down
2 changes: 1 addition & 1 deletion krab/action_migrate_down_arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestActionMigrateDownArguments(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
ctx := context.Background()

set := createMigrationSet("tenants",
set := CreateMigrationSet("tenants",
"v1",
`CREATE TABLE animals(name VARCHAR)`,
`DROP TABLE animals`,
Expand Down
2 changes: 1 addition & 1 deletion krab/action_migrate_down_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestActionMigrateDownHooks(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
ctx := context.Background()

set := createMigrationSet("tenants",
set := CreateMigrationSet("tenants",
"v1",
`CREATE TABLE animals(name VARCHAR)`,
`DROP TABLE animals`,
Expand Down
3 changes: 2 additions & 1 deletion krab/action_migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

// ActionMigrateUp keeps data needed to perform this action.
type ActionMigrateUp struct {
Ui cli.UI
Set *MigrationSet
}

Expand All @@ -38,7 +39,7 @@ func (a *ActionMigrateUp) Synopsis() string {

// Run in CLI.
func (a *ActionMigrateUp) Run(args []string) int {
ui := cli.DefaultUI()
ui := a.Ui
flags := cliargs.New(args)
flags.RequireNonFlagArgs(0)

Expand Down
2 changes: 1 addition & 1 deletion krab/action_migrate_up_arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestActionMigrateUpArguments(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
ctx := context.Background()

set := createMigrationSet("tenants",
set := CreateMigrationSet("tenants",
"v1",
`CREATE TABLE animals(name VARCHAR)`,
`DROP TABLE animals`,
Expand Down
2 changes: 1 addition & 1 deletion krab/action_migrate_up_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestActionMigrateUpHooks(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
ctx := context.Background()

set := createMigrationSet("tenants",
set := CreateMigrationSet("tenants",
"v1",
`CREATE TABLE animals(name VARCHAR)`,
`DROP TABLE animals`,
Expand Down
75 changes: 0 additions & 75 deletions krab/action_migrate_up_test.go

This file was deleted.

61 changes: 0 additions & 61 deletions krab/action_migrate_up_transaction_test.go

This file was deleted.

9 changes: 5 additions & 4 deletions krab/action_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
)

// ActionVersion prints full version.
type ActionVersion struct{}
type ActionVersion struct {
Ui cli.UI
}

func (a *ActionVersion) Help() string {
return `Usage: krab version
Expand All @@ -22,8 +24,7 @@ func (a *ActionVersion) Synopsis() string {

// Run in CLI.
func (a *ActionVersion) Run(args []string) int {
ui := cli.DefaultUI()
ui.Output(fmt.Sprint(InfoName, " ", InfoVersion))
ui.Output(fmt.Sprint("Build ", InfoCommit, " ", InfoBuildDate))
a.Ui.Output(fmt.Sprint(InfoName, " ", InfoVersion))
a.Ui.Output(fmt.Sprint("Build ", InfoCommit, " ", InfoBuildDate))
return 0
}
2 changes: 1 addition & 1 deletion krab/factories_test.go → krab/fixtures_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package krab

func createMigrationSet(
func CreateMigrationSet(
refName string,
migrationData ...string,
) *MigrationSet {
Expand Down
68 changes: 68 additions & 0 deletions krabcli/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package krabcli

import (
"fmt"

mcli "github.com/mitchellh/cli"
"github.com/ohkrab/krab/cli"
"github.com/ohkrab/krab/krab"
)

type Command mcli.Command

type App struct {
Ui cli.UI
CLI *mcli.CLI
Config *krab.Config
}

func New(ui cli.UI, args []string, config *krab.Config) *App {
c := mcli.NewCLI(krab.InfoName, krab.InfoVersion)
c.Args = args
c.Commands = make(map[string]mcli.CommandFactory, 0)

app := &App{
Ui: ui,
CLI: c,
Config: config,
}
app.RegisterAll()

return app
}

func (a *App) RegisterAll() {
a.RegisterCmd("version", func() Command {
return &krab.ActionVersion{Ui: a.Ui}
})

for _, set := range a.Config.MigrationSets {
localSet := set

a.RegisterCmd(fmt.Sprintln("migrate", "up", set.RefName), func() Command {
return &krab.ActionMigrateUp{Ui: a.Ui, Set: localSet}
})

a.RegisterCmd(fmt.Sprintln("migrate", "down", set.RefName), func() Command {
return &krab.ActionMigrateDown{Ui: a.Ui, Set: localSet, Arguments: krab.Arguments{
Args: []*krab.Argument{
{
Name: "version",
Type: "string",
Description: "Migration version to rollback",
},
},
}}
})
}
}

func (a *App) Run() (int, error) {
return a.CLI.Run()
}

func (a *App) RegisterCmd(names string, cmd func() Command) {
a.CLI.Commands[names] = func() (mcli.Command, error) {
return cmd(), nil
}
}
Loading

0 comments on commit 0fed02a

Please sign in to comment.