Golang utility for managing sql migrations using.
The package includes the following drivers: postgres
, mysql
, sqlite3
.
go get -u github.com/malyg1n/sql-migrator
Create a file .env
by copying from .env.example
and specify your database settings.
In the same file, specify the folder for migrations (migrations
in root of project by default).
After that, tou need to run the command:
sql-migrator init
This command creates table and directory (if not exists) to store your migrations.
At the root of the project, you need to run a command with the following signature:
sql-migrator create migration-name
Like this:
sql-migrator create create-users-table
After that, two files will appear in the migrations' folder at the root of the project.
00001-create-users-table-up.sql
00001-create-users-table-down.sql
They need to write SQL code for rolling and rolling back migration, respectively.
See migrations' examples here.
To roll out migrations, use the command:
sql-migrator up
And to rollback:
sql-migrator down
Complete cleaning of all migrations and roll out them over again:
sql-migrator refresh
Complete cleaning of all migrations:
sql-migrator clean