Skip to content

Commit

Permalink
Support float precision fixes #8 (#36)
Browse files Browse the repository at this point in the history
* Support float precision fixes #8

* Fix fixmie issues

* Fix bad comment formatting
  • Loading branch information
stanislas-m authored Nov 29, 2018
1 parent 8ac5462 commit df16e09
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 20 deletions.
11 changes: 11 additions & 0 deletions translators/cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions translators/cockroach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
31 changes: 21 additions & 10 deletions translators/mssqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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.")
Expand All @@ -112,15 +112,15 @@ 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
}

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]
Expand All @@ -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, ", "))
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -160,15 +160,15 @@ 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
}

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]
Expand Down Expand Up @@ -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":
Expand Down
9 changes: 9 additions & 0 deletions translators/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions translators/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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", {})
}
Expand Down
16 changes: 12 additions & 4 deletions translators/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"fmt"
"strings"

"github.com/pkg/errors"

"github.com/gobuffalo/fizz"
"github.com/pkg/errors"
)

type Postgres struct {
Expand Down Expand Up @@ -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":
Expand Down
6 changes: 4 additions & 2 deletions translators/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions translators/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit df16e09

Please sign in to comment.