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() 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() 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() 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() 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 has next methods:
- Column()
- String()
- Integer()
- WithTimeStamps()
- RenameColumn()
- DropColumn()
- PrimaryKey()
- ForeignKey()
- DropForeignKey()
- GetSQL()
- Exec()
- MustExec()
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() 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() 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() will add columns created_at
and updated_at
to the table.
Signature:
WithTimestamps() Table
Return value:
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() will drop an existing column from the table.
Signature:
DropColumn(string) Column
Arguments:
- Name of an existing column (type string)
Return value:
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() 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() will drop foreign key from the table.
Signature:
DropForeignKey(string) Table
Arguments:
- Name of an existing foreign kay (type string)
Return value:
GetSQL() will return generated sql.
Signature:
GetSQL() string
Return value:
- string
Warning: result can be a several queries, each query ended on ;
.
Exec() will execute generated sql.
Signature:
Exec() error
Return value:
- error
DropForeignKey() will execute generated sql.
Signature:
MustExec()
Warning: will be throw panic()
when error.
Column has next methods:
- Type()
- Nullable()
- NotNull()
- Autoincrement()
- NotAutoincrement()
- Default()
- Primary()
- Unique()
- NotUnique()
- Drop()
- Change()
- First()
- After()
- Rename()
Type() will set a type for the column.
Signature:
Type(string) Column
Arguments:
- Valid type for SQL (type string)
Return value:
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() will set NOT NULL
modifier for the column.
Signature:
NotNull() Column
Return value:
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() will remove auto_increment
modifier for the column.
Signature:
NotAutoincrement() Column
Return value:
Warning: no effect for PostgresSQL.
Default() will set a default value for the column.
Signature:
Default(string) Column
Arguments:
- Valid value for column type (type string)
Return value:
Primary() will mark the column as primary key.
Signature:
Primary() Column
Return value:
Unique() will add for the column a unique key.
Signature:
Unique() Column
Return value:
NotUnique() will remove from the column a unique key.
Signature:
NotUnique() Column
Return value:
Drop() will mark the column for deleting, then this column will be dropped from table.
Signature:
Drop() Column
Return value:
Change() will mark a column as existed, then column will be change by new modifiers.
Signature:
Change() Column
Return value:
First() will set column position as first.
Signature:
First() Column
Return value:
Warning: no effect for PostgresSQL.
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() will set a type for the column.
Signature:
Rename(string) Column
Arguments:
- New name for existing column (type string)
Return value:
ForeignKey has next methods:
Reference() will set a target table name.
Signature:
Reference(string) ForeignKey
Arguments:
- Name of an existing table (type string)
Return value:
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() will set a reference option on the update.
Signature:
OnUpdate(string) ForeignKey
Arguments:
- Valid reference option (type string)
Return value:
OnDelete() will set a reference option on delete.
Signature:
OnDelete(string) ForeignKey
Arguments:
- Valid reference option (type string)
Return value:
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() 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.