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.
from https://github.com/kamijin-fanta/prrn/releases
docker pull docker.pkg.github.com/kamijin-fanta/prrn/prrn
go install github.com/kamijin-fanta/prrn
see ./example dir.
$ 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.
-- schema/main.sql
create table article (
id bigint not null auto_increment primary key,
content text not null
);
$ 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;
-- 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
);
$ 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;
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.