create_table("users", func(t) {
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", func(t) {
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 create_table
function will generate an id
column of type integer
that will auto-increment. This can be changed to use the UUID
type:
create_table("users", func(t) {
t.Column("id", "uuid", {})
// ...
})
It will also 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.
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 jsonb
and it will be supported, however, SQLite will yell very loudly at you if you do the same thing!
size
- The size of the column. For example if you wanted avarchar(50)
in Postgres you would do:t.Column("column_name", "string", {"size": 50})
null
- By default columns are not allowed to benull
.default
- The default value you want for this column. By default this isnull
.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_table("table_name")
rename_table("old_table_name", "new_table_name")
add_column("table_name", "column_name", "string", {})
See above for more details on column types and options.
change_column("table_name", "column_name", "string", {})
rename_column("table_name", "old_column_name", "new_column_name")
drop_column("table_name", "column_name")
name
- This defaults totable_name_column_name_idx
unique
add_index("table_name", "column_name", {})
add_index("table_name", ["column_1", "column_2"], {})
add_index("table_name", "column_name", {"unique": true})
add_index("table_name", "column_name", {}) # name => table_name_column_name_idx
add_index("table_name", "column_name", {"name": "custom_index_name"})
rename_index("table_name", "old_index_name", "new_index_name")
drop_index("table_name", "index_name")
add_foreign_key("table_name", "field", {"ref_table_name": ["ref_column"]}, {
"name": "optional_fk_name",
"on_delete": "action",
"on_update": "action",
})
name
- This defaults totable_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_foreign_key("table_name", "fk_name", {"if_exists": true})
if_exists
- AddsIF EXISTS
condition
raw("select * from users;")
All calls to raw
must end with a ;
!
Sometimes during a migration you need to shell out to an external command.
exec("echo hello")