zoukankan      html  css  js  c++  java
  • 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”用了确保两个机器不会尝试在同一时刻修改数据库。

  • 相关阅读:
    Java设计模式之依赖倒置原则
    windows 下安装apache 遇到的问题
    Java序列化相关
    批量插入————优化
    接口相关
    Redis使用及工具类
    面试回顾——kafka
    面试回顾——List<T>排序
    Java面试——线程池
    面试回顾——session相关
  • 原文地址:https://www.cnblogs.com/jimboi/p/8093043.html
Copyright © 2011-2022 走看看