整理了Go制作的迁移工具

相对比较

    • CLIで使える

 

    MySQLが使える
名前Star数スキーマ方式Dry Run設定liamstask/goose-Go/SQLUp/Down-YAMLmattes/migrate892SQLUp/Down–rubenv/sql-migrate576SQLUp/Down✔YAMLelwinar/rambler371SQLUp/Down-JSONnaoina/migu29Go(struct)Sync✔-dev-cloverlab/carpenter12JSON/実DBSync✔-

2017年1月的时候

本次比较没有考虑的内容。

    • https://github.com/tanel/dbmigrate

 

    • https://github.com/BurntSushi/migration

 

    • https://github.com/Boostport/migration

 

    • https://github.com/github/gh-ost

 

    • https://github.com/wawandco/transporter

 

    • https://github.com/GuiaBolso/darwin

 

    • https://github.com/DavidHuie/gomigrate

 

    • https://github.com/go-gormigrate/gormigrate

 

    • https://github.com/pravasan/pravasan

 

    https://github.com/eure/kamimai

liamstask/goose => liamstask/goose

处理数据库

    • MySQL

 

    • PostgreSQL

 

    SQLite3

安装

go get bitbucket.org/liamstask/goose/cmd/goose

设定

development:
  driver: mysql
  open: user=root dbname=goose_test

杂感

    • 普通な感じ

 

    • 使ってる人多そう

 

    適用履歴がテーブルに残るのは良い

迁移文件

$ goose create CreatePosts sql
goose: created /Users/nownabe/src/github.com/nownabe/research-go/migration-tools/goose/db/migrations/20170111152547_CreatePosts.sql

使用CLI创建模板并将Up/Down的SQL写入到文件中。

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE `posts` (
  `post_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `author` VARCHAR(255) NOT NULL,
  `text` TEXT NOT NULL
) ENGINE = InnoDB;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE `posts`;

虽然Go语言也可以编写迁移文件,但由于似乎不太需要,所以省略。

命令

$ goose
goose is a database migration management system for Go projects.

Usage:
    goose [options] <subcommand> [subcommand options]

Options:
  -env string
        which DB environment to use (default "development")
  -path string
        folder containing db info (default "db")
  -pgschema string
        which postgres-schema to migrate (default = none)

Commands:
    up         Migrate the DB to the most recent version available
    down       Roll back the version by 1
    redo       Re-run the latest migration
    status     dump the migration status for the current DB
    create     Create the scaffolding for a new migration
    dbversion  Print the current version of the database

版本管理

保存在名为goose_db_version的表中。

> select * from goose_db_version;
+----+----------------+------------+---------------------+
| id | version_id     | is_applied | tstamp              |
+----+----------------+------------+---------------------+
|  1 |              0 |          1 | 2017-01-11 15:35:03 |
|  2 | 20170111152547 |          1 | 2017-01-11 15:47:34 |
|  3 | 20170111152547 |          0 | 2017-01-11 15:47:36 |
|  4 | 20170111152547 |          1 | 2017-01-11 15:47:36 |
|  5 | 20170111152547 |          0 | 2017-01-11 15:47:53 |
|  6 | 20170111152547 |          1 | 2017-01-11 15:48:06 |
+----+----------------+------------+---------------------+
6 rows in set (0.00 sec)

[粤语] 迁移/迁徙

处理数据库

    • Cassandra

 

    • MongoDB

 

    • MySQL (experimental)

 

    • PostgreSQL

 

    • SQLite

 

    Ql

安装

go get -u github.com/mattes/migrate

感想

    • コマンドが豊富

 

    CLIの結果が色付きで見やすい
スクリーンショット 2017-01-11 16.39.19.png (43.4 kB)

迁移文件

$ migrate -url="mysql://root@tcp(127.0.0.1:3306)/migrate_test" create create_posts
Version 1484119339 migration files created in /Users/nownabe/src/github.com/nownabe/research-go/migration-tools/goose:
1484119339_create_posts.up.sql
1484119339_create_posts.down.sql

在CLI中,将会创建上升/下降两种模板文件。

CREATE TABLE `posts` (
  `post_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `author` VARCHAR(255) NOT NULL,
  `text` TEXT NOT NULL
) ENGINE = InnoDB;
DROP TABLE `posts`;

命令

$ migrate
usage: migrate [-path=<path>] -url=<url> <command> [<args>]

Commands:
   create <name>  Create a new migration
   up             Apply all -up- migrations
   down           Apply all -down- migrations
   reset          Down followed by Up
   redo           Roll back most recent migration, then apply it again
   version        Show current migration version
   migrate <n>    Apply migrations -n|+n
   goto <v>       Migrate to version v
   help           Show this help

'-path' defaults to current working directory.

版本控制

将其保存在名为”schema_migrations”的表中。

> select * from schema_migrations;
+------------+
| version    |
+------------+
| 1484119339 |
+------------+
1 row in set (0.00 sec)

rubenv/sql-migrate 可以将数据库迁移工作自动化。

数据库处理

    • MySQL

 

    • MSSQL

 

    • Oracle Database

 

    • PostgreSQL

 

    SQLite

安装

go get github.com/rubenv/sql-migrate/...

设定

development:
  dialect: mysql
  datasource: root:@tcp(127.0.0.1:3306)/sqlmigrate_test?parseTime=true
  dir: migrations/mysql # ディレクトリを作っておく必要がある

杂感

    対応RDBMSが多い

迁移文件

没有雏形制作功能。

-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE `posts` (
  `post_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `author` VARCHAR(255) NOT NULL,
  `text` TEXT NOT NULL
) ENGINE = InnoDB;

-- +migrate Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE `posts`;

Command. (命令)

$ sql-migrate
usage: sql-migrate [--version] [--help] <command> [<args>]

Available commands are:
    down      Undo a database migration
    redo      Reapply the last migration
    status    Show migration status
    up        Migrates the database to the most recent version available

版本管理

默认情况下,它将保存在名为gorp_migrations的表中。

> select * from gorp_migrations;
+--------------------+---------------------+
| id                 | applied_at          |
+--------------------+---------------------+
| 1_create_posts.sql | 2017-01-11 08:12:18 |
+--------------------+---------------------+
1 row in set (0.00 sec)

埃尔维纳/漫步者

数据库适配

    • MySQL

 

    • PostgreSQL

 

    SQLite

安装

go get github.com/elwinar/rambler

设定

{
  "driver": "mysql",
  "protocol": "tcp",
  "host": "localhost",
  "port": 3306,
  "user": "root",
  "password": "",
  "database": "rambler_test",
  "directory": ".",
  "table": "migrations"
}

杂感

    • 設定ファイルがJSONで良い

 

    ログがとても見にくい

迁移文件

没有创建原型功能。文件将以version_description.sql的命名规则创建。

-- rambler up

CREATE TABLE `posts` (
  `post_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `author` VARCHAR(255) NOT NULL,
  `text` TEXT NOT NULL
) ENGINE = InnoDB;

-- rambler down

DROP TABLE `posts`;

指令

$ rambler
11:38 debug loading configuration from%!(EXTRA string=)unable to load configuration from file: open : no such file or directory

NAME:
   rambler - Migrate all the things!

USAGE:
   rambler [global options] command [command options] [arguments...]

AUTHOR:
   Romain Baugue <romain.baugue@elwinar.com>

COMMANDS:
     apply    apply the next migration
     reverse  reverse the last migration
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --configuration value, -c value  path to the configuration file
   --environment value, -e value    set the working environment (default: "default")
   --debug                          display debug messages
   --help, -h                       show help
   --version, -v                    print the version
unable to load configuration from file: open : no such file or directory

版本管理

> select * from migrations;
+---------------------+
| migration           |
+---------------------+
| 01_create_posts.sql |
+---------------------+
1 row in set (0.00 sec)

闹哪儿/米谷

数据库管理

    MySQL

安装

go get -u github.com/naoina/migu/cmd/migu

杂感

    • 細かいことを気にしなければ楽ちん

 

    融通はきかなそう

迁移文件

我要写一个Go的struct。

package main

type Post struct {
        PostId int `migu:"pk,autoincrement"`
        Author string
        Text   string `migu:"size:65535"`
}

移民

以这种方式从struct生成模式并执行给我。

$ migu sync -u root -h 127.0.0.1:3306 migu_test schema.go
--------applying--------
  CREATE TABLE `post` (
    `post_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `author` VARCHAR(255) NOT NULL,
    `text` MEDIUMTEXT NOT NULL
  )
--------done 0.019s--------

命令

$ migu
migu: too few arguments
Usage: migu [OPTIONS] COMMAND [ARG...]

Commands:
  sync      synchronize the database schema
  dump      dump the database schema as Go code

Options:
      --help             Display this help and exit

$ migu sync
migu: too few arguments
Usage: migu sync [OPTIONS] DATABASE [FILE]

Options:
      --dry-run          Print the results with no changes
  -q, --quiet            Suppress non-error messages
  -u, --user=NAME        User for login to database if not current user
  -h, --host=HOST        Connect to host of database
  -u, --user=NAME        User for login to database if not current user
  -p, --password[=PASS]  Password to use when connecting to server.
                         If password is not given, it's asked from the tty
      --help             Display this help and exit

With no FILE, or when FILE is -, read standard input.

木匠/dev-cloverlab

对应的数据库

    MySQL

安装

go get github.com/dev-cloverlab/carpenter/cmd/carpenter

混乱的情绪

    面白い

翻译:模式导出

木工师可以使用特殊工具来进行以下的两种操作。

    • 実DBのスキーマをJSONへエクスポート

 

    JSONのスキーマによるマイグレーション

以JSON为基础的模式相当复杂,似乎不适合由人来编写。

导出可以使用如下命令完成。

carpenter --schema goose_test --data-source 'root:@tcp(127.0.0.1:3306)' design

将创建一个名为 tables.json 的文件。

jq "." < tables.json | wc -l
     248
$ jq "." < tables.json | head -30
[
  {
    "TableCatalog": "def",
    "TableSchema": "goose_test",
    "TableName": "goose_db_version",
    "TableType": "BASE TABLE",
    "Engine": "InnoDB",
    "Version": 10,
    "RowFormat": "Compact",
    "TableRows": 14,
    "AvgRowLength": 1170,
    "DataLength": 16384,
    "MaxDataLength": 0,
    "IndexLength": 16384,
    "DataFree": 0,
    "AutoIncrement": 17,
    "TableCollation": "utf8_general_ci",
    "CheckSum": null,
    "CreateOptions": "",
    "TableComment": "",
    "Columns": [
      {
        "TableCatalog": "def",
        "TableSchema": "goose_test",
        "TableName": "goose_db_version",
        "ColumnName": "id",
        "OrdinalPosition": 1,
        "ColumnDefault": null,
        "Nullable": "NO",
        "DataType": "bigint",

移民

如果有JSON,可以根据它生成表格。

carpenter --schema carpenter_test --data-source 'root:@tcp(127.0.0.1:3306)' build --dir .

如果表已经存在并且模式发生了变化,它似乎会进行相应的调整。

命令

$ carpenter
NAME:
   carpenter - Carpenter is a tool to manage DB schema and data

USAGE:
   carpenter [global options] command [command options] [arguments...]

VERSION:
   0.3.1

AUTHOR:
   hatajoe <hatanaka@cloverlab.jp>

COMMANDS:
     design   Export table structure as JSON string
     build    Build(Migrate) table from specified JSON string
     import   Import CSV to table
     export   Export CSV to table
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --verbose, --vv                show verbose output (default off)
   --dry-run                      execute as dry-run mode (default off)
   --schema value, -s value       database name (required)
   --data-source value, -d value  data source name like '[username[:password]@][tcp[(address:port)]]' (required)
   --help, -h                     show help
   --version, -v                  print the version

似乎还可以使用CSV进行导入/导出。

请参阅

    • DBマイグレーションツール作った話

 

    Go製マイグレーションツールの現状確認
广告
将在 10 秒后关闭
bannerAds