diff --git a/translators/cockroach.go b/translators/cockroach.go index 2e629d5d..15ef275d 100644 --- a/translators/cockroach.go +++ b/translators/cockroach.go @@ -376,6 +376,17 @@ func (p *Cockroach) colType(c fizz.Column) string { return "timestamp" case "blob": return "BYTES" + case "float", "decimal": + if c.Options["precision"] != nil { + precision := c.Options["precision"] + if c.Options["scale"] != nil { + scale := c.Options["scale"] + return fmt.Sprintf("DECIMAL(%d,%d)", precision, scale) + } + return fmt.Sprintf("DECIMAL(%d)", precision) + } + + return "DECIMAL" default: return c.ColType } diff --git a/translators/cockroach_test.go b/translators/cockroach_test.go index 5f198b90..99f96e44 100644 --- a/translators/cockroach_test.go +++ b/translators/cockroach_test.go @@ -40,6 +40,7 @@ func (p *CockroachSuite) Test_Cockroach_CreateTable() { "permissions" jsonb, "age" integer DEFAULT '40', "raw" BYTES NOT NULL, +"float" DECIMAL(5) NOT NULL, "company_id" UUID NOT NULL DEFAULT uuid_generate_v1(), "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL @@ -54,6 +55,7 @@ func (p *CockroachSuite) Test_Cockroach_CreateTable() { t.Column("permissions", "jsonb", {"null": true}) t.Column("age", "integer", {"null": true, "default": 40}) t.Column("raw", "blob", {}) + t.Column("float", "float", {"precision": 5}) t.Column("company_id", "uuid", {"default_raw": "uuid_generate_v1()"}) } `, p.crdbt()) diff --git a/translators/mssqlserver.go b/translators/mssqlserver.go index b96600b6..894c8c83 100644 --- a/translators/mssqlserver.go +++ b/translators/mssqlserver.go @@ -56,14 +56,14 @@ func (p *MsSqlServer) DropTable(t fizz.Table) (string, error) { func (p *MsSqlServer) RenameTable(t []fizz.Table) (string, error) { if len(t) < 2 { - return "", errors.New("Not enough table names supplied!") + return "", errors.New("Not enough table names supplied") } return fmt.Sprintf("EXEC sp_rename '%s', '%s';", t[0].Name, t[1].Name), nil } func (p *MsSqlServer) ChangeColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("Not enough columns supplied") } c := t.Columns[0] @@ -95,7 +95,7 @@ func (p *MsSqlServer) ChangeColumn(t fizz.Table) (string, error) { func (p *MsSqlServer) AddColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } //if _, ok := t.Columns[0].Options["first"]; ok { // return "", fmt.Errorf("T-SQL does not support adding column at a specific position.") @@ -112,7 +112,7 @@ func (p *MsSqlServer) AddColumn(t fizz.Table) (string, error) { func (p *MsSqlServer) DropColumn(t fizz.Table) (string, error) { if len(t.Columns) == 0 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } c := t.Columns[0] return fmt.Sprintf("ALTER TABLE %s DROP COLUMN %s;", t.Name, c.Name), nil @@ -120,7 +120,7 @@ func (p *MsSqlServer) DropColumn(t fizz.Table) (string, error) { func (p *MsSqlServer) RenameColumn(t fizz.Table) (string, error) { if len(t.Columns) < 2 { - return "", errors.New("Not enough columns supplied!") + return "", errors.New("not enough columns supplied") } oc := t.Columns[0] nc := t.Columns[1] @@ -130,7 +130,7 @@ func (p *MsSqlServer) RenameColumn(t fizz.Table) (string, error) { func (p *MsSqlServer) AddIndex(t fizz.Table) (string, error) { if len(t.Indexes) == 0 { - return "", errors.New("Not enough indexes supplied!") + return "", errors.New("not enough indexes supplied") } i := t.Indexes[0] s := fmt.Sprintf("CREATE INDEX %s ON %s (%s);", i.Name, t.Name, strings.Join(i.Columns, ", ")) @@ -142,7 +142,7 @@ func (p *MsSqlServer) AddIndex(t fizz.Table) (string, error) { func (p *MsSqlServer) DropIndex(t fizz.Table) (string, error) { if len(t.Indexes) == 0 { - return "", errors.New("Not enough indexes supplied!") + return "", errors.New("not enough indexes supplied") } i := t.Indexes[0] return fmt.Sprintf("DROP INDEX %s ON %s;", i.Name, t.Name), nil @@ -151,7 +151,7 @@ func (p *MsSqlServer) DropIndex(t fizz.Table) (string, error) { func (p *MsSqlServer) RenameIndex(t fizz.Table) (string, error) { ix := t.Indexes if len(ix) < 2 { - return "", errors.New("Not enough indexes supplied!") + return "", errors.New("not enough indexes supplied") } oi := ix[0] ni := ix[1] @@ -160,7 +160,7 @@ func (p *MsSqlServer) RenameIndex(t fizz.Table) (string, error) { func (p *MsSqlServer) AddForeignKey(t fizz.Table) (string, error) { if len(t.ForeignKeys) == 0 { - return "", errors.New("Not enough foreign keys supplied!") + return "", errors.New("not enough foreign keys supplied") } return p.buildForeignKey(t, t.ForeignKeys[0]), nil @@ -168,7 +168,7 @@ func (p *MsSqlServer) AddForeignKey(t fizz.Table) (string, error) { func (p *MsSqlServer) DropForeignKey(t fizz.Table) (string, error) { if len(t.ForeignKeys) == 0 { - return "", errors.New("Not enough foreign keys supplied!") + return "", errors.New("not enough foreign keys supplied") } fk := t.ForeignKeys[0] @@ -215,6 +215,17 @@ func (p *MsSqlServer) colType(c fizz.Column) string { return "uniqueidentifier" case "blob": return "VARBINARY(MAX)" + case "float", "decimal": + if c.Options["precision"] != nil { + precision := c.Options["precision"] + if c.Options["scale"] != nil { + scale := c.Options["scale"] + return fmt.Sprintf("DECIMAL(%d,%d)", precision, scale) + } + return fmt.Sprintf("DECIMAL(%d)", precision) + } + + return "DECIMAL" case "timestamp": return "DATETIME" case "boolean": diff --git a/translators/mysql.go b/translators/mysql.go index 91b8f351..ab1e004f 100644 --- a/translators/mysql.go +++ b/translators/mysql.go @@ -233,6 +233,15 @@ func (p *MySQL) colType(c fizz.Column) string { case "int", "integer": return "INTEGER" case "float", "decimal": + if c.Options["precision"] != nil { + precision := c.Options["precision"] + if c.Options["scale"] != nil { + scale := c.Options["scale"] + return fmt.Sprintf("FLOAT(%d,%d)", precision, scale) + } + return fmt.Sprintf("FLOAT(%d)", precision) + } + return "FLOAT" case "json": return "JSON" diff --git a/translators/mysql_test.go b/translators/mysql_test.go index f47e07b1..fef02206 100644 --- a/translators/mysql_test.go +++ b/translators/mysql_test.go @@ -3,8 +3,7 @@ package translators_test import ( "fmt" - // Load MySQL Go driver - _ "github.com/go-sql-driver/mysql" + _ "github.com/go-sql-driver/mysql" // Load MySQL Go driver "github.com/gobuffalo/envy" "github.com/gobuffalo/fizz" "github.com/gobuffalo/fizz/translators" @@ -51,7 +50,7 @@ PRIMARY KEY(` + "`id`" + `), ` + "`age`" + ` INTEGER DEFAULT 40, ` + "`raw`" + ` BLOB NOT NULL, ` + "`json`" + ` JSON NOT NULL, -` + "`float`" + ` FLOAT NOT NULL, +` + "`float`" + ` FLOAT(5) NOT NULL, ` + "`integer`" + ` INTEGER NOT NULL, ` + "`bytes`" + ` BLOB NOT NULL, ` + "`created_at`" + ` DATETIME NOT NULL, @@ -68,7 +67,7 @@ PRIMARY KEY(` + "`id`" + `), t.Column("age", "integer", {"null": true, "default": 40}) t.Column("raw", "blob", {}) t.Column("json", "json", {}) - t.Column("float", "float", {}) + t.Column("float", "float", {"precision": 5}) t.Column("integer", "integer", {}) t.Column("bytes", "[]byte", {}) } diff --git a/translators/postgres.go b/translators/postgres.go index f158660c..4c9346c1 100644 --- a/translators/postgres.go +++ b/translators/postgres.go @@ -4,9 +4,8 @@ import ( "fmt" "strings" - "github.com/pkg/errors" - "github.com/gobuffalo/fizz" + "github.com/pkg/errors" ) type Postgres struct { @@ -212,8 +211,17 @@ func (p *Postgres) colType(c fizz.Column) string { return "timestamp" case "blob", "[]byte": return "bytea" - case "float": - return "decimal" + case "float", "decimal": + if c.Options["precision"] != nil { + precision := c.Options["precision"] + if c.Options["scale"] != nil { + scale := c.Options["scale"] + return fmt.Sprintf("DECIMAL(%d,%d)", precision, scale) + } + return fmt.Sprintf("DECIMAL(%d)", precision) + } + + return "DECIMAL" case "[]string": return "varchar[]" case "[]float": diff --git a/translators/postgres_test.go b/translators/postgres_test.go index 9d24c576..5594d0e1 100644 --- a/translators/postgres_test.go +++ b/translators/postgres_test.go @@ -48,13 +48,14 @@ func (p *PostgreSQLSuite) Test_Postgres_CreateTable_UUID() { "permissions" jsonb, "age" integer DEFAULT '40', "integer" integer NOT NULL, -"float" decimal NOT NULL, +"float" DECIMAL NOT NULL, "bytes" bytea NOT NULL, "strings" varchar[] NOT NULL, "floats" decimal[] NOT NULL, "ints" integer[] NOT NULL, "jason" jsonb NOT NULL, -"mydecimal" decimal NOT NULL, +"mydecimal" DECIMAL NOT NULL, +"mydecimal2" DECIMAL(5,2) NOT NULL, "uuid" UUID NOT NULL, PRIMARY KEY("uuid"), "created_at" timestamp NOT NULL, @@ -76,6 +77,7 @@ PRIMARY KEY("uuid"), t.Column("ints", "[]int", {}) t.Column("jason", "json", {}) t.Column("mydecimal", "decimal", {}) + t.Column("mydecimal2", "decimal", {"precision": 5, "scale": 2}) t.Column("uuid", "uuid", {"primary": true}) } `, pgt) diff --git a/translators/sqlite.go b/translators/sqlite.go index 17a97439..8a00a6ef 100644 --- a/translators/sqlite.go +++ b/translators/sqlite.go @@ -388,6 +388,7 @@ func (p *SQLite) colType(c fizz.Column) string { case "int", "integer": return "INTEGER" case "float": + // precision and scale not supported here return "REAL" case "json": return "TEXT"