数据库变更管理工具

数据库变更管理是数据库生命周期的重要环节,本文介绍了Flyway、LiquiBase、DBDeploy、MyBatis Migration和DBMaintain等工具,涵盖了它们的运行模式、变更方式及使用步骤,帮助你更好地理解和使用这些工具。

数据库变更也叫模式迁移,是管理数据库生命周期的过程。本文列举了几个数据库变更管理工具,后续持续更新完善。主要包括Flyway、LiquiBase、MyBatis migration、DBDeploy、DBMaintain。更多关于模式迁移与其他工具,参考schema migration的wiki

Flyway

Flyway是一个开源的数据库迁移工具。支持命令行以及JAVA方式运行。支持众多数据库。

官网: https://flywaydb.org/

运行模式

命令行
API(Java/Android)
Maven
Gradle
SBT
Ant

基本命令

Migrate, Clean, Info, Validate, Baseline and Repair

变更方式

SQL: SQL脚本的形式描述变更
JAVA: 使用JVM应用实现数据库迁移(例如操作BLOB)

LiquiBase

LiquiBase是一个强大的数据库变更维护工具,通过change Log, change set,changes,precondition, context等概念,提供强大的变更执行、版本控制、集成等功能。

官网: http://www.liquibase.org/
文档:http://www.liquibase.org/documentation/index.html

使用步骤:

1)创建changelog文件:XML JSON YAML 或者SQL格式
change log文件描述数据库的变更内容,下面是一个空的change log:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

</databaseChangeLog>

2)添加changeset到changelog文件
change set是liquibase执行数据库变更的基本单元,是事务执行单元。但是像create,drop这样的语句,有些数据库是直接提交事务并恢复事务,客户端不可知。change set一般封装一个原子的数据库变更。

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

    <changeSet id="1" author="bob">
        <createTable tableName="department">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="active" type="boolean" defaultValueBoolean="true"/>
        </createTable>
    </changeSet>

</databaseChangeLog>

3)执行changelog
有多种方式可以执行change log:
命令行、Ant、Maven、Spring、Servelt Listener、CDI环境。
例如,使用命令行执行变更的一个例子:

liquibase --driver=com.mysql.jdbc.Driver \
     --classpath=/path/to/classes \
     --changeLogFile=com/example/db.changelog.xml \
     --url="jdbc:mysql://localhost/example" \
     --username=user \
     --password=asdf \
     migrate

changlog格式

xml
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <preConditions>
        <runningAs username="liquibase"/>
    </preConditions>

    <changeSet id="1" author="nvoxland">
        <createTable tableName="person">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="firstname" type="varchar(50)"/>
            <column name="lastname" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="state" type="char(2)"/>
        </createTable>
    </changeSet>

    <changeSet id="2" author="nvoxland">
        <addColumn tableName="person">
            <column name="username" type="varchar(8)"/>
        </addColumn>
    </changeSet>
    <changeSet id="3" author="nvoxland">
        <addLookupTable
            existingTableName="person" existingColumnName="state"
            newTableName="state" newColumnName="id" newColumnDataType="char(2)"/>
    </changeSet>

</databaseChangeLog>
JSON
{
    "databaseChangeLog": [
        {
            "preConditions": [
                {
                    "runningAs": {
                        "username": "liquibase"
                    }
                }
            ]
        },

        {
            "changeSet": {
                "id": "1",
                "author": "nvoxland",
                "changes": [
                    {
                        "createTable": {
                            "tableName": "person",
                            "columns": [ { "column": { "name": "id", "type": "int", "autoIncrement": true, "constraints": { "primaryKey": true, "nullable": false }, } }, { "column": { "name": "firstname", "type": "varchar(50)" } }, { "column": { "name": "lastname", "type": "varchar(50)" "constraints": { "nullable": false }, } }, { "column": { "name": "state", "type": "char(2)" } } ] }
                    }
                ]
            }
        },

        {
            "changeSet": {
                "id": "2",
                "author": "nvoxland",
                "changes": [
                    {
                        "addColumn": {
                            "tableName": "person",
                            "columns": [ { "column": { "name": "username", "type": "varchar(8)" } } ] }
                    }
                ]
            }
        },

        {
            "changeSet": {
                "id": "3",
                "author": "nvoxland",
                "changes": [
                    {
                        "addLookupTable": {
                            "tableName": "person",
                            "existingTableName": "person",
                            "existingColumnName":"state",
                            "newTableName": "state",
                            "newColumnName": "id",
                            "newColumnDataType": "char(2)",
                        }
                    }
                ]
            }
        }
    ]
}
YAML
databaseChangeLog:
  - preConditions:
    - runningAs:
        username: liquibase

  - changeSet:
      id: 1
      author: nvoxland
      changes:
        - createTable:
            tableName: person
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: firstname
                  type: varchar(50)
              - column:
                  name: lastname
                  type: varchar(50)
                  constraints:
                    nullable: false
              - column:
                  name: state
                  type: char(2)

  - changeSet:
      id: 2
      author: nvoxland
      changes:
        - addColumn:
            tableName: person
            columns:
              - column:
                  name: username
                  type: varchar(8)

  - changeSet:
      id: 3
      author: nvoxland
      changes:
        - addLookupTable:
            existingTableName: person
            existingColumnName:state
            newTableName: state
            newColumnName: id
            newColumnDataType: char(2)
SQL
--liquibase formatted sql

--changeset nvoxland:1
create table person (
  id int not null primary key,
  firstname varchar(80),
  lastname varchar(80) not null,
  state varchar(2)
);

--changeset nvoxland:2
alter table person add column username varchar(8)

--changeset nvoxland:3
create table state AS SELECT DISTINCT state AS id FROM person WHERE state IS NOT NULL;
alter table state modify id char(2) NOT NULL;
alter table state add primary key(id);
alter table person add constraint fk_person_state foreign key (state) references state(id);

DBDeploy

基于数据库脚本(delta script)对数据库进行版本管理,是一个数据库变更管理工具。
但是目前维护很不活跃。

入门教程: https://code.google.com/archive/p/dbdeploy/wikis/GettingStarted.wiki

MyBatis Migration

MyBatis提供的一个数据库迁移(migration)工具,主要是以命令行作为工具。
提供了pre/post-migration hook

官网: http://www.mybatis.org/migrations/

常见命令

init
bootstrap
new
status
up,down
version
pending
script

DBMaintain

数据库脚本版本管理工具,也就是数据库版本管理工具。像管理源代码一样管理数据库脚本。
官网: http://www.dbmaintain.org/overview.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值