Last active
September 25, 2024 14:32
-
-
Save padmalcom/8f24ee64c76b83fc5e41eed20cbb2b52 to your computer and use it in GitHub Desktop.
Add precondition to liquibase changesets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install beautifulsoup4 lxml | |
# - Can handle createTable, addUniqueConstraint, and addForeignKeyConstraint | |
# - Every command has to be wrapped in an own changeset to be found by the script | |
from bs4 import BeautifulSoup | |
file = open("data.txt", "r") | |
xml = BeautifulSoup(file, 'xml') | |
changesets = xml.find_all('changeSet') | |
for changeset in changesets: | |
createTable = changeset.find_all('createTable') | |
addUniqueConstraint = changeset.find_all('addUniqueConstraint') | |
addForeignKeyConstraint = changeset.find_all('addForeignKeyConstraint') | |
if (len(createTable) > 0): | |
precondition = xml.new_tag("preConditions", onFail="MARK_RAN") | |
nott = xml.new_tag("not") | |
tableExists = xml.new_tag("tableExists", schemaName=createTable[0]['schemaName'], tableName=createTable[0]['tableName']) | |
nott.append(tableExists) | |
precondition.append(nott) | |
changeset.insert(0, precondition) | |
if (len(addUniqueConstraint) > 0): | |
precondition = xml.new_tag("preConditions", onFail="MARK_RAN") | |
nott = xml.new_tag("not") | |
tableExists = xml.new_tag("uniqueConstraintExists", constraintName=addUniqueConstraint[0]['constraintName'], tableName=addUniqueConstraint[0]['tableName']) | |
nott.append(tableExists) | |
precondition.append(nott) | |
changeset.insert(0, precondition) | |
if (len(addForeignKeyConstraint) > 0): | |
precondition = xml.new_tag("preConditions", onFail="MARK_RAN") | |
nott = xml.new_tag("not") | |
tableExists = xml.new_tag("foreignKeyConstraintExists", | |
foreignKeyName=addForeignKeyConstraint[0]['constraintName'], | |
foreignKeyTableName=addForeignKeyConstraint[0]['baseTableName'], | |
schemaName=addForeignKeyConstraint[0]['referencedTableSchemaName']) | |
nott.append(tableExists) | |
precondition.append(nott) | |
changeset.insert(0, precondition) | |
f = open("done.xml", "w") | |
f.write(xml.prettify()) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment