-
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
21 changed files
with
442 additions
and
61 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
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 |
---|---|---|
@@ -1,7 +1,22 @@ | ||
package krab | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/ohkrab/krab/krabdb" | ||
) | ||
|
||
// DDLCheck constraint DSL for table DDL. | ||
type DDLCheck struct { | ||
Name string `hcl:"name,label"` | ||
Expression string `hcl:"expression"` | ||
} | ||
|
||
// ToSQL converts migration definition to SQL. | ||
func (d *DDLCheck) ToSQL(w io.StringWriter) { | ||
w.WriteString("CONSTRAINT ") | ||
w.WriteString(krabdb.QuoteIdent(d.Name)) | ||
w.WriteString(" CHECK (") | ||
w.WriteString(d.Expression) | ||
w.WriteString(")") | ||
} |
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 |
---|---|---|
@@ -1,6 +1,18 @@ | ||
package krab | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/ohkrab/krab/krabdb" | ||
) | ||
|
||
// DDLDropTable contains DSL for dropping tables. | ||
type DDLDropTable struct { | ||
Table string `hcl:"table,label"` | ||
Name string `hcl:"name,label"` | ||
} | ||
|
||
// ToSQL converts migration definition to SQL. | ||
func (d *DDLDropTable) ToSQL(w io.StringWriter) { | ||
w.WriteString("DROP TABLE ") | ||
w.WriteString(krabdb.QuoteIdent(d.Name)) | ||
} |
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 |
---|---|---|
@@ -1,8 +1,23 @@ | ||
package krab | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/ohkrab/krab/krabdb" | ||
) | ||
|
||
// DDLForeignKey constraint DSL for table DDL. | ||
type DDLForeignKey struct { | ||
Name string `hcl:"name,label"` | ||
Columns []string `hcl:"columns"` | ||
References DDLReferences `hcl:"references,block"` | ||
} | ||
|
||
// ToSQL converts migration definition to SQL. | ||
func (d *DDLForeignKey) ToSQL(w io.StringWriter) { | ||
w.WriteString("FOREIGN KEY (") | ||
cols := krabdb.QuoteIdentStrings(d.Columns) | ||
w.WriteString(strings.Join(cols, ",")) | ||
w.WriteString(") ") | ||
d.References.ToSQL(w) | ||
} |
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 |
---|---|---|
@@ -1,6 +1,15 @@ | ||
package krab | ||
|
||
import "io" | ||
|
||
// DDLGeneratedColumn DSL. | ||
type DDLGeneratedColumn struct { | ||
As string `hcl:"as"` | ||
} | ||
|
||
// ToSQL converts migration definition to SQL. | ||
func (d *DDLGeneratedColumn) ToSQL(w io.StringWriter) { | ||
w.WriteString("GENERATED ALWAYS AS (") | ||
w.WriteString(d.As) | ||
w.WriteString(") STORED") | ||
} |
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
package krab | ||
|
||
import "io" | ||
|
||
// DDLIdentity DSL. | ||
type DDLIdentity struct { | ||
Generated string `hcl:"generated,label"` | ||
// Generated string `hcl:"generated,optional"` | ||
} | ||
|
||
// ToSQL converts migration definition to SQL. | ||
func (d *DDLIdentity) ToSQL(w io.StringWriter) { | ||
w.WriteString("GENERATED ALWAYS AS IDENTITY") | ||
} |
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 |
---|---|---|
@@ -1,8 +1,29 @@ | ||
package krab | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/ohkrab/krab/krabdb" | ||
) | ||
|
||
// DDLPrimaryKey constraint DSL for table DDL. | ||
type DDLPrimaryKey struct { | ||
Name string `hcl:"name,label"` | ||
Columns []string `hcl:"columns"` | ||
Include []string `hcl:"include,optional"` | ||
} | ||
|
||
// ToSQL converts migration definition to SQL. | ||
func (d *DDLPrimaryKey) ToSQL(w io.StringWriter) { | ||
w.WriteString("PRIMARY KEY (") | ||
cols := krabdb.QuoteIdentStrings(d.Columns) | ||
w.WriteString(strings.Join(cols, ",")) | ||
w.WriteString(")") | ||
|
||
if len(d.Include) > 0 { | ||
w.WriteString(" INCLUDE (") | ||
include := krabdb.QuoteIdentStrings(d.Include) | ||
w.WriteString(strings.Join(include, ",")) | ||
w.WriteString(")") | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,36 @@ | ||
package krab | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/ohkrab/krab/krabdb" | ||
) | ||
|
||
// 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"` | ||
} | ||
|
||
// ToSQL converts migration definition to SQL. | ||
func (d *DDLReferences) ToSQL(w io.StringWriter) { | ||
w.WriteString("REFERENCES ") | ||
w.WriteString(krabdb.QuoteIdent(d.Table)) | ||
w.WriteString("(") | ||
cols := krabdb.QuoteIdentStrings(d.Columns) | ||
w.WriteString(strings.Join(cols, ",")) | ||
w.WriteString(")") | ||
|
||
if d.OnDelete != "" { | ||
w.WriteString(" ON DELETE ") | ||
w.WriteString(d.OnDelete) | ||
} | ||
|
||
if d.OnUpdate != "" { | ||
w.WriteString(" ON UPDATE ") | ||
w.WriteString(d.OnUpdate) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,29 @@ | ||
package krab | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/ohkrab/krab/krabdb" | ||
) | ||
|
||
// DDLUnique constraint DSL for table DDL. | ||
type DDLUnique struct { | ||
Name string `hcl:"name,label"` | ||
Columns []string `hcl:"columns"` | ||
Include []string `hcl:"include,optional"` | ||
} | ||
|
||
// ToSQL converts migration definition to SQL. | ||
func (d *DDLUnique) ToSQL(w io.StringWriter) { | ||
w.WriteString("UNIQUE (") | ||
cols := krabdb.QuoteIdentStrings(d.Columns) | ||
w.WriteString(strings.Join(cols, ",")) | ||
w.WriteString(")") | ||
|
||
if len(d.Include) > 0 { | ||
w.WriteString(" INCLUDE (") | ||
include := krabdb.QuoteIdentStrings(d.Include) | ||
w.WriteString(strings.Join(include, ",")) | ||
w.WriteString(")") | ||
} | ||
} |
Oops, something went wrong.