-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package krab | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ohkrab/krab/cli" | ||
"github.com/ohkrab/krab/krabdb" | ||
"github.com/ohkrab/krab/tpls" | ||
) | ||
|
||
// ActionCustom keeps data needed to perform this action. | ||
type ActionCustom struct { | ||
Ui cli.UI | ||
Action *Action | ||
Arguments Arguments | ||
Connection krabdb.Connection | ||
} | ||
|
||
func (a *ActionCustom) Help() string { | ||
return fmt.Sprint( | ||
`Usage: krab action namespace name`, | ||
"\n\n", | ||
a.Arguments.Help(), | ||
` | ||
Performs custom action. | ||
`, | ||
) | ||
} | ||
|
||
func (a *ActionCustom) Synopsis() string { | ||
return fmt.Sprintf("Action") | ||
} | ||
|
||
// Run in CLI. | ||
func (a *ActionCustom) Run(args []string) int { | ||
return 0 | ||
} | ||
|
||
// Do performs the action. | ||
func (a *ActionCustom) Do(ctx context.Context, db krabdb.DB, tpl *tpls.Templates) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package krab | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
// Action represents custom action to execute. | ||
// | ||
type Action struct { | ||
Namespace string `hcl:"namespace,label"` | ||
RefName string `hcl:"ref_name,label"` | ||
|
||
SQL string `hcl:"sql"` | ||
} | ||
|
||
func (a *Action) Addr() Addr { | ||
return Addr{Keyword: "action", Labels: []string{a.Namespace, a.RefName}} | ||
} | ||
|
||
func (a *Action) Validate() error { | ||
return ErrorCoalesce( | ||
ValidateRefName(a.Namespace), | ||
ValidateRefName(a.RefName), | ||
) | ||
} | ||
|
||
func (m *Action) ToSQL(w io.StringWriter) { | ||
w.WriteString(m.SQL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package spec | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestActionCustom(t *testing.T) { | ||
c := mockCli(mockConfig(` | ||
migration "create_animals" { | ||
version = "v1" | ||
up { sql = "CREATE TABLE animals(name VARCHAR)" } | ||
down { sql = "DROP TABLE animals" } | ||
} | ||
migration "create_animals_view" { | ||
version = "v2" | ||
up { sql = "CREATE MATERIALIZED VIEW anims AS SELECT name FROM animals" } | ||
down { sql = "DROP VIEW anims" } | ||
} | ||
migration "seed_animals" { | ||
version = "v3" | ||
up { sql = "INSERT INTO animals(name) VALUES('Elephant'),('Turtle'),('Cat')" } | ||
down { sql = "TRUNACTE animals" } | ||
} | ||
migration_set "animals" { | ||
migrations = [ | ||
migration.create_animals, | ||
migration.create_animals_view, | ||
migration.seed_animals, | ||
] | ||
} | ||
action "view" "refresh" { | ||
sql = "REFRESH VIEW anims" | ||
} | ||
`)) | ||
defer c.Teardown() | ||
|
||
c.AssertSuccessfulRun(t, []string{"migrate", "up", "animals"}) | ||
c.AssertSchemaMigrationTable(t, "public", "v1", "v2", "v3") | ||
|
||
_, vals := c.Query(t, "SELECT * FROM anims") | ||
if assert.Len(t, vals, 0, "No values should be returned") { | ||
c.AssertSuccessfulRun(t, []string{"action", "view", "refresh"}) | ||
_, vals := c.Query(t, "SELECT * FROM anims") | ||
assert.Len(t, vals, 3, "There should be 3 animals after refresh") | ||
} | ||
|
||
} |