Advertisement

go 数据库迁移工具sql-migrate

阅读量:

migrate 软件

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

下载安装

复制代码
    go get -v github.com/rubenv/sql-migrate/...
    
    
      
    
    AI写代码

tutorial: [\text{https://github.com/rubenv/sql-migrate}]

使用

sql-migrate 命令

复制代码
    \$ sql-migrate --help
    usage: sql-migrate [--version] [--help] <command> [<args>]
    
    Available commands are:
    down      Undo a database migration
    new       Create a new migration
    redo      Reapply the last migration
    status    Show migration status
    up        Migrates the database to the most recent version available
    
    
      
      
      
      
      
      
      
      
      
    
    AI写代码

sql-migrate up --help

复制代码
    \$ sql-migrate up --help
    Usage: sql-migrate up [options] ...
    
      Migrates the database to the most recent version available.
    
    Options:
    
      -config=dbconfig.yml   Configuration file to use.
      -env="development"     Environment.
      -limit=0               Limit the number of migrations (0 = unlimited).
      -dryrun                Don't apply migrations, just print them.
    
    
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

demo

migrations/mysql/test.sql

复制代码
    -- +migrate Up
    create table student (
    id int(10) primary key,
    name varchar(100) unique not null,
    description text not null
    );
    
    CREATE TABLE people (id int);
    
    -- +migrate Down
    drop table student;
    drop table people;
    
    
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

test.yml

复制代码
    development:
      dialect: 'mysql'
      datasource: 'root@tcp(127.0.0.1:3306)/kkk?parseTime=true'
      dir: 'migrations/mysql'
    
    
    
      
      
      
      
      
    
    AI写代码
复制代码
    执行命令:
    sql-migrate up -config=test.yml -env="development"
    撤销命令:
    sql-migrate down -config=test.yml -env="development"
    
    
      
      
      
      
    
    AI写代码

全部评论 (0)

还没有任何评论哟~