Skip to content
Snippets Groups Projects
Commit f1522f22 authored by Pascal Neub's avatar Pascal Neub
Browse files

Liquibase export for keyword migration

parent bf719459
No related branches found
No related tags found
No related merge requests found
......@@ -239,7 +239,7 @@ LiquiXmlUtils.databaseChangeLogWithChangeSet = function (pAuthor, pChangeSetId)
LiquiXmlUtils.colValue = function (name, value, valueAttribute)
{
var DO_NOT_ADD_EMPTY_VALUES = true;//clob and blob will ignore this setting and always skip empty values
if (DO_NOT_ADD_EMPTY_VALUES && value == "")
if (DO_NOT_ADD_EMPTY_VALUES && value === "")
return null;
var x = <column {valueAttribute}={value}/>;
x.@name = name;
......
<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.2.1" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/process/1.2.1">
<process xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.2.2" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/process/1.2.2">
<name>migrateKeywordContainers</name>
<title>Migrate keyword containers</title>
<majorModelMode>DISTRIBUTED</majorModelMode>
......
import("system.fileIO");
import("system.db");
import("system.SQLTYPES");
import("system.logging");
import("system.util");
import("system.vars");
import("Sql_lib");
import("Util_lib");
import("Liquibase_lib");
// Change this to switch between generating liquibase file and directly modify the db
var EXPORT_TO_LIQUIBASE = true;
// Writes the liquibase files to log instead of writing them to disk
// Useful for cloud systems
var WRITE_FILES_TO_LOG = false;
var newKeywordContainers = [];
var oldKeywordContainers = newSelect("AB_KEYWORD_ENTRY.CONTAINER")
.from("AB_KEYWORD_ENTRY")
.where("AB_KEYWORD_ENTRY.CONTAINER", newSelect("1")
.from("AB_KEYWORD_CATEGORY")
.where("AB_KEYWORD_CATEGORY.NAME = AB_KEYWORD_ENTRY.CONTAINER")
, SqlBuilder.NOT_EXISTS())
.groupBy("AB_KEYWORD_ENTRY.CONTAINER")
.orderBy("AB_KEYWORD_ENTRY.CONTAINER")
.arrayColumn();
oldKeywordContainers.forEach(function(pElement, pIndex, pArray) {
var columns = ["AB_KEYWORD_CATEGORYID", "NAME", "SORTINGBY", "SORTINGDIRECTION"];
var values = [util.getNewUUID(), pElement, 0, "ASC"];
logging.log(values);
//(new SqlBuilder()).insertData("AB_KEYWORD_CATEGORY", columns, null, values);
.from("AB_KEYWORD_ENTRY")
.where(
"AB_KEYWORD_ENTRY.CONTAINER",
newSelect("AB_KEYWORD_CATEGORY.NAME")
.from("AB_KEYWORD_CATEGORY")
.where("AB_KEYWORD_CATEGORY.NAME = AB_KEYWORD_ENTRY.CONTAINER"),
SqlBuilder.NOT_EXISTS()
)
.groupBy("AB_KEYWORD_ENTRY.CONTAINER")
.orderBy("AB_KEYWORD_ENTRY.CONTAINER")
.arrayColumn();
var categories = {};
newSelect(["AB_KEYWORD_CATEGORYID", "AB_KEYWORD_CATEGORY.NAME"])
.from("AB_KEYWORD_CATEGORY").table().forEach(function(pRow) {
categories[pRow[1]] = pRow[0];
});
var keywordUpdateQuery = "UPDATE AB_KEYWORD_ENTRY"
+ "SET AB_KEYWORD_CATEGORY_ID = ("
+ "SELECT AB_KEYWORD_CATEGORYID "
+ "FROM AB_KEYWORD_CATEGORY "
+ "WHERE AB_KEYWORD_CATEGORY.NAME = AB_KEYWORD_ENTRY.CONTAINER"
+ ");"
;
if(EXPORT_TO_LIQUIBASE)
{
function _writeLiquibaseXml(name, xmlScript)
{
var fileContent = LiquiXmlUtils.xmlHeaderLineStr() + "\n" + xmlScript;
if(WRITE_FILES_TO_LOG)
{
logging.log(name);
logging.log(fileContent + "\n");
}
else
{
var outFolderPath = vars.get("$sys.servertemp") + "/" + "keywordMigration" + "/";
fileIO.storeData(outFolderPath + "init_keyword_category.xml",
fileContent, util.DATA_TEXT, false, "UTF-8");
}
}
// init_keyword_category.xml
var liquibaseContainers = LiquiXmlUtils.databaseChangeLogWithChangeSet("autogenerated");
oldKeywordContainers.forEach(function(pElement) {
var uuid = util.getNewUUID();
categories[pElement] = uuid;
var attributeUpdateQuery = "UPDATE AB_KEYWORD_ATTRIBUTE"
+ "SET AB_KEYWORD_CATEGORY_ID = ("
+ "SELECT AB_KEYWORD_CATEGORYID "
+ "FROM AB_KEYWORD_CATEGORY "
+ "WHERE AB_KEYWORD_CATEGORY.NAME = AB_KEYWORD_ATTRIBUTE.CONTAINER"
+ ");"
;
var categoryTable = LiquiXTable.make("AB_KEYWORD_CATEGORY");
categoryTable.addIdCol("AB_KEYWORD_CATEGORYID", uuid);
categoryTable.addStrCol("NAME", pElement);
categoryTable.addNumberCol("SORTINGBY", 0);
categoryTable.addStrCol("SORTINGDIRECTION", "ASC");
liquibaseContainers.changeSet.insert += categoryTable.xml;
});
_writeLiquibaseXml("init_keyword_category.xml", liquibaseContainers);
// update_keyword_entry
var updateEntryChangeset = LiquiXmlUtils.databaseChangeLogWithChangeSet("autogenerated");
newSelect(["AB_KEYWORD_ENTRYID", "AB_KEYWORD_ENTRY.CONTAINER"])
.from("AB_KEYWORD_ENTRY")
.where("AB_KEYWORD_CATEGORY_ID is null")
.table().forEach(function(pRow) {
var updateXml = <update tableName="AB_KEYWORD_ENTRY"/>;
updateXml.column = <column name="AB_KEYWORD_CATEGORY_ID" value={categories[pRow[1]]}/>;
updateXml.where = <where>AB_KEYWORD_ENTRYID = {"'" + pRow[0] + "'"}</where>;
updateEntryChangeset.changeSet.update += updateXml;
});
_writeLiquibaseXml("update_keyword_entry.xml", updateEntryChangeset);
//logging.log(db.runStatement(keywordUpdateQuery) + " keyword containers updated");
//logging.log(db.runStatement(attributeUpdateQuery) + " attribute containers updated");
// update_keyword_attribute
var updateAttributeChangeset = LiquiXmlUtils.databaseChangeLogWithChangeSet("autogenerated");
newSelect(["AB_KEYWORD_ATTRIBUTEID", "AB_KEYWORD_ATTRIBUTE.CONTAINER"])
.from("AB_KEYWORD_ATTRIBUTE")
.where("AB_KEYWORD_CATEGORY_ID is null")
.table().forEach(function(pRow) {
var updateXml = <update tableName="AB_KEYWORD_ATTRIBUTE"/>;
updateXml.column = <column name="AB_KEYWORD_CATEGORY_ID" value={categories[pRow[1]]}/>;
updateXml.where = <where>AB_KEYWORD_ATTRIBUTEID = {"'" + pRow[0] + "'"}</where>;
updateAttributeChangeset.changeSet.update += updateXml;
});
_writeLiquibaseXml("update_keyword_attribute.xml", updateAttributeChangeset);
// changelog.xml
var changelogFile = LiquiXmlUtils.databaseChangeLog();
changelogFile.include += <include relativeToChangelogFile="true" file="init_keyword_category.xml"/>;
changelogFile.include += <include relativeToChangelogFile="true" file="update_keyword_entry.xml"/>;
changelogFile.include += <include relativeToChangelogFile="true" file="update_keyword_attribute.xml"/>;
_writeLiquibaseXml("changelog.xml", changelogFile);
logging.log("Keyword migration liquibase scripts created succesfully");
}
else
{
var statements = [];
oldKeywordContainers.forEach(function(pElement) {
var uuid = util.getNewUUID();
categories[pElement] = uuid;
var columns = ["AB_KEYWORD_CATEGORYID", "NAME", "SORTINGBY", "SORTINGDIRECTION"];
statements.push([
"AB_KEYWORD_CATEGORY",
columns,
db.getColumnTypes("AB_KEYWORD_CATEGORY", columns),
[uuid, pElement, "0", "ASC"]
]);
});
var FIELD_NAMES = ["AB_KEYWORD_CATEGORY_ID"];
var FIELD_TYPES = [SQLTYPES.VARCHAR];
newSelect(["AB_KEYWORD_ENTRYID", "AB_KEYWORD_ENTRY.CONTAINER"])
.from("AB_KEYWORD_ENTRY")
.where("AB_KEYWORD_CATEGORY_ID is null")
.table().forEach(function(pRow) {
statements.push([
"AB_KEYWORD_ENTRY",
FIELD_NAMES, FIELD_TYPES,
[categories[pRow[1]]],
"AB_KEYWORD_ENTRYID = '" + pRow[0] + "'"]);
});
newSelect(["AB_KEYWORD_ATTRIBUTEID", "AB_KEYWORD_ATTRIBUTE.CONTAINER"])
.from("AB_KEYWORD_ATTRIBUTE")
.where("AB_KEYWORD_CATEGORY_ID is null")
.table().forEach(function(pRow) {
statements.push([
"AB_KEYWORD_ATTRIBUTE",
FIELD_NAMES, FIELD_TYPES,
[categories[pRow[1]]],
"AB_KEYWORD_ATTRIBUTEID = '" + pRow[0] + "'"]);
});
db.execute(statements);
logging.log("Keywords migrated succesfully");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment