databaseChangeLog
屬性
屬性 | 描述 |
---|---|
logicalFilePath | 路徑+文件名進行唯一標識,當重命名文件或者重構文件路徑時可通過該屬性經行唯一標識 |
子標簽
preConditions
用途:
- 記錄執行的先決條件或者注釋
- 測試執行該databaseChangeLog的先決條件是否完備
- 執行數據檢查
- 根據條件控制執行哪些changesets
屬性:
屬性 | 描述 | |
---|---|---|
onFail | 不滿足測試條件,執行以下選項:HALT/CONTINUE/MARK_RAN/WARN | |
onError | 執行過程中出現錯誤,執行以下選項:HALT/CONTINUE/MARK_RAN/WARN | |
onUpdateSQL | RUN/FAIL/IGNORE | |
onFailMessage | 失敗后輸出的信息 | |
onErrorMessage | 拋錯后輸出的信息 |
詳細用法示例如下:
databaseChangeLog(logicalFilePath: '') {
preConditions(onFail: 'WARN') {
and {
dbms(type: 'mysql')
runningAs(username: 'root')
or {
changeSetExecuted(id: '', author: '', changeLogFile: '')
columnExists(schemaName: '', tableName: '', columnName: '')
tableExists(schemaName: '', tableName: '')
viewExists(schemaName: '', viewName: '')
foreignKeyConstraintExists(schemaName: '', foreignKeyName: '')
indexExists(schemaName: '', indexName: '')
sequenceExists(schemaName: '', sequenceName: '')
primaryKeyExists(schemaName: '', primaryKeyName: '', tableName: '')
sqlCheck(expectedResult: '') {
"SELECT COUNT(1) FROM monkey WHERE status='angry'"
}
customPrecondition(className: '') {
tableName('our_table')
count(42)
}
}
}
}
}
property
可以自定義一些參數:
databaseChangeLog(logicalFilePath: '') {
clobType = 0
}
changeSet
屬性 | 描述 |
---|---|
id | 必輸 ,一般以時間戳+名稱作為唯一標識 |
author | 必輸 ,創建者 |
dbms | 指定數據庫類型 |
runAlways | 該changeSet不論之前是否執行過,每次都會執行 true/false |
runOnChange | 在第一次或者修改后執行 true/false |
context | 特定的上下文通過后執行 |
runInTransaction | 執行是否事務化,默認為true,大部分情況下都為true |
failOnError | 是否忽略錯誤繼續執行 |
屬性:
屬性 | 描述 |
---|---|
id | 必輸 ,一般以時間戳+名稱作為唯一標識 |
author | 必輸 ,創建者 |
dbms | 指定數據庫類型 |
runAlways | 該changeSet不論之前是否執行過,每次都會執行 true/false |
runOnChange | 在第一次或者修改后執行 true/false |
context | 特定的上下文通過后執行 |
runInTransaction | 執行是否事務化,默認為true,大部分情況下都為true |
failOnError | 是否忽略錯誤繼續執行 |
子標簽:
- comment: changeSet的描述
- preConditions:必須通過后才能執行該changeSet,主要用于一些不可恢復操作之前的數據檢查
- <Any Refactoring Tag(s)> :具體的數據、表操作
- validCheckSum:一般不用,使用來區分數據庫中儲存的changeSet與該changeSet是否一致,一般會自動生成。
- rollback:會滾操作
具體詳細用例如下:
- rollback
databaseChangeLog(logicalFilePath: '') {
//定義參數
clobType = 0
//rollback的用法
changeSet(id: '', author: '', dbms: '', runAlways: true, runOnChange: false, context: '', runInTransaction: true, failOnError: false) {
// 添加描述
comment "Liquibase can be aware of this comment"
preConditions {
// 與changeLog的preConditions一致
}
validCheckSum 'd0763edaa9d9bd2a9516280e9044d885'
// rollback可以直接是一個string,會當作SQL直接執行
rollback "DROP TABLE monkey_table"
rollback """
UPDATE monkey_table SET emotion='angry' WHERE status='PENDING';
ALTER TABLE monkey_table DROP COLUMN angry;
"""
// 也可以是liquibase腳本
rollback {
dropTable(tableName: 'monkey_table')
}
// 也可以指定執行某個changeSet
rollback(changeSetId: '', changeSetAuthor: '')
}
- addColumn語法:
changeSet(id: 'add-column', author: 'tlberglund') {
addColumn(tableName: '', schemaName: '') {
column(name: '', type: '', value: '', defaultValue: '', autoIncrement: false, remarks: '') {
// 約束
// 寫法一
constraints {
nullable(false)//是否必輸
primaryKey(true)//主鍵約束
unique(true)//唯一約束
uniqueConstraintName('make_it_unique_yo')//唯一約束名稱
foreignKeyName('key_to_monkey')//外鍵約束名稱
references('monkey_table')//外鍵約束
deleteCascade(true)//是否級聯刪除
deferrable(true)//約束驗證是否可延時(事務中或者事務完成后)
initiallyDeferred(false)//事務結束的時候才去檢查約束
}
// 寫法二,推薦使用寫法二,可讀性較好
constraints(nullable: false, primaryKey: true)
constraints(unique: true, uniqueConstraintName: 'make_it_unique_yo')
constraints(foreignKeyName: 'key_to_monkey', references: 'monkey_table')
constraints(deleteCascase: true)
constraints(deferrable: true, initiallyDeferred: false)
}
// 列的其他屬性
column(name: '', type: '', valueNumeric: '', defaultValueNumeric: '')
column(name: '', type: '', valueBoolean: '', defaultValueBoolean: '')
column(name: '', type: '', valueDate: '', defaultValueDate: '')
}
}
- 列column操作:
// renameColumn
changeSet(id: 'rename-column', author: 'tlberglund') {
renameColumn(schemaName: '', tableName: '', oldColumnName: '', newColumnName: '', columnDataType: '')
}
// modifyColumn
changeSet(id: 'modify-column', author: 'tlberglund') {
modifyColumn(schemaName: '', tableName: '') {
column() { }
}
}
// dropColumn
changeSet(id: 'drop-column', author: 'tlberglund') {
dropColumn(schemaName: '', tableName: '', columnName: '')
}
- 自增長列
changeSet(id: 'alter-sequence', author: 'tlberglund') {
alterSequence(sequenceName: '', incrementBy: '')
}
- 表table操作:
// createTable
changeSet(id: 'create-table', author: 'tlberglund') {
createTable(schemaName: '', tablespace: '', tableName: '', remarks: '') {
column() {}
column() {}
column() {}
column() {}
}
}
// renameTable
changeSet(id: 'rename-table', author: 'tlberglund') {
renameTable(schemaName: '', oldTableName: '', newTableName: '')
}
// dropTab
changeSet(id: 'drop-table', author: 'tlberglund') {
dropTab(schemaName: '', tableName: '')
}
- 視圖view相關操作:
changeSet(id: 'create-view', author: 'tlberglund') {
createView:(schemaName: '', viewName: '', replaceIfExists: true) {
"SELECT id, emotion FROM monkey"
}
}
changeSet(id: 'rename-view', author: 'tlberglund') {
renameView(schemaName: '', oldViewName: '', newViewName: '')
}
changeSet(id: 'drop-view', author: 'tlberglund') {
dropView(schemaName: '', viewName: '')
}
- 列合并:
changeSet(id: 'merge-columns', author: 'tlberglund') {
mergeColumns(schemaName: '', tableName: '', column1Name: '', column2Name: '', finalColumnName: '', finalColumnType: '', joinString: ' ')
}
- 存儲過程
changeSet(id: 'create-stored-proc', author: 'tlberglund') {
createStoredProcedure """
CREATE OR REPLACE PROCEDURE testMonkey
IS
BEGIN
-- do something with the monkey
END;
"""
}
- 單獨列操作,如約束、序列、默認值等等操作:
changeSet(id: 'add-not-null-constraint', author: 'tlberglund') {
addNotNullConstraint(tableName: '', columnName: '', defaultNullValue: '')
}
changeSet(id: 'drop-not-null-constraint', author: 'tlberglund') {
dropNotNullConstraint(schemaName: '', tableName: '', columnName: '', columnDataType: '')
}
changeSet(id: 'add-unique-constraint', author: 'tlberglund') {
addUniqueConstraint(tableName: '', columnNames: '', constraintName: '')
}
changeSet(id: 'drop-unique-constraint', author: 'tlberglund') {
dropUniqueConstraint(schemaName: '', tableName: '', constraintName: '')
}
changeSet(id: 'create-sequence', author: 'tlberglund') {
createSequence(sequenceName: '', schemaName: '', incrementBy: '', minValue: '', maxValue: '', ordered: true, startValue: '')
}
changeSet(id: 'drop-sequence', author: 'tlberglund') {
dropSequence(sequenceName: '')
}
changeSet(id: 'add-auto-increment', author: 'tlberglund') {
addAutoIncrement(schemaName: '', tableName: '', columnName: '', columnDataType: '')
}
changeSet(id: 'add-default-value', author: 'tlberglund') {
addDefaultValue(schemaName: '', tableName: '', columnName: '', defaultValue: '')
addDefaultValue(schemaName: '', tableName: '', columnName: '', defaultValueNumeric: '')
addDefaultValue(schemaName: '', tableName: '', columnName: '', defaultValueBoolean: '')
addDefaultValue(schemaName: '', tableName: '', columnName: '', defaultValueDate: '')
}
changeSet(id: 'drop-default-value', author: 'tlberglund') {
dropDefaultValue(schemaName: '', tableName: '', columnName: '')
}
changeSet(id: 'add-foreign-key-constraint', author: 'tlberglund') {
addForeignKeyConstraint(constraintName: '',
baseTableName: '', baseTableSchemaName: '', baseColumnNames: '',
referencedTableName: '', referencedTableSchemaName: '', referencedColumnNames: '',
deferrable: true,
initiallyDeferred: false,
deleteCascase: true,
onDelete: 'CASCADE|SET NULL|SET DEFAULT|RESTRICT|NO ACTION',
onUpdate: 'CASCADE|SET NULL|SET DEFAULT|RESTRICT|NO ACTION')
}
changeSet(id: 'drop-foreign-key', author: 'tlberglund') {
dropForeignKeyConstraint(constraintName: '', baseTableName: '', baseTableSchemaName: '')
}
changeSet(id: 'add-primary-key', author: 'tlberglund') {
addPrimaryKey(schemaName: '', tablespace: '', tableName: '', columnNames: '', constraintName: '')
}
changeSet(id: 'drop-primary-key', author: 'tlberglund') {
dropPrimaryKey(schemaName: '', tableName: '', constraintName: '')
}
- 數據操作,我們一般采用excel進行數據導入,這里用法一般不用:
changeSet(id: 'insert-data', author: 'tlberglund') {
insert(schemaName: '', tableName: '') {
column(name: '', value: '')
column(name: '', valueNumeric: '')
column(name: '', valueDate: '')
column(name: '', valueBoolean: '')
}
}
changeSet(id: 'load-data', author: 'tlberglund') {
loadData(schemaName: '', tableName: '', file: '', encoding: 'UTF8|etc') {
column(name: '', index: 2, type: 'NUMERIC')
column(name: '', index: 3, type: 'BOOLEAN')
column(name: '', header: 'shipDate', type: 'DATE')
column(name: '', index: 5, type: 'STRING')
}
}
changeSet(id: 'load-update-data', author: 'tlberglund') {
loadUpdateData(schemaName: '', tableName: '', primaryKey: '', file: '', encoding: '') {
column(name: '', index: 2, type: 'NUMERIC')
column(name: '', index: 3, type: 'BOOLEAN')
column(name: '', header: 'shipDate', type: 'DATE')
column(name: '', index: 5, type: 'STRING')
}
}
changeSet(id: 'update', author: 'tlberglund') {
update(schemaName: '', tableName: '') {
column(name: '', value: '')
column(name: '', valueNumeric: '')
column(name: '', valueDate: '')
column(name: '', valueBoolean: '')
where "species='monkey' AND status='angry'"
}
}
changeSet(id: 'delete-data', author: 'tlberglund') {
delete(schemaName: '', tableName: '') {
where "id=39" // optional
}
}
- 索引:
changeSet(id: 'create-index', author: 'tlberglund') {
createIndex(schemaName: '', tablespace: '', tableName: '', indexName: '', unique: true) {
column(name: '')
column(name: '')
column(name: '')
}
}
changeSet(id: 'drop-index', author: 'tlberglund') {
dropIndex(tableName: '', indexName: '')
}
- sql執行:
changeSet(id: 'custom-sql', author: 'tlberglund') {
sql(stripComments: true, splitStatements: false, endDelimiter: ';') {
"INSERT INTO ANIMALS (id, species, status) VALUES (1, 'monkey', 'angry')"
}
}
changeSet(id: 'sql-file', author: 'tlberglund') {
sqlFile(path: '', stripComments: true, splitStatements: '', encoding: '', endDelimiter: '')
}
- 其他:
changeSet(id: 'custom-refactoring', author: 'tlberglund') {
customChange(class: 'net.saliman.liquibase.MonkeyRefactoring') {
tableName('animal')
species('monkey')
status('angry')
}
}
changeSet(id: 'shell-command', author: 'tlberglund') {
executeCommand(executable: '') {
arg('--monkey')
arg('--skip:1')
}
}
changeSet(id: 'tag', author: 'tlberglund') {
tagDatabase(tag: 'monkey')
}
changeSet(id: 'stop', author: 'tlberglund') {
stop('Migration stopped because something bad went down')
}
include
屬性 | 描述 |
---|---|
file | 要引入的文件名 |
relativeToChangelogFile | 關聯的文件路徑是否為相對路徑,默認false |
類似js中的引入,可以通過該標簽引入其他的文件
屬性:
屬性 | 描述 |
---|---|
file | 要引入的文件名 |
relativeToChangelogFile | 關聯的文件路徑是否為相對路徑,默認false |
databaseChangeLog(logicalFilePath: '') {
include(file: '', relative: true)
}