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

  • 相关阅读:
    Goroutine被动调度之一(18)
    实战分析一个运行起来会卡死的Go程序
    Go语言调度器之盗取goroutine(17)
    第三章 Goroutine调度策略(16)
    非main goroutine的退出及调度循环(15)
    Go语言调度器之调度main goroutine(14)
    PHP经典面试题之 Redis 内存满了怎么办?
    【PHP】让新人快速理解ThinkPHP6中的事务操作
    面试官:说说swoole+PHP实现自动取消订单,还原库存等操作
    最新整理的PHP高级面试题来啦!【附答案】
  • 原文地址:https://www.cnblogs.com/jimboi/p/8093043.html
Copyright © 2011-2022 走看看