本頁目錄

LiquiBase 快速開始

第1步:創造一個 Changelog 檔

資料庫 changelog 檔案 是列出所有數據庫變動的地方.它是以 XML 為基礎的,所以我們從一個空的 XML 檔開始:

<?xml version="1.0" encoding="UTF-8"?>
 
<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.6"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.6
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.6.xsd">
 
</databaseChangeLog>

第2步:增加一個 ChangeSet

每個 change set 是用一個 “id” 屬性, 以及一個 “author” 屬性, 獨一無二地來區別. 而這兩個標記,再搭配上 changelog 檔案的檔名(與 package 路徑), 則用來獨一無二地區別該項異動[change]. 如果只有用一個 “id” 來指定的話, 那將會太容易就意外地用到重複的 “id”, 尤其是當面臨多個開發者以及 code branches 的情況. 多指定一個 “author” 屬性能有所幫助,讓發生重複的機會變到最小.

Think of each change set as an atomic change that you want to apply to your database. It's usually best to include just one change in your change set, but more are allowed and can make sense if you are insert multiple rows that should be added as a single transaction. LiquiBase will attempt to run each change set as a single transaction, but many databases will silently commit and resume transactions for certain commands (create table, drop table, etc.)

<?xml version="1.0" encoding="UTF-8"?>
 
<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.6"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.6
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.6.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" defaultValue="1"/>
        </createTable>
    </changeSet>
 
</databaseChangeLog>

Step 3: Run the ChangeSet

There are many ways to execute your change log including via command line, Ant, Maven, Grails, and a servlet listener.

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: Check Your Database

You will see that your database now contains a table called “department”. Two other tables are created as well: “databasechangelog” and “databasechangeloglock”. The databasechangelog table contains a list of all the statements that have been run against the database. The databasechangeloglock table is used to make sure two machines don't attempt to modify the database at the same time.

Next Steps

This quick-start guide is designed to get you started with LiquiBase. For a full description of all its capabilities, see the LiquiBase Manual. You can also view recorded sessions on LiquiBase or visit the mailing list.


(translated by 季斯遠)