Simple SQL migration.

0.3.7 2019-02-27 09:14 UTC

This package is auto-updated.

Last update: 2024-03-28 18:30:17 UTC


README

Mig is a simple migration tool for SQL scripts, created by php.

It was tested by only MySQL.

Quickstart

Install mig by composer global

$ composer global require arakaki-yuji/mig

add execution path for composer to $PATH.

$ export PATH=$PATH:$HOME/.composer/vendor/bin

Set mig.config.php to project root directory.

<?php

return [
    'db_dsn' => '', // example: mysql:host=localhost:3306;dbname=project_db;
    'db_username' => '',
    'db_passwd' => '', 
    'migration_filepath' => 'migrations' // directory for place SQL scripts.
];

Run init command. it create migrations table.

$ mig-cli init

Create SQL scripts for migration.

$ mig-cli create create_user_table 
Create a new migration file.
=================

create migrations/20180907025457_create_user_table.up.sql
create migrations/20180907025457_create_user_table.down.sql

Add SQL scripts like the below to migrations/20180907025457_create_user_table.up.sql.

CREATE TABLE IF NOT EXISTS user(id BIGINT);

Add SQL scripts like the below to migrations/20180907025457_create_user_table.down.sql.

DROP TABLE IF EXISTS user;

Apply pending migrations.

$ mig-cli migrate 
Start migration.
=================

Migrate migrations/20180907025457_create_user_table.up.sql

Rollback the latest migration applied.

$ mig-cli rollback
Rollback a migration.
======================

Rollback migrations/20180907025457_create_user_table.down.sql

Set config values by environment values

if you want to set config values by environment values, you can do it.

$ MIG_DB_DSN="mysql:host=127.0.0.1:3306;dbname=migrationdbname;" MIG_DB_USERNAME="admin" MIG_DB_PASSWD="password" MIG_MIGRATION_FILEPATH="migrations" mig-cli migrate
Start migration.
=================

Migrate migrations/20180907025457_create_user_table.up.sql