Name: linq
Owner: gophergala2016
Description: A migration toolkit writted in Golang and inspired on Rails' ActiveRecord::Migration, it allows you to create migration files to make change on your DB using a DSL.
Created: 2016-01-23 02:31:40.0
Updated: 2018-04-15 22:05:44.0
Pushed: 2016-01-24 18:10:54.0
Size: 4017
Language: Go
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A migration toolkit writted in Golang and inspired on Rails' ActiveRecord::Migration, that allows you to create migration files to make change on your DB using a DSL.
Execute the following go get command to install the toolkit from the github repository
et github.com/gophergala2016/linq/migrin
This gives you access to the migrin command to execute different actions
migrin init
migrin new <MigrationName>
migrin up
migrin down
As you may see, there are two environments on config.yml production|development this allows you to separate the credentials on each database, and only run changs on your production DB when explicitly specified.
To run commands in production append the -production
flag, or it shorthand -p
, to migrin commands
The following example executes migrations with production credentials
in up -p
Migrin commands are pretty awesome, you can't even tell it what changes dou you want to make tou your database through the terminal, no need to open migrations or remembering SQL. Here's an example
Given that I want to create a users table with email,password and age fields:
in new CreateUsersTable create_table users email:varchar password:varchar age:int
Migrin will generate the following migration:
age main
rt(
"github.com/gophergala2016/linq/migrator"
"os"
main(){
migrator.Options(os.Args)
columns = []migrator.ColumnBuilder{{Name:"email",Data_type:"varchar"},{Name:"password",Data_type:"varchar"},{Name:"age",Data_type:"int"}
migrator.CreateTable("users",columns)
And there you have, your table ready to go… this is the sintax to generate such awesome migrations:
in new migration_name command table_name column_name:data_type...
There's no limit on how much columns you can add, every argument after the table_name is considered a new column for the table.
Here are the available commands:
| command | action | |—————|——————————————————-| | create_table | To create a new table | | add_columns | Add specified columns to the indicated table | | remove_column | Remove the specified columns from the indicated table |
The beauty of migrin is that you don't need to write SQL to define what your migration should do, you use a simple API to modify your database
Creates a new table with the specified name (1st argument) and the specified columns, defined by an slice of ColumnBuilder's
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
migrator.CreateTable("courses",[]migrator.ColumnBuilder{{Name:"title"},{Name:"description"}})
Drops the specified table from the database
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
migrator.DropTable("courses")
Adds a column to an already created table
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
migrator.AddColumn("courses",ColumnBuilder{Name:'status',Data_type:'int'})
Removes a column from the specified table
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
migrator.RemoveColumn("courses","status")
Changes the column structure of an existing column from the specified table, the column name is obtained from the ColumBuilder struct, if you want to change the name of the column you need to fill the new_name attribute from the ColumnBuilder struct
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
//Changes the column name status to state
migrator.ChangeColumn("courses",migrator.ColumnBuilder{Name:"status",New_name:"state"})
Adds an index to the specified table and column
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
migrator.AddIndex("courses","status_index","status")
Removes the specified index from the specified talbe
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
migrator.RemoveIndex("courses","status_index")
Creates a foreign key between two columns, the tables of each column are specified in a table attribute in the ColumnBuilder struct
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
migrator.AddForeignKey(migrator.ColumnBuilder{Name:"id",Table:"courses"},ColumnBuilder{Name:"course_id",Table:"videos"})
Removes a foreign key, the foreign key to eliminate is specified in the ForeignKey attribute of a ColumnBuilder
Example
age main
rt(
"github.com/gophergala2016/linq/migrator"
main(){
migrator.Options(os.Args)
migrator.RemoveForeignKey(migrator.ColumnBuilder{ForeignKey:"foreign_key"})
A struct that defines the atributtes for a column, it's used for multiple methods of the DSL, it accepts the following attributes:
string
_type string
th int
bool
ary_key bool
x bool
_increment bool
ult_value string
name string
e string
ignKey string
Created for the GopherGala2016 hackathon.