Skip to content

Commit

Permalink
Parsable DSL for creating and dropping tables
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 27, 2021
1 parent cffae70 commit f94d0c5
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 52 deletions.
7 changes: 7 additions & 0 deletions krab/type_ddl_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package krab

// DDLCheck constraint DSL for table DDL.
type DDLCheck struct {
Name string `hcl:"name,label"`
Expression string `hcl:"expression"`
}
13 changes: 13 additions & 0 deletions krab/type_ddl_column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package krab

import "github.com/hashicorp/hcl/v2"

// DDLColumn constraint DSL for table DDL.
type DDLColumn struct {
Name string `hcl:"name,label"`
Type string `hcl:"type,label"`
Null *bool `hcl:"null,optional"`
Identity *DDLIdentity `hcl:"identity,block"`
Default hcl.Expression `hcl:"default,optional"`
Generated *DDLGeneratedColumn `hcl:"generated,block"`
}
12 changes: 12 additions & 0 deletions krab/type_ddl_create_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package krab

// DDLCreateTable contains DSL for creating tables.
type DDLCreateTable struct {
Name string `hcl:"name,label"`
Unlogged bool `hcl:"unlogged,optional"`
Columns []*DDLColumn `hcl:"column,block"`
PrimaryKeys []*DDLPrimaryKey `hcl:"primary_key,block"`
ForeignKeys []*DDLForeignKey `hcl:"foreign_key,block"`
Uniques []*DDLUnique `hcl:"unique,block"`
Checks []*DDLCheck `hcl:"check,block"`
}
6 changes: 6 additions & 0 deletions krab/type_ddl_drop_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package krab

// DDLDropTable contains DSL for dropping tables.
type DDLDropTable struct {
Table string `hcl:"table,label"`
}
8 changes: 8 additions & 0 deletions krab/type_ddl_foreign_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package krab

// DDLForeignKey constraint DSL for table DDL.
type DDLForeignKey struct {
Name string `hcl:"name,label"`
Columns []string `hcl:"columns"`
References DDLReferences `hcl:"references,block"`
}
6 changes: 6 additions & 0 deletions krab/type_ddl_generated_column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package krab

// DDLGeneratedColumn DSL.
type DDLGeneratedColumn struct {
As string `hcl:"as"`
}
6 changes: 6 additions & 0 deletions krab/type_ddl_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package krab

// DDLIdentity DSL.
type DDLIdentity struct {
Generated string `hcl:"generated,label"`
}
8 changes: 8 additions & 0 deletions krab/type_ddl_primary_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package krab

// DDLPrimaryKey constraint DSL for table DDL.
type DDLPrimaryKey struct {
Name string `hcl:"name,label"`
Columns []string `hcl:"columns"`
Include []string `hcl:"include,optional"`
}
9 changes: 9 additions & 0 deletions krab/type_ddl_references.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package krab

// DDLReferences DSL for ForeignKey.
type DDLReferences struct {
Table string `hcl:"table,label"`
Columns []string `hcl:"columns"`
OnDelete string `hcl:"on_delete,optional"`
OnUpdate string `hcl:"on_update,optional"`
}
8 changes: 8 additions & 0 deletions krab/type_ddl_unique.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package krab

// DDLUnique constraint DSL for table DDL.
type DDLUnique struct {
Name string `hcl:"name,label"`
Columns []string `hcl:"columns"`
Include []string `hcl:"include,optional"`
}
62 changes: 12 additions & 50 deletions krab/type_migration.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,25 @@
package krab

import "fmt"
import (
"fmt"
)

// Migration represents single up/down migration pair.
//
type Migration struct {
RefName string `hcl:"ref_name,label"`

Version string `hcl:"version"`
Up MigrationUp `hcl:"up,block"`
Down MigrationDown `hcl:"down,block"`
Transaction *bool `hcl:"transaction,optional"` // wrap operaiton in transaction
Version string `hcl:"version"`
Up MigrationUpOrDown `hcl:"up,block"`
Down MigrationUpOrDown `hcl:"down,block"`
Transaction *bool `hcl:"transaction,optional"` // wrap operaiton in transaction
}

// MigrationUp contains info how to migrate up.
type MigrationUp struct {
SQL string `hcl:"sql,optional"`
}

// MigrationDown contains info how to migrate down.
type MigrationDown struct {
SQL string `hcl:"sql,optional"`
}

// CreateTable contains DSL for creating tables.
type CreateTable struct {
Name string `hcl:"name,label"`
Unlogged bool
}

// PrimaryKey constraint DSL for table DDL.
type PrimaryKey struct {
Name string `hcl:"name,label"`
}

// ForeignKey constraint DSL for table DDL.
type ForeignKey struct {
Name string `hcl:"name,label"`
}

// ForeignKey constraint DSL for table DDL.
type Unique struct {
Name string `hcl:"name,label"`
}

// Column constraint DSL for table DDL.
type Column struct {
Name string `hcl:"name,label"`
Type string `hcl:"type,label"`
}

// Check constraint DSL for table DDL.
type Check struct {
Name string `hcl:"name,label"`
}

// DropTable contains DSL for dropping tables.
type DropTable struct {
Table string `hcl:"table,label"`
// Migration contains info how to migrate up or down.
type MigrationUpOrDown struct {
SQL string `hcl:"sql,optional"`
CreateTables []*DDLCreateTable `hcl:"create_table,block"`
DropTables []*DDLDropTable `hcl:"drop_table,block"`
}

func (ms *Migration) Validate() error {
Expand Down
4 changes: 2 additions & 2 deletions spec/action_migrate_up_dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/jmoiron/sqlx"
)

func XTestActionMigrateUpDsl(t *testing.T) {
func TestActionMigrateUpDsl(t *testing.T) {
withPg(t, func(db *sqlx.DB) {
c := mockCli(mockConfig(`
migration "create_categories" {
Expand Down Expand Up @@ -101,7 +101,7 @@ create_animals v2
Done
`,
)
c.AssertSchemaMigrationTable(t, db, "public", "v1")
c.AssertSchemaMigrationTable(t, db, "public", "v1", "v2")
})
}

Expand Down

0 comments on commit f94d0c5

Please sign in to comment.