Skip to content

Commit

Permalink
Fix specs for parser
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 4, 2022
1 parent daeccb6 commit 0cdc201
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
2 changes: 2 additions & 0 deletions krab/type_ddl_create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var schemaCreateIndex = &hcl.BodySchema{
func (d *DDLCreateIndex) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
d.Source.Extract(block)

d.Table = block.Labels[0]
d.Name = block.Labels[1]
d.Unique = false
d.Concurrently = false
d.Columns = []string{}
Expand Down
2 changes: 1 addition & 1 deletion krab/type_ddl_drop_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (d *DDLDropIndex) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {

case "cascade":
expr := krabhcl.Expression{Expr: v.Expr, EvalContext: ctx}
d.Concurrently = expr.AsBool()
d.Cascade = expr.AsBool()

default:
return fmt.Errorf("Unknown attribute `%s` for `%s` block", k, block.Type)
Expand Down
6 changes: 5 additions & 1 deletion krab/type_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ func (m *MigrationUpOrDown) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) er
case "sql":
m.AttrDefRanges["sql"] = v.Range
expr := krabhcl.Expression{Expr: v.Expr, EvalContext: ctx}
m.SQL = expr.AsString()
sql, err := expr.TryString()
if err != nil {
return err
}
m.SQL = sql

default:
return fmt.Errorf("Unknown attribute `%s` for `%s` block", k, block.Type)
Expand Down
13 changes: 13 additions & 0 deletions krabhcl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ func (e Expression) AsFloat64() float64 {
return 0
}

func (e Expression) TryString() (string, error) {
val, diags := e.Expr.Value(e.EvalContext)
if diags.HasErrors() {
return "", diags.Errs()[0]
}
var str string
err := gocty.FromCtyValue(val, &str)
if err != nil {
return "", err
}
return str, nil
}

func (e Expression) AsString() string {
val, _ := e.Expr.Value(e.EvalContext)
var str string
Expand Down
22 changes: 11 additions & 11 deletions spec/action_migrate_dsl_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ migration "create_animals" {
column "extinct" "boolean" {
null = false
default = true
default = "TRUE"
}
column "weight_kg" "int" { null = false }
Expand Down Expand Up @@ -107,8 +107,8 @@ CREATE TABLE "categories"(
c.AssertSQLContains(t, `
CREATE UNLOGGED TABLE "animals"(
"id" bigint GENERATED ALWAYS AS IDENTITY,
"name" varchar NULL,
"extinct" boolean NOT NULL DEFAULT true,
"name" varchar,
"extinct" boolean NOT NULL DEFAULT TRUE,
"weight_kg" int NOT NULL,
"weight_g" int GENERATED ALWAYS AS (weight_kg * 1000) STORED,
"category_id" bigint NOT NULL
Expand Down Expand Up @@ -136,13 +136,13 @@ migration "create_animals" {
up {
create_table "animals" {
column "id" "bigint" { default = 1 }
column "x" "real" { default = 1.2 }
column "y" "double precision" { default = 1.3 }
column "name" "varchar" { default = "hello" }
column "extinct" "boolean" { default = true }
column "map" "jsonb" { default = {} }
column "list" "jsonb" { default = [] }
column "id" "bigint" { default = "1" }
column "x" "real" { default = "1.2" }
column "y" "double precision" { default = "1.3" }
column "name" "varchar" { default = "'hello'" }
column "extinct" "boolean" { default = "TRUE" }
column "map" "jsonb" { default = "'{}'" }
column "list" "jsonb" { default = "'[]'" }
}
}
Expand All @@ -163,7 +163,7 @@ migration_set "animals" {
c.AssertSQLContains(t, "DEFAULT 1.2")
c.AssertSQLContains(t, "DEFAULT 1.3")
c.AssertSQLContains(t, "DEFAULT 'hello'")
c.AssertSQLContains(t, "DEFAULT true")
c.AssertSQLContains(t, "DEFAULT TRUE")
c.AssertSQLContains(t, "DEFAULT '{}'")
c.AssertSQLContains(t, "DEFAULT '[]'")
}
Expand Down
7 changes: 7 additions & 0 deletions spec/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func (m *cliMock) AssertSuccessfulRun(t *testing.T, args []string) bool {
if assert.Equal(t, 0, m.exitCode, "Exit code should be eql to 0") {
return true
} else {
fmt.Println("statements debug:")
for _, sql := range m.connection.recorder {
fmt.Println("---")
fmt.Println(ctc.ForegroundBrightRed, sql, ctc.Reset)
}
fmt.Println("---")
fmt.Println(ctc.ForegroundRed, m.uiErrorWriter.String(), ctc.Reset)
fmt.Println(ctc.ForegroundRed, m.uiErrorWriter.String(), ctc.Reset)
return false
}
Expand Down

0 comments on commit 0cdc201

Please sign in to comment.