Giter VIP home page Giter VIP logo

fizz's Introduction

Fizz

Actions Status Go Reference

A Common DSL for Migrating Databases

Supported Database Engines

Fizz supports minimum supported version of all supported database engines. Currently, the following database engines are officially supported. (Since Fizz is used with the migration feature of Pop, supported databases and the versions are correlated with Pop.)

  • PostgreSQL 10
  • MySQL 5.7 / MariaDB 10.3
  • SQLite3 3.22
  • CockroachDB v21.1
  • MSSQL 2017 (not fully supported)

Usage

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", {})
  t.Index("email", {"unique": true})
}

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}

Composite primary key

create_table("user_privileges") {
	t.Column("user_id", "int")
	t.Column("privilege_id", "int")
	t.PrimaryKey("user_id", "privilege_id")
}

Please note that the t.PrimaryKey statement MUST be after the columns definitions.

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")

Development

Testing

To run end-to-end tests, use

make test

If you made changes to the end-to-end tests and want to update the fixtures, run the following command a couple of times until tests pass:

REFRESH_FIXTURES=true make test

fizz's People

Contributors

aeneasr avatar alexsante avatar bogh avatar dependabot[bot] avatar duckbrain avatar endihunter avatar fasmat avatar fdonzello avatar josegonzalez avatar julientant avatar koesie10 avatar kushwiz avatar larrymjordan avatar lumost avatar markbates avatar mclark4386 avatar mem avatar michsior14 avatar natedsaint avatar paganotoni avatar sbinet avatar seblw avatar sio4 avatar slashk avatar slomek avatar spankie avatar stangah avatar stanislas-m avatar timraymond avatar u007 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.