Skip to content

Latest commit

 

History

History
540 lines (322 loc) · 9.15 KB

builder.md

File metadata and controls

540 lines (322 loc) · 9.15 KB

Builder API

import "github.com/ShkrutDenis/go-migrations/builder"

General, module have a four main function, which using like entrypoint:

Module has a next general types:

NewTable

NewTable() will init a structure for adding a new table.

Signature:

NewTable(string, *sqlx.DB) Table

Arguments:

  • Name of new table (type string)
  • Pointer to DB connection (type *sqlx.DB)

Return value:

ChangeTable

ChangeTable() will init structure for changing an existing table.

Signature:

ChangeTable(string, *sqlx.DB) Table

Arguments:

  • Name of existing table (type string)
  • Pointer to DB connection (type *sqlx.DB)

Return value:

RenameTable

RenameTable() will init structure for rename an existing table.

Signature:

RenameTable(string, string, *sqlx.DB) Table

Arguments:

  • Old name of existing table (type string)
  • New name for existing table (type string)
  • Pointer to DB connection (type *sqlx.DB)

Return value:

DropTable

DropTable() will init structure for dropping an existing table.

Signature:

DropTable(string, *sqlx.DB) Table

Arguments:

  • Name of existing table (type string)
  • Pointer to DB connection (type *sqlx.DB)

Return value:

Table type

Table has next methods:

Column

Column() will init a structure for adding a new column to the table.

Signature:

Column(string) Column

Arguments:

  • Name of new column (type string)

Return value:

String

String() will init a structure for adding a new column with type varchar(length) to the table.

Signature:

String(string, int) Column

Arguments:

  • Name of new column (type string)
  • Length of string (type int)

Return value:

Integer

Integer() will init a structure for adding a new column with type int to the table.

Signature:

Integer(string) Column

Arguments:

  • Name of new column (type string)

Return value:

WithTimeStamps

WithTimeStamps() will add columns created_at and updated_at to the table.

Signature:

WithTimestamps() Table

Return value:

RenameColumn

RenameColumn() will rename an existing column in the table.

Signature:

RenameColumn(string, string) Column

Arguments:

  • Old name of an existing column (type string)
  • New name for an existing column (type string)

Return value:

DropColumn

DropColumn() will drop an existing column from the table.

Signature:

DropColumn(string) Column

Arguments:

  • Name of an existing column (type string)

Return value:

PrimaryKey

PrimaryKey() will add primary key for an existing column or add the new column with type int and autoincrement to the table.

Signature:

PrimaryKey(string) Column

Arguments:

  • Name of an existing or new column (type string)

Return value:

ForeignKey

ForeignKey() will init structure for adding a new foreign key to the table.

Signature:

ForeignKey(string) ForeignKey

Arguments:

  • Name of column in the current table (type string)

Return value:

DropForeignKey

DropForeignKey() will drop foreign key from the table.

Signature:

DropForeignKey(string) Table

Arguments:

  • Name of an existing foreign kay (type string)

Return value:

GetSQL

GetSQL() will return generated sql.

Signature:

GetSQL() string

Return value:

  • string

Warning: result can be a several queries, each query ended on ;.

Exec

Exec() will execute generated sql.

Signature:

Exec() error

Return value:

  • error

MustExec

DropForeignKey() will execute generated sql.

Signature:

MustExec()

Warning: will be throw panic() when error.

Column type

Column has next methods:

Type

Type() will set a type for the column.

Signature:

Type(string) Column

Arguments:

  • Valid type for SQL (type string)

Return value:

Nullable

Nullable() will set NULL modifier for the column.

Signature:

Nullable() Column

Return value:

Warning: by default, the column will be created with NOT NULL modifier.

NotNull

NotNull() will set NOT NULL modifier for the column.

Signature:

NotNull() Column

Return value:

Autoincrement

Autoincrement() will set auto_increment modifier for the column.

Signature:

Autoincrement() Column

Return value:

Warning: this method used with Primary() method usually (if you don`t use PrimaryKey() method) Warning: no effect for PostgresSQL.

NotAutoincrement

NotAutoincrement() will remove auto_increment modifier for the column.

Signature:

NotAutoincrement() Column

Return value:

Warning: no effect for PostgresSQL.

Default

Default() will set a default value for the column.

Signature:

Default(string) Column

Arguments:

  • Valid value for column type (type string)

Return value:

Primary

Primary() will mark the column as primary key.

Signature:

Primary() Column

Return value:

Unique

Unique() will add for the column a unique key.

Signature:

Unique() Column

Return value:

NotUnique

NotUnique() will remove from the column a unique key.

Signature:

NotUnique() Column

Return value:

Drop

Drop() will mark the column for deleting, then this column will be dropped from table.

Signature:

Drop() Column

Return value:

Change

Change() will mark a column as existed, then column will be change by new modifiers.

Signature:

Change() Column

Return value:

First

First() will set column position as first.

Signature:

First() Column

Return value:

Warning: no effect for PostgresSQL.

After

After() will set column position after target column.

Signature:

After(string) Column

Arguments:

  • Name of existing column (type string)

Return value:

Warning: no effect for PostgresSQL.

Rename

Rename() will set a type for the column.

Signature:

Rename(string) Column

Arguments:

  • New name for existing column (type string)

Return value:

Foreign Key type

ForeignKey has next methods:

Reference

Reference() will set a target table name.

Signature:

Reference(string) ForeignKey

Arguments:

  • Name of an existing table (type string)

Return value:

On

On() will set a column name related to target table which was set by Referance().

Signature:

On(string) ForeignKey

Arguments:

  • Name of an existing column in target table (type string)

Return value:

OnUpdate

OnUpdate() will set a reference option on the update.

Signature:

OnUpdate(string) ForeignKey

Arguments:

  • Valid reference option (type string)

Return value:

OnDelete

OnDelete() will set a reference option on delete.

Signature:

OnDelete(string) ForeignKey

Arguments:

  • Valid reference option (type string)

Return value:

SetKeyName

SetKeyName() will set a custom name for the foreign kay.

Signature:

SetKeyName(string) ForeignKey

Arguments:

  • Name for foreign key (type string)

Return value:

Warning: that method not require. if key will be empty, key name will generate automatically by method GenerateKeyName().

GenerateKeyName

GenerateKeyName() will generate a name for foreign key in the next format: <base_table>_<target_table>_<target_column>_fk.

Signature:

GenerateKeyName() ForeignKey

Return value:

Warning: that method will be use automatically if foreign key name will be empty.