fizz

package
v4.0.5+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2018 License: MIT Imports: 10 Imported by: 0

README

Fizz

A Common DSL for Migrating Databases

Create a Table

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 also generate an id column of type integer that will auto-increment. It will also generate two timestamp columns; created_at and updated_at.

Columns all have the same syntax. First is the name of the column. Second is the type of the field. Third is any options you want to set on that column.

"Common" Types:
  • string
  • text
  • timestamp, time, datetime
  • integer
  • bool

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.

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

raw("select * from users;")

All calls to raw must end with a ;!

Execute an External Command

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

exec("echo hello")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CREATED_COL = Column{Name: "created_at", ColType: "timestamp", Options: Options{}}
View Source
var INT_ID_COL = Column{
	Name:    "id",
	Primary: true,
	ColType: "integer",
	Options: Options{},
}
View Source
var UPDATED_COL = Column{Name: "updated_at", ColType: "timestamp", Options: Options{}}
View Source
var UUID_ID_COL = Column{
	Name:    "id",
	Primary: true,
	ColType: "uuid",
	Options: Options{},
}

Functions

func AFile

func AFile(f *os.File, t Translator) (string, error)

func AString

func AString(s string, t Translator) (string, error)

Types

type BubbleType

type BubbleType int

type Bubbler

type Bubbler struct {
	Translator
	// contains filtered or unexported fields
}

func NewBubbler

func NewBubbler(t Translator) *Bubbler

func (*Bubbler) Bubble

func (b *Bubbler) Bubble(s string) (string, error)

func (*Bubbler) String

func (b *Bubbler) String() string

type Column

type Column struct {
	Name    string
	ColType string
	Primary bool
	Options map[string]interface{}
}

type ForeignKey

type ForeignKey struct {
	Name       string
	Column     string
	References ForeignKeyRef
	Options    Options
}

type ForeignKeyRef

type ForeignKeyRef struct {
	Table   string
	Columns []string
}

type Index

type Index struct {
	Name    string
	Columns []string
	Unique  bool
	Options Options
}

type Options

type Options map[string]interface{}

type Table

type Table struct {
	Name        string `db:"name"`
	Columns     []Column
	Indexes     []Index
	ForeignKeys []ForeignKey
	Options     map[string]interface{}
}

func (*Table) Column

func (t *Table) Column(name string, colType string, options map[string]interface{})

func (*Table) ColumnNames

func (t *Table) ColumnNames() []string

func (*Table) DisableTimestamps

func (t *Table) DisableTimestamps()

func (*Table) ForeignKey

func (t *Table) ForeignKey(column string, refs interface{}, options Options)

func (*Table) HasColumns

func (t *Table) HasColumns(args ...string) bool

func (*Table) Timestamp

func (t *Table) Timestamp(name string)

func (*Table) Timestamps

func (t *Table) Timestamps()

type Translator

type Translator interface {
	CreateTable(Table) (string, error)
	DropTable(Table) (string, error)
	RenameTable([]Table) (string, error)
	AddColumn(Table) (string, error)
	ChangeColumn(Table) (string, error)
	DropColumn(Table) (string, error)
	RenameColumn(Table) (string, error)
	AddIndex(Table) (string, error)
	DropIndex(Table) (string, error)
	RenameIndex(Table) (string, error)
	AddForeignKey(Table) (string, error)
	DropForeignKey(Table) (string, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL