From 3215da2f480b646435971aed110961bedbf3637d Mon Sep 17 00:00:00 2001 From: Sueyasu Date: Mon, 18 Nov 2019 22:32:10 +0900 Subject: [PATCH] Support null: false option --- translators/cockroach.go | 2 +- translators/cockroach_test.go | 6 +++--- translators/mssqlserver.go | 4 ++-- translators/mssqlserver_test.go | 6 +++--- translators/mysql.go | 2 +- translators/mysql_test.go | 6 +++--- translators/postgres.go | 4 ++-- translators/postgres_test.go | 6 +++--- translators/sqlite.go | 2 +- translators/sqlite_test.go | 4 ++-- 10 files changed, 21 insertions(+), 21 deletions(-) diff --git a/translators/cockroach.go b/translators/cockroach.go index 0421adbc..fdaf0c16 100644 --- a/translators/cockroach.go +++ b/translators/cockroach.go @@ -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 { diff --git a/translators/cockroach_test.go b/translators/cockroach_test.go index 70457a45..e5850527 100644 --- a/translators/cockroach_test.go +++ b/translators/cockroach_test.go @@ -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}) @@ -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}) @@ -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()) diff --git a/translators/mssqlserver.go b/translators/mssqlserver.go index 6fd27e46..ef007115 100644 --- a/translators/mssqlserver.go +++ b/translators/mssqlserver.go @@ -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) @@ -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 diff --git a/translators/mssqlserver_test.go b/translators/mssqlserver_test.go index 896e88a6..eb5e2bd9 100644 --- a/translators/mssqlserver_test.go +++ b/translators/mssqlserver_test.go @@ -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}) @@ -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}) @@ -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) diff --git a/translators/mysql.go b/translators/mysql.go index 1ce51c26..82c56e25 100644 --- a/translators/mysql.go +++ b/translators/mysql.go @@ -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 { diff --git a/translators/mysql_test.go b/translators/mysql_test.go index bbe14793..88de6bb7 100644 --- a/translators/mysql_test.go +++ b/translators/mysql_test.go @@ -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}) @@ -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}) @@ -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) diff --git a/translators/postgres.go b/translators/postgres.go index 60d5152f..1e1f9f52 100644 --- a/translators/postgres.go +++ b/translators/postgres.go @@ -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 { @@ -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)) diff --git a/translators/postgres_test.go b/translators/postgres_test.go index afd44dff..149c55be 100644 --- a/translators/postgres_test.go +++ b/translators/postgres_test.go @@ -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}) @@ -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}) @@ -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) diff --git a/translators/sqlite.go b/translators/sqlite.go index fbb71a29..14f0fa75 100644 --- a/translators/sqlite.go +++ b/translators/sqlite.go @@ -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 { diff --git a/translators/sqlite_test.go b/translators/sqlite_test.go index 4e022a4e..600f82e3 100644 --- a/translators/sqlite_test.go +++ b/translators/sqlite_test.go @@ -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}) @@ -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})