问题
New to liquibase. I have an existing schema managed by hibernate. I now need to remove some tables and I'm using liquibase. I wrote a config that succesfully deletes the table.
But I want the check to run only if the table exists, since a new install of the system would not have the table since the hibernate mapping objects no longer exist.
I attempted to add a precondition. However, my logs still indicating that liquibase is failling becuase it tries to delete the table and it doesn't exist. What am I not doing correctly? I'm using Spring Boot / Java
databaseChangeLog:
- preConditions:
on-fail: mark_ran
on-error: mark_ran
tableExists:
tableName: some_old_table
schemaName: public
- changeSet:
id: 01_del_old_table
author: some-dev-01
comment: Remove old table
changes:
- dropTable:
tableName: some_old_table
error in log: Table public.some_old_table does not exist ...
PreconditionFailedException
It's like ti's checking the preconditions... and still failing them.
Also tried
databaseChangeLog:
- changeSet:
id: zzchange-1.0-remove-xczczxc
author: zzzz
comment: Remove some_old_table table - no longer needed
preConditions:
on-fail: mark_ran
tableExists:
tableName: some_old_table
changes:
- dropTable:
tableName: some_old_table
回答1:
In your second try put your comment after your precondition
databaseChangeLog:
- changeSet:
id: zzchange-1.0-remove-xczczxc
author: zzzz
preConditions:
on-fail: mark_ran
tableExists:
tableName: some_old_table
comment: Remove some_old_table table - no longer needed
changes:
- dropTable:
tableName: some_old_table
This is what they recommend in liquibase documentation here: https://www.liquibase.org/documentation/preconditions.html
I am not really sure why your first try will not work but however I don't think it is a good idea to have a global precondition. This is because what they say in the same documentation:
Preconditions at the changelog level apply to all changesets, not just those listed in the current changelog or its child changelogs.
回答2:
The issue was in the on-fail attribute not being spelled correctly. on-fail should be onFail.
As @Julian mentioned, it's best to put a precondition scoped to a specific change set, and the comment should go after a preCondition, although that was not the issue at hand here.
databaseChangeLog:
- changeSet:
id: zzchange-1.0-remove-xczczxc
author: zzzz
preConditions:
on-fail: mark_ran
tableExists:
tableName: some_old_table
comment: Remove some_old_table table - no longer needed
changes:
- dropTable:
tableName: some_old_table
来源:https://stackoverflow.com/questions/57562687/skipping-liquibase-change-if-table-exists