Skip to content

Commit

Permalink
Support null: false option
Browse files Browse the repository at this point in the history
  • Loading branch information
YasukeXXX committed Nov 18, 2019
1 parent eea01a8 commit c0d4f48
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion translators/cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (p *Cockroach) DropForeignKey(t fizz.Table) (string, error) {
func (p *Cockroach) buildAddColumn(c fizz.Column) string {
s := fmt.Sprintf("\"%s\" %s", c.Name, p.colType(c))

if c.Options["null"] == nil || c.Primary {
if c.Options["null"] != true || c.Primary {
s = fmt.Sprintf("%s NOT NULL", s)
}
if c.Options["default"] != nil {
Expand Down
6 changes: 3 additions & 3 deletions translators/cockroach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ PRIMARY KEY("id"),
create_table("users") {
t.Column("id", "integer", {"primary": true})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "jsonb", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down Expand Up @@ -80,7 +80,7 @@ PRIMARY KEY("uuid"),
res, _ := fizz.AString(`
create_table("users") {
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "jsonb", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down Expand Up @@ -119,7 +119,7 @@ CONSTRAINT "profiles_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users" ("i
t.Column("id", "INT", {"primary": true})
t.Column("user_id", "INT", {})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.ForeignKey("user_id", {"users": ["id"]}, {})
}
`, p.crdbt())
Expand Down
4 changes: 2 additions & 2 deletions translators/mssqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (p *MsSqlServer) ChangeColumn(t fizz.Table) (string, error) {

cmds := make([]string, 0)
s := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s %s", t.Name, c.Name, p.colType(c))
if c.Options["null"] == nil {
if c.Options["null"] != true {
s = fmt.Sprintf("%s NOT NULL", s)
} else {
s = fmt.Sprintf("%s NULL", s)
Expand Down Expand Up @@ -197,7 +197,7 @@ func (p *MsSqlServer) DropForeignKey(t fizz.Table) (string, error) {

func (p *MsSqlServer) buildAddColumn(tableName string, c fizz.Column) string {
s := fmt.Sprintf("%s %s", c.Name, p.colType(c))
if c.Options["null"] == nil {
if c.Options["null"] != true {
s = fmt.Sprintf("%s NOT NULL", s)
}
setDefault := c.Options["default"] != nil || c.Options["default_raw"] != nil
Expand Down
6 changes: 3 additions & 3 deletions translators/mssqlserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ updated_at DATETIME NOT NULL
res, _ := fizz.AString(`
create_table("users") {
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "text", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand All @@ -72,7 +72,7 @@ updated_at DATETIME NOT NULL
res, _ := fizz.AString(`
create_table("users") {
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "text", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down Expand Up @@ -109,7 +109,7 @@ ALTER TABLE profiles ADD CONSTRAINT profiles_users_id_fk FOREIGN KEY (user_id) R
t.Column("id", "INT", {"primary": true})
t.Column("user_id", "INT", {})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.ForeignKey("user_id", {"users": ["id"]}, {})
}
`, sqlsrv)
Expand Down
2 changes: 1 addition & 1 deletion translators/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (p *MySQL) DropForeignKey(t fizz.Table) (string, error) {

func (p *MySQL) buildColumn(c fizz.Column) string {
s := fmt.Sprintf("`%s` %s", c.Name, p.colType(c))
if c.Options["null"] == nil || c.Primary {
if c.Options["null"] != true || c.Primary {
s = fmt.Sprintf("%s NOT NULL", s)
}
if c.Options["default"] != nil {
Expand Down
6 changes: 3 additions & 3 deletions translators/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ PRIMARY KEY(` + "`id`" + `),
create_table("users") {
t.Column("id", "integer", {"primary": true})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "text", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down Expand Up @@ -93,7 +93,7 @@ PRIMARY KEY(` + "`uuid`" + `),
res, err := fizz.AString(`
create_table("users") {
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "text", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down Expand Up @@ -134,7 +134,7 @@ FOREIGN KEY (` + "`user_id`" + `) REFERENCES ` + "`users`" + ` (` + "`id`" + `)
t.Column("id", "INT", {"primary": true})
t.Column("user_id", "INT", {})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.ForeignKey("user_id", {"users": ["id"]}, {})
}
`, myt)
Expand Down
4 changes: 2 additions & 2 deletions translators/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (p *Postgres) DropForeignKey(t fizz.Table) (string, error) {
func (p *Postgres) buildAddColumn(c fizz.Column) string {
s := fmt.Sprintf("\"%s\" %s", c.Name, p.colType(c))

if c.Options["null"] == nil || c.Primary {
if c.Options["null"] != true || c.Primary {
s = fmt.Sprintf("%s NOT NULL", s)
}
if c.Options["default"] != nil {
Expand All @@ -191,7 +191,7 @@ func (p *Postgres) buildChangeColumn(c fizz.Column) string {
s := fmt.Sprintf("\"%s\" TYPE %s", c.Name, p.colType(c))

var sets []string
if c.Options["null"] == nil {
if c.Options["null"] != true {
sets = append(sets, fmt.Sprintf("ALTER COLUMN \"%s\" SET NOT NULL", c.Name))
} else {
sets = append(sets, fmt.Sprintf("ALTER COLUMN \"%s\" DROP NOT NULL", c.Name))
Expand Down
6 changes: 3 additions & 3 deletions translators/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ PRIMARY KEY("id"),
create_table("users") {
t.Column("id", "integer", {"primary": true})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "jsonb", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down Expand Up @@ -65,7 +65,7 @@ PRIMARY KEY("uuid"),
res, _ := fizz.AString(`
create_table("users") {
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "jsonb", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down Expand Up @@ -145,7 +145,7 @@ FOREIGN KEY ("user_id") REFERENCES "users" ("id")
t.Column("id", "INT", {"primary": true})
t.Column("user_id", "INT", {})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.ForeignKey("user_id", {"users": ["id"]}, {})
}
`, pgt)
Expand Down
2 changes: 1 addition & 1 deletion translators/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (p *SQLite) withTempTable(table string, fn func(fizz.Table) (string, error)

func (p *SQLite) buildColumn(c fizz.Column) string {
s := fmt.Sprintf("\"%s\" %s", c.Name, p.colType(c))
if c.Options["null"] == nil {
if c.Options["null"] != true {
s = fmt.Sprintf("%s NOT NULL", s)
}
if c.Options["default"] != nil {
Expand Down
4 changes: 2 additions & 2 deletions translators/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (p *SQLiteSuite) Test_SQLite_CreateTable() {
create_table("users") {
t.Column("id", "integer", {"primary": true})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "text", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down Expand Up @@ -114,7 +114,7 @@ func (p *SQLiteSuite) Test_SQLite_CreateTable_UUID() {
res, _ := fizz.AString(`
create_table("users") {
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("last_name", "string", {"null": false})
t.Column("email", "string", {"size":20})
t.Column("permissions", "text", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
Expand Down

0 comments on commit c0d4f48

Please sign in to comment.