A Common DSL for Migrating Databases
Clone or download
stanislas-m Add MariaDB support (#25)
* Add MariaDB support

* Add missing files
Latest commit 0732b67 Dec 5, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Remove sqlx dep (#31) Nov 10, 2018
migrations Fixes `t.DisableTimestamps` and enables `create_table("foo", {timesta… Jun 29, 2018
translators Add MariaDB support (#25) Dec 5, 2018
.gitignore Adding common types from attr (#27) Oct 24, 2018
.gometalinter.json fixes a broken go.mod (#26) Sep 27, 2018
.travis.yml Improve Travis build (#30) Oct 29, 2018
LICENSE.txt added a whole bunch of documentation Feb 2, 2016
Makefile updated deps and added package level doc (#28) Oct 23, 2018
README.md Fix doc about auto-generated ID (#35) Nov 15, 2018
bubbler.go Rename RawSql to RawSQL to follow conventions (#19) Aug 20, 2018
bubbler_test.go use shellquote (#24) Sep 23, 2018
columns.go Remove ID column enforcement (#15) Aug 20, 2018
columns_test.go Allow usage of options on PK in postgreSQL (#18) Aug 22, 2018
database.yml MS Sql Server support (#1) Nov 10, 2018
docker-compose.yml MS Sql Server support (#1) Nov 10, 2018
fizz.go updated deps and added package level doc (#28) Oct 23, 2018
foreign_keys.go working on cleaning up the API Jun 16, 2018
go.mod Remove sqlx dep (#31) Nov 10, 2018
go.sum Remove sqlx dep (#31) Nov 10, 2018
index.go working on cleaning up the API Jun 16, 2018
raw_sql.go Rename RawSql to RawSQL to follow conventions (#19) Aug 20, 2018
shoulders.md version bump: v1.0.15 Oct 25, 2018
tables.go Remove ID column enforcement (#15) Aug 20, 2018
tables_test.go Add Table and Column Stringer implementation (#14) Aug 16, 2018
test.sh MS Sql Server support (#1) Nov 10, 2018
translator.go Rename RawSql to RawSQL to follow conventions (#19) Aug 20, 2018
version.go version bump: v1.0.15 Oct 25, 2018

README.md

Fizz

A Common DSL for Migrating Databases

Create a Table

create_table("users") {
  t.Column("id", "integer", {primary: true})
  t.Column("email", "string", {})
  t.Column("twitter_handle", "string", {"size": 50})
  t.Column("age", "integer", {"default": 0})
  t.Column("admin", "bool", {"default": false})
  t.Column("company_id", "uuid", {"default_raw": "uuid_generate_v1()"})
  t.Column("bio", "text", {"null": true})
  t.Column("joined_at", "timestamp", {})
}

create_table("todos") {
  t.Column("user_id", "integer", {})
  t.Column("title", "string", {"size": 100})
  t.Column("details", "text", {"null": true})
  t.ForeignKey("user_id", {"users": ["id"]}, {"on_delete": "cascade"})
}

The id column don't have to be an integer. For instance, your can use an UUID type instead:

create_table("users") {
  t.Column("id", "uuid", {primary: true})
  // ...
}

By default, fizz will generate two timestamp columns: created_at and updated_at.

The t.Columns method takes the following arguments: name of the column, the type of the field, and finally the last argument is any options you want to set on that column.

"Common" Types:

  • string
  • text
  • timestamp, time, datetime
  • integer
  • bool
  • uuid

Any other type passed it will be be passed straight through to the underlying database.

For example for PostgreSQL you could pass jsonband it will be supported, however, SQLite will yell very loudly at you if you do the same thing!

Supported Options:

  • size - The size of the column. For example if you wanted a varchar(50) in Postgres you would do: t.Column("column_name", "string", {"size": 50})
  • null - By default columns are not allowed to be null.
  • default - The default value you want for this column. By default this is null.
  • default_raw - The default value defined as a database function.
  • after - (MySQL Only) Add a column after another column in the table. example: {"after":"created_at"}
  • first - (MySQL Only) Add a column to the first position in the table. example: {"first": true}

Drop a Table

drop_table("table_name")

Rename a Table

rename_table("old_table_name", "new_table_name")

Add a Column

add_column("table_name", "column_name", "string", {})

See above for more details on column types and options.

Alter a column

change_column("table_name", "column_name", "string", {})

Rename a Column

rename_column("table_name", "old_column_name", "new_column_name")

Drop a Column

drop_column("table_name", "column_name")

Add an Index

Supported Options:

  • name - This defaults to table_name_column_name_idx
  • unique

Simple Index:

add_index("table_name", "column_name", {})

Multi-Column Index:

add_index("table_name", ["column_1", "column_2"], {})

Unique Index:

add_index("table_name", "column_name", {"unique": true})

Index Names:

add_index("table_name", "column_name", {}) # name => table_name_column_name_idx
add_index("table_name", "column_name", {"name": "custom_index_name"})

Rename an Index

rename_index("table_name", "old_index_name", "new_index_name")

Drop an Index

drop_index("table_name", "index_name")

Add a Foreign Key

add_foreign_key("table_name", "field", {"ref_table_name": ["ref_column"]}, {
    "name": "optional_fk_name",
    "on_delete": "action",
    "on_update": "action",
})

Supported Options

  • name - This defaults to table_name_ref_table_name_ref_column_name_fk
  • on_delete - CASCADE, SET NULL, ...
  • on_update

Note: on_update and on_delete are not supported on CockroachDB yet.

Drop a Foreign Key

drop_foreign_key("table_name", "fk_name", {"if_exists": true})

Supported Options

  • if_exists - Adds IF EXISTS condition

Raw SQL

sql("select * from users;")

Execute an External Command

Sometimes during a migration you need to shell out to an external command.

exec("echo hello")