Skip to content

Commit

Permalink
Add test suite migrate action
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Jan 11, 2022
1 parent f128534 commit e18e4dd
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 42 deletions.
41 changes: 27 additions & 14 deletions krab/cmd_test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,34 @@ func (c *CmdTestRun) Do(ctx context.Context, o CmdOpts) (interface{}, error) {
var result ResponseTestRun

for _, do := range c.Suite.Before.Dos {
addr, err := krabhcl.ExpressionToAddr(do.MigrationSet)
if err != nil {
return nil, fmt.Errorf("Failed to parse MigrationSet reference: %w", err)
}
for _, migrate := range do.Migrate {
addr, err := krabhcl.Expression{Expr: migrate.SetExpr}.Addr()
if err != nil {
return nil, fmt.Errorf("Failed to parse MigrationSet reference: %w", err)
}

for _, cmd := range c.Registry.Commands {
if addr.Equal(cmd.Addr()) {
if cmd.Name()[1] == "up" {
result, err := cmd.Do(ctx, CmdOpts{Inputs: do.Inputs()})
if err != nil {
return nil, fmt.Errorf("Failed to execute before hook: %w", err)
}
resp := result.([]ResponseMigrateUp)
for _, migration := range resp {
fmt.Println(ctc.ForegroundYellow, migration.Success, migration.Version, migration.Name, ctc.Reset)
for _, cmd := range c.Registry.Commands {
if addr.Equal(cmd.Addr()) {
if cmd.Name()[1] == migrate.Type {
inputs := InputsFromCtyInputs(do.CtyInputs)
migrateInputs := InputsFromCtyInputs(migrate.CtyInputs)
inputs.Merge(migrateInputs)
result, err := cmd.Do(ctx, CmdOpts{Inputs: inputs})
if err != nil {
return nil, fmt.Errorf("Failed to execute before hook: %w", err)
}
respUp, ok := result.([]ResponseMigrateUp)
if ok {
for _, migration := range respUp {
fmt.Println(ctc.ForegroundYellow, "UP ", migration.Success, migration.Version, migration.Name, ctc.Reset)
}
}
respDown, ok := result.([]ResponseMigrateDown)
if ok {
for _, migration := range respDown {
fmt.Println(ctc.ForegroundYellow, "DOWN", migration.Success, migration.Version, migration.Name, ctc.Reset)
}
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions krab/inputs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package krab

import "github.com/zclconf/go-cty/cty"

// Inputs are params passed to command.
type Inputs map[string]interface{}

func InputsFromCtyInputs(vals map[string]cty.Value) Inputs {
inputs := Inputs{}
for k, v := range vals {
str := v.AsString()
inputs[k] = str
}

return inputs
}

func (i Inputs) Merge(other Inputs) {
for k, v := range other {
i[k] = v
}
}
37 changes: 28 additions & 9 deletions krab/subtype_do.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
package krab

import (
"fmt"

"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
)

// Do subtype for other types.
type Do struct {
MigrationSet hcl.Expression `hcl:"migration_set,optional"`
CtyInputs map[string]cty.Value `hcl:"inputs,optional"`
SQL string `hcl:"sql,optional"`
Migrate []*DoMigrate `hcl:"migrate,block"`
CtyInputs map[string]cty.Value `hcl:"inputs,optional"`
SQL string `hcl:"sql,optional"`
}

func (d *Do) Validate() error {
for _, m := range d.Migrate {
if err := m.Validate(); err != nil {
return err
}
}

return nil
}

type DoMigrate struct {
Type string `hcl:"type,label"`
SetExpr hcl.Expression `hcl:"migration_set"`
CtyInputs map[string]cty.Value `hcl:"inputs,optional"`

Set *MigrationSet
}

func (d *Do) Inputs() Inputs {
inputs := Inputs{}
for k, v := range d.CtyInputs {
str := v.AsString()
inputs[k] = str
func (d *DoMigrate) Validate() error {
switch d.Type {
case "up", "down":
return nil
}

return inputs
return fmt.Errorf("Invalid type `%s` for `do` command", d.Type)
}
10 changes: 10 additions & 0 deletions krab/type_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ func (t *TestSuite) Addr() krabhcl.Addr {
func (t *TestSuite) Validate() error {
return ErrorCoalesce(
ValidateRefName(t.RefName),
t.Before.Validate(),
)
}

type TestSuiteBefore struct {
Dos []*Do `hcl:"do,block"`
}

func (t *TestSuiteBefore) Validate() error {
for _, do := range t.Dos {
if err := do.Validate(); err != nil {
return err
}
}
return nil
}
14 changes: 0 additions & 14 deletions krabhcl/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,3 @@ func ParseTraversalToAddr(t hcl.Traversal) (Addr, error) {

return addr, nil
}

func ExpressionToAddr(expr hcl.Expression) (Addr, error) {
traversals := expr.Variables()
if len(traversals) != 1 {
return Addr{}, fmt.Errorf("Failed to extract single addr from HCL expression")
}

t := traversals[0]
parsedAddr, err := ParseTraversalToAddr(t)
if err != nil {
return Addr{}, err
}
return parsedAddr, nil
}
16 changes: 16 additions & 0 deletions krabhcl/expression.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package krabhcl

import (
"fmt"

"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
Expand All @@ -11,6 +13,20 @@ type Expression struct {
EvalContext *hcl.EvalContext
}

func (e Expression) Addr() (Addr, error) {
traversals := e.Expr.Variables()
if len(traversals) != 1 {
return Addr{}, fmt.Errorf("Failed to extract single addr from HCL expression")
}

t := traversals[0]
parsedAddr, err := ParseTraversalToAddr(t)
if err != nil {
return Addr{}, err
}
return parsedAddr, nil
}

func (e Expression) AsBool() bool {
val, _ := e.Expr.Value(e.EvalContext)
var boolean bool
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/tests/versions.krab.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ migration "create_version_type" {

up {
sql = <<SQL
CREATE TYPE version AS (
CREATE TYPE sem_version AS (
major SMALLINT,
minor SMALLINT,
patch SMALLINT
)
SQL
}

down { sql = "DROP TYPE version" }
down { sql = "DROP TYPE sem_version " }
}

migration "create_version_function" {
version = "v2"

up {
sql = <<SQL
CREATE FUNCTION version_inc(_ver version, _type varchar = 'major') RETURNS version
CREATE FUNCTION version_inc(_ver sem_version, _type varchar = 'major') RETURNS sem_version
AS
$$
DECLARE
Expand All @@ -44,7 +44,7 @@ migration "create_version_function" {
}

down {
sql = "DROP FUNCTION version_inc(version, varchar)"
sql = "DROP FUNCTION version_inc(sem_version, varchar)"
}
}

Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/tests/versions_test.krab.hcl
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
test_suite "versions" {
before {
do {
migration_set = migration_set.versions
inputs = {
schema = "aaa"
}

migrate "up" { migration_set = migration_set.versions }
migrate "down" {
migration_set = migration_set.versions
inputs = { version = "v2" }
}
migrate "up" { migration_set = migration_set.versions }
}
}
}
Expand Down

0 comments on commit e18e4dd

Please sign in to comment.