Skip to content

Commit

Permalink
Adding common types from attr (#27)
Browse files Browse the repository at this point in the history
* adding common types from attr

* fixing test

* removing cockroachdata

* updating gitignore

* removing schema from the repository

* adding schema to the gitignore

* adding mysql mapping

* adding mysql common types

* removing vendor

* adding other 2 folders to gitignore

* re-enabling postgres and cockroack on test.sh

* reverting some changes

* adding common types to sqlite

* removing unneeded yml attributes
  • Loading branch information
paganotoni authored and stanislas-m committed Oct 24, 2018
1 parent ec9f6f3 commit c084213
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ generated/
bin/*
gin-bin
.idea/
cockroach-data/
migrations/schema.sql
vendor/
sqldumps/
sql/
2 changes: 1 addition & 1 deletion database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mysql:
host: {{ envOr "MYSQL_HOST" "127.0.0.1" }}
port: {{ envOr "MYSQL_PORT" "3306" }}
user: {{ envOr "MYSQL_USER" "root" }}
password: {{ envOr "MYSQL_PASSWORD" "root" }}
password: {{ envOr "MYSQL_PASSWORD" "root" }}

mysql_travis:
dialect: "mysql"
Expand Down
12 changes: 6 additions & 6 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ then
fi

docker-compose up -d
sleep 4 # Ensure mysql is online
sleep 10 # Ensure mysql is online

go get -v -tags sqlite github.com/gobuffalo/pop
go get -v -tags sqlite github.com/gobuffalo/pop/...
# go build -v -tags sqlite -o tsoda ./soda

function test {
echo "!!! Testing $1"
export SODA_DIALECT=$1
soda drop -e $SODA_DIALECT -c ./database.yml
soda create -e $SODA_DIALECT -c ./database.yml
soda migrate -e $SODA_DIALECT -c ./database.yml
soda drop -e $SODA_DIALECT
soda create -e $SODA_DIALECT
soda migrate -e $SODA_DIALECT
go test -tags sqlite $verbose $(go list ./... | grep -v /vendor/)
}

Expand All @@ -33,4 +33,4 @@ test "sqlite"

docker-compose down

find ./sql_scripts/sqlite -name *.sqlite* -delete
find ./sql_scripts/sqlite -name *.sqlite* -delete
9 changes: 7 additions & 2 deletions translators/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (p *MySQL) CreateTable(t fizz.Table) (string, error) {
}

s := fmt.Sprintf("CREATE TABLE %s (\n%s\n) ENGINE=InnoDB;", p.escapeIdentifier(t.Name), strings.Join(cols, ",\n"))

sql = append(sql, s)

for _, i := range t.Indexes {
Expand Down Expand Up @@ -230,8 +229,14 @@ func (p *MySQL) colType(c fizz.Column) string {
return "char(36)"
case "timestamp", "time", "datetime":
return "DATETIME"
case "blob":
case "blob", "[]byte":
return "BLOB"
case "int", "integer":
return "INTEGER"
case "float", "decimal":
return "FLOAT"
case "json":
return "JSON"
default:
return c.ColType
}
Expand Down
20 changes: 14 additions & 6 deletions translators/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ CREATE UNIQUE INDEX ` + "`version_idx`" + ` ON ` + "`schema_migrations`" + ` (`
func (p *MySQLSuite) Test_MySQL_CreateTable() {
r := p.Require()
ddl := `CREATE TABLE ` + "`users`" + ` (
` + "`id`" + ` integer NOT NULL AUTO_INCREMENT,
` + "`id`" + ` INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY(` + "`id`" + `),
` + "`first_name`" + ` VARCHAR (255) NOT NULL,
` + "`last_name`" + ` VARCHAR (255) NOT NULL,
` + "`email`" + ` VARCHAR (20) NOT NULL,
` + "`permissions`" + ` text,
` + "`age`" + ` integer DEFAULT 40,
` + "`age`" + ` INTEGER DEFAULT 40,
` + "`raw`" + ` BLOB NOT NULL,
` + "`json`" + ` JSON NOT NULL,
` + "`float`" + ` FLOAT NOT NULL,
` + "`integer`" + ` INTEGER NOT NULL,
` + "`bytes`" + ` BLOB NOT NULL,
` + "`created_at`" + ` DATETIME NOT NULL,
` + "`updated_at`" + ` DATETIME NOT NULL
) ENGINE=InnoDB;`
Expand All @@ -63,6 +67,10 @@ PRIMARY KEY(` + "`id`" + `),
t.Column("permissions", "text", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
t.Column("raw", "blob", {})
t.Column("json", "json", {})
t.Column("float", "float", {})
t.Column("integer", "integer", {})
t.Column("bytes", "[]byte", {})
}
`, myt)
r.NoError(err)
Expand All @@ -76,7 +84,7 @@ func (p *MySQLSuite) Test_MySQL_CreateTable_UUID() {
` + "`last_name`" + ` VARCHAR (255) NOT NULL,
` + "`email`" + ` VARCHAR (20) NOT NULL,
` + "`permissions`" + ` text,
` + "`age`" + ` integer DEFAULT 40,
` + "`age`" + ` INTEGER DEFAULT 40,
` + "`company_id`" + ` char(36) NOT NULL DEFAULT 'test',
` + "`uuid`" + ` char(36) NOT NULL,
PRIMARY KEY(` + "`uuid`" + `),
Expand All @@ -102,16 +110,16 @@ PRIMARY KEY(` + "`uuid`" + `),
func (p *MySQLSuite) Test_MySQL_CreateTables_WithForeignKeys() {
r := p.Require()
ddl := `CREATE TABLE ` + "`users`" + ` (
` + "`id`" + ` INT NOT NULL AUTO_INCREMENT,
` + "`id`" + ` INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY(` + "`id`" + `),
` + "`email`" + ` VARCHAR (20) NOT NULL,
` + "`created_at`" + ` DATETIME NOT NULL,
` + "`updated_at`" + ` DATETIME NOT NULL
) ENGINE=InnoDB;
CREATE TABLE ` + "`profiles`" + ` (
` + "`id`" + ` INT NOT NULL AUTO_INCREMENT,
` + "`id`" + ` INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY(` + "`id`" + `),
` + "`user_id`" + ` INT NOT NULL,
` + "`user_id`" + ` INTEGER NOT NULL,
` + "`first_name`" + ` VARCHAR (255) NOT NULL,
` + "`last_name`" + ` VARCHAR (255) NOT NULL,
` + "`created_at`" + ` DATETIME NOT NULL,
Expand Down
12 changes: 11 additions & 1 deletion translators/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,18 @@ func (p *Postgres) colType(c fizz.Column) string {
return "UUID"
case "time", "datetime":
return "timestamp"
case "blob":
case "blob", "[]byte":
return "bytea"
case "float":
return "decimal"
case "[]string":
return "varchar[]"
case "[]float":
return "decimal[]"
case "[]int":
return "integer[]"
case "json":
return "jsonb"
default:
return c.ColType
}
Expand Down
16 changes: 16 additions & 0 deletions translators/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (p *PostgreSQLSuite) Test_Postgres_CreateTable_UUID() {
"email" VARCHAR (20) NOT NULL,
"permissions" jsonb,
"age" integer DEFAULT '40',
"integer" integer 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,
"uuid" UUID NOT NULL,
PRIMARY KEY("uuid"),
"created_at" timestamp NOT NULL,
Expand All @@ -60,6 +68,14 @@ PRIMARY KEY("uuid"),
t.Column("email", "string", {"size":20})
t.Column("permissions", "jsonb", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
t.Column("integer", "integer", {})
t.Column("float", "float", {})
t.Column("bytes", "[]byte", {})
t.Column("strings", "[]string", {})
t.Column("floats", "[]float", {})
t.Column("ints", "[]int", {})
t.Column("jason", "json", {})
t.Column("mydecimal", "decimal", {})
t.Column("uuid", "uuid", {"primary": true})
}
`, pgt)
Expand Down
10 changes: 8 additions & 2 deletions translators/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,15 @@ func (p *SQLite) colType(c fizz.Column) string {
return "DATETIME"
case "boolean", "date":
return "NUMERIC"
case "string":
case "string", "text":
return "TEXT"
case "blob":
case "int", "integer":
return "INTEGER"
case "float":
return "REAL"
case "json":
return "TEXT"
case "blob", "[]byte":
return "BLOB"
default:
return c.ColType
Expand Down
16 changes: 12 additions & 4 deletions translators/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ func (p *SQLiteSuite) Test_SQLite_CreateTable() {
"first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"permissions" text,
"age" integer DEFAULT '40',
"permissions" TEXT,
"age" INTEGER DEFAULT '40',
"raw" BLOB NOT NULL,
"into" INTEGER NOT NULL,
"flotante" REAL NOT NULL,
"json" TEXT NOT NULL,
"bytes" BLOB NOT NULL,
"created_at" DATETIME NOT NULL,
"updated_at" DATETIME NOT NULL
);`
Expand All @@ -84,6 +88,10 @@ func (p *SQLiteSuite) Test_SQLite_CreateTable() {
t.Column("permissions", "text", {"null": true})
t.Column("age", "integer", {"null": true, "default": 40})
t.Column("raw", "blob", {})
t.Column("into", "int", {})
t.Column("flotante", "float", {})
t.Column("json", "json", {})
t.Column("bytes", "[]byte", {})
}
`, sqt)
r.Equal(ddl, res)
Expand All @@ -95,8 +103,8 @@ func (p *SQLiteSuite) Test_SQLite_CreateTable_UUID() {
"first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"permissions" text,
"age" integer DEFAULT '40',
"permissions" TEXT,
"age" INTEGER DEFAULT '40',
"company_id" char(36) NOT NULL DEFAULT lower(hex(randomblob(16))),
"uuid" TEXT PRIMARY KEY,
"created_at" DATETIME NOT NULL,
Expand Down

0 comments on commit c084213

Please sign in to comment.