Skip to content

Commit

Permalink
Allow usage of options on PK in postgreSQL (#18)
Browse files Browse the repository at this point in the history
* Allow usage of options on PK in postgre

* make sure to declare the column before the PK modifier
  • Loading branch information
JulienTant authored and stanislas-m committed Aug 22, 2018
1 parent 7eecf9a commit cfb4532
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
14 changes: 14 additions & 0 deletions columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ func Test_Column_Stringer(t *testing.T) {
r.Equal(`t.Column("pk", "int", {primary: true})`, c.String())
})

t.Run("primary column with raw default", func(tt *testing.T) {
r := require.New(tt)
c := fizz.Column{
Name: "pk",
ColType: "int",
Primary: true,
Options: map[string]interface{}{
"default_raw": "uuid_generate_v4()",
},
}

r.Equal(`t.Column("pk", "int", {default_raw: "uuid_generate_v4()", primary: true})`, c.String())
})

t.Run("simple column", func(tt *testing.T) {
r := require.New(tt)
c := fizz.Column{
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module github.com/gobuffalo/fizz

require (
github.com/go-sql-driver/mysql v1.4.0
github.com/gobuffalo/envy v1.6.3
github.com/gobuffalo/plush v0.0.0-20180810170812-274552812256
github.com/jmoiron/sqlx v0.0.0-20180614180643-0dae4fefe7c0
github.com/pkg/errors v0.8.0
github.com/stretchr/testify v1.2.2
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24 // indirect
google.golang.org/appengine v1.1.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.0.0 h1:BrX964Rv5uQ3wwS+KRUAJCBBw5PQmgJfJ6v4yly5QwU=
github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/gobuffalo/envy v1.6.3 h1:I9iyNACF0Tovfta7iqLrUAXFHYBDBWveQrjpEv2XeWs=
github.com/gobuffalo/envy v1.6.3/go.mod h1:gOxUQY+OEwqH1a2m25Sqax1GIhj31tPNOIdFzj8QThs=
github.com/gobuffalo/github_flavored_markdown v1.0.0 h1:e2dK+SoHgOc/vfXuYMdXwEg2vAUlFzp8SBRwTOXckQ0=
Expand Down Expand Up @@ -52,5 +54,7 @@ golang.org/x/net v0.0.0-20180801234040-f4c29de78a2a/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24 h1:mEsFm194MmS9vCwxFy+zwu0EU7ZkxxMD1iH++vmGdUY=
golang.org/x/net v0.0.0-20180808004115-f9ce57c11b24/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/russross/blackfriday.v1 v1.5.1 h1:/G8rrKhg8HTP6/VLtuQLbNSXHUzpDdNqZS5umeauNvc=
gopkg.in/russross/blackfriday.v1 v1.5.1/go.mod h1:NAEMj3mL3YDCD1Mxuzav3y8y68EZs2OnJ9xdWYLES00=
16 changes: 8 additions & 8 deletions translators/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ func (p *Postgres) CreateTable(t fizz.Table) (string, error) {
for _, c := range t.Columns {
if c.Primary {
switch c.ColType {
case "string", "uuid":
s = fmt.Sprintf("\"%s\" %s PRIMARY KEY", c.Name, p.colType(c))
case "string", "uuid": // make sure that we don't fall into default
case "integer", "INT", "int":
s = fmt.Sprintf("\"%s\" SERIAL PRIMARY KEY", c.Name)
c.ColType = "SERIAL"
case "bigint", "BIGINT":
s = fmt.Sprintf("\"%s\" BIGSERIAL PRIMARY KEY", c.Name)
c.ColType = "BIGSERIAL"
default:
return "", errors.Errorf("can not use %s as a primary key", c.ColType)
}
} else {
s = p.buildAddColumn(c)
}
cols = append(cols, s)
cols = append(cols, p.buildAddColumn(c))
if c.Primary {
cols = append(cols, fmt.Sprintf(`PRIMARY KEY("%s")`, c.Name))
}
}

for _, fk := range t.ForeignKeys {
Expand Down Expand Up @@ -163,7 +163,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 {
if c.Options["null"] == nil || c.Primary {
s = fmt.Sprintf("%s NOT NULL", s)
}
if c.Options["default"] != nil {
Expand Down
44 changes: 40 additions & 4 deletions translators/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var pgt = translators.NewPostgres()
func (p *PostgreSQLSuite) Test_Postgres_CreateTable() {
r := p.Require()
ddl := `CREATE TABLE "users" (
"id" SERIAL PRIMARY KEY,
"id" SERIAL NOT NULL,
PRIMARY KEY("id"),
"first_name" VARCHAR (255) NOT NULL,
"last_name" VARCHAR (255) NOT NULL,
"email" VARCHAR (20) NOT NULL,
Expand Down Expand Up @@ -46,7 +47,8 @@ func (p *PostgreSQLSuite) Test_Postgres_CreateTable_UUID() {
"email" VARCHAR (20) NOT NULL,
"permissions" jsonb,
"age" integer DEFAULT '40',
"uuid" UUID PRIMARY KEY,
"uuid" UUID NOT NULL,
PRIMARY KEY("uuid"),
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL
);`
Expand All @@ -64,16 +66,50 @@ func (p *PostgreSQLSuite) Test_Postgres_CreateTable_UUID() {
r.Equal(ddl, res)
}

func (p *PostgreSQLSuite) Test_Postgres_CreateTable_UUID_With_Default() {
r := p.Require()
ddl := `CREATE TABLE "users" (
"uuid" UUID NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY("uuid")
);`

res, _ := fizz.AString(`
create_table("users") {
t.Column("uuid", "uuid", {"primary": true, "default_raw": "uuid_generate_v4()"})
t.DisableTimestamps()
}
`, pgt)
r.Equal(ddl, res)
}

func (p *PostgreSQLSuite) Test_Postgres_CreateTable_Cant_Set_PK_To_Nullable() {
r := p.Require()
ddl := `CREATE TABLE "users" (
"uuid" UUID NOT NULL,
PRIMARY KEY("uuid")
);`

res, _ := fizz.AString(`
create_table("users") {
t.Column("uuid", "uuid", {"primary": true, "null": true})
t.DisableTimestamps()
}
`, pgt)
r.Equal(ddl, res)
}

func (p *PostgreSQLSuite) Test_Postgre_CreateTables_WithForeignKeys() {
r := p.Require()
ddl := `CREATE TABLE "users" (
"id" SERIAL PRIMARY KEY,
"id" SERIAL NOT NULL,
PRIMARY KEY("id"),
"email" VARCHAR (20) NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL
);
CREATE TABLE "profiles" (
"id" SERIAL PRIMARY KEY,
"id" SERIAL NOT NULL,
PRIMARY KEY("id"),
"user_id" INT NOT NULL,
"first_name" VARCHAR (255) NOT NULL,
"last_name" VARCHAR (255) NOT NULL,
Expand Down

0 comments on commit cfb4532

Please sign in to comment.