Skip to content

kamijin-fanta/prrn

Repository files navigation

prrn

Simple migration tool for MySQL

This is a CLI that helps you create a DB migration file. There is no need to write up and down files from scratch anymore.

Github issues Github forks Github stars Github top language Github license

Installation

Download binary

from https://github.com/kamijin-fanta/prrn/releases

Docker Image

docker pull docker.pkg.github.com/kamijin-fanta/prrn/prrn

Via go cli

go install github.com/kamijin-fanta/prrn

Minimal Example

see ./example dir.

1. Initialize project

$ prrn init
$ tree
.
└── schema       
    ├── main.sql    # An SQL file that defines a declarative schema, describing CREATE TABLE, etc.
    ├── histories   # The main.sql file is copied when the migration is created.
    └── migrations  # Up and down files that can be executed by the migration tool.

2. Put schema

-- schema/main.sql
create table article (
  id bigint not null auto_increment primary key,
  content text not null
);

3. Make first migration

$ prrn make --name=init
$ cat schema/migrations/000001_init.sql 
-- +migrate Up
SET FOREIGN_KEY_CHECKS = 0;
CREATE TABLE `article` (
`id` BIGINT (20) NOT NULL AUTO_INCREMENT,
`content` TEXT NOT NULL,
PRIMARY KEY (`id`)
);
SET FOREIGN_KEY_CHECKS = 1;


-- +migrate Down
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE `article`;
SET FOREIGN_KEY_CHECKS = 1;

4. Edit schema

-- schema/main.sql
create table article (
  id bigint not null auto_increment primary key,
  content text not null,
  original_url varchar(255) not null,
  created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);

5. Make second migration

$ prrn make --name=init
$ cat schema/migrations/000002_add-article-fileds.sql 
-- +migrate Up
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE `article` ADD COLUMN `original_url` VARCHAR (255) NOT NULL AFTER `content`;
ALTER TABLE `article` ADD COLUMN `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `original_url`;
SET FOREIGN_KEY_CHECKS = 1;


-- +migrate Down
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE `article` DROP COLUMN `original_url`;
ALTER TABLE `article` DROP COLUMN `created_at`;
SET FOREIGN_KEY_CHECKS = 1;

6. Exec migration

Run the migration with the tool of your choice.

recommended migrate tool: https://github.com/rubenv/sql-migrate

⚠ Be sure to check the content of the migration to be performed.