Pasang Iklan Atas 1

Down Menu

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, February 17, 2020

Golang Migrate Mysql with CLI

This is standard documentation for golang-migrate and mysql driver

About golang-migrate CLI

You can see the migrate CLI documentation usage in here CLI Documentation

Installation golang-migrate CLI

This is example installation for mysql driver migrate. you can see another database in here Golang Migrate Documentation
$ go get -u -d github.com/golang-migrate/migrate/cmd/migrate github.com/go-sql-driver/mysql
$ go build -tags 'mysql' -o /usr/local/bin/migrate github.com/golang-migrate/migrate/cmd/migrate

Migrate CLI usage

You can see the documentation usage with command of migrate -help or you can see in here Migrate CLI Usage

Run Migration

$ migrate -database mysql://user:password@/dbname -path ./migrations up N

Rollback Migration

$ migrate -database mysql://user:passwrod@/dbname -path ./migrations down N

Recover Failed Migration

$ migrate -database mysql://user:password@/dbname -path ./migrations force N
Note: N is version that you wanted, if you want to execute all you can remove N

Migrations Best Practices

This best practices based on reference in here Migration Version Format

Migration Filename Format

migrate expects the filenames of migrations to have the format:
{version}_{title}.up.{extension}
{version}_{title}.down.{extension}
Common versioning schemes include incrementing integers:
1_user.down.sql
1_user.up.sql
2_role.down.sql
2_role.up.sql
...
Or timestamps at an appropriate resolution:
1500360784_user.down.sql
1500360784_user.up.sql
1500445949_role.down.sql
1500445949_role.up.sql
...

SQL Statement for Migration Files

For example up file 1_user.up.sql
CREATE TABLE IF NOT EXISTS user (
    id INTEGER NOT NULL AUTO_INCREMENT KEY,
    role_id INTEGER,
    user_image_path VARCHAR (250),
    first_name VARCHAR (100),
    second_name VARCHAR (100),
    email VARCHAR (30) UNIQUE,
    password VARCHAR (30),
    phone_number VARCHAR (20),
    address VARCHAR (250),
    created_by VARCHAR(100),
    created_at DATETIME,
    updated_by VARCHAR(100),
    updated_at DATETIME
);
For example down file 1_user.down.sql
DROP TABLE IF EXISTS user;
See more documentation SQL Statement in here SQL Statement

Source: 

2 comments :

  1. This way my associate Wesley Virgin's autobiography begins with this shocking and controversial VIDEO.

    You see, Wesley was in the military-and soon after leaving-he found hidden, "self mind control" secrets that the government and others used to get whatever they want.

    THESE are the exact same methods many famous people (notably those who "became famous out of nothing") and the greatest business people used to become rich and successful.

    You've heard that you use only 10% of your brain.

    Mostly, that's because most of your BRAINPOWER is UNTAPPED.

    Perhaps this expression has even occurred INSIDE your own mind... as it did in my good friend Wesley Virgin's mind seven years back, while driving an unregistered, trash bucket of a vehicle with a suspended license and with $3 in his bank account.

    "I'm very fed up with living paycheck to paycheck! Why can't I become successful?"

    You took part in those types of thoughts, isn't it right?

    Your own success story is waiting to start. You need to start believing in YOURSELF.

    Take Action Now!

    ReplyDelete
  2. Water Hack Burns 2 lb of Fat OVERNIGHT

    Well over 160 thousand men and women are using a easy and SECRET "water hack" to lose 1-2lbs every night in their sleep.

    It is simple and works on anybody.

    This is how to do it yourself:

    1) Get a drinking glass and fill it half the way

    2) And then learn this awesome HACK

    you'll be 1-2lbs lighter the next day!

    ReplyDelete

Terimakasih sudah berkomentar