• Liquibase 快速开始


    Step 1 :创建Changelog文件,所有的数据库变动都会保存在Changelog文件中

    <?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>

    Step 2:添加数据库变动(对应于Changelog文件中的changeSet元素)

        每一个ChangeSet元素都是通过id,author,文件名,包全限定名来唯一标识。如果只指定了id属性,那么在有多个人协同开发并且存在多个分支的时候很容易造成ChangeSet重复。

        将每一个changeSet都看做你想要应用到数据库的一个原子操作,通常在一个ChangeSet中最好只包含一个变化,但是如果你想在一个事务中插入多条记录,将它们放在一个ChangeSet中效果会非常好。Liquibase会尝试将每个changeSet放在一个事务中执行,但是很多数据库可能会静默的提交事务,或者为某些命令如(create table,drop table,等)开启新的事物。

    <?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>

    Step 3:运行ChangeSet

      有很多方式都可以运行change log 包括: via command lineAntMavenSpring, a servlet listener, and a CDI Environment.

      一个mysql通过jdbc运行的例子:

    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

    Step 4:检查你的数据库

      你会发现你的数据库中多了一个"department"表。而且还创建了两张其它的表“databasechangelog” 和 “databasechangeloglock”。“databasechangelog”表包含了在这个数据库中已经执行的所有的语法的列表。“databasechangeloglock”用了确保两个机器不会尝试在同一时刻修改数据库。

  • 相关阅读:
    解决IDEA中项目出现cannot resolve method ‘XXXXX(java.lang.String)’问题
    JDK1.8下载、安装和环境配置教程
    JavaBean是什么,POJO是什么
    什么是MVC
    IDEA使用-test下没有resource文件
    Hive入门--3.UDF编写与使用
    Hive入门--2.分区表 外部分区表 关联查询
    Hive入门--1.简介与环境搭建
    SLF4J-jar包多绑定冲突解决
    Hbase--1 简介
  • 原文地址:https://www.cnblogs.com/jimboi/p/8093043.html
Copyright © 2020-2023  润新知