diff --git a/process/Liquibase_lib/process.js b/process/Liquibase_lib/process.js index e97ca5b27214feb4d5a3589abdf1928d5c72ee3f..dc12de6cc2530f6df82778c8644d4add36b1ad65 100644 --- a/process/Liquibase_lib/process.js +++ b/process/Liquibase_lib/process.js @@ -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; diff --git a/process/migrateKeywordContainers/migrateKeywordContainers.aod b/process/migrateKeywordContainers/migrateKeywordContainers.aod index 6cccb9359719985c4f4e8faf3b6058b929981710..d1bb16a7a5afedcfdaa36b334c2009339dcf7e2a 100644 --- a/process/migrateKeywordContainers/migrateKeywordContainers.aod +++ b/process/migrateKeywordContainers/migrateKeywordContainers.aod @@ -1,5 +1,5 @@ <?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> diff --git a/process/migrateKeywordContainers/process.js b/process/migrateKeywordContainers/process.js index f9734d8dd35c55bbeabc6c116c4eacdd13597957..cfffbb903c8af0de9ec4c18b53ed4741bc2b73c3 100644 --- a/process/migrateKeywordContainers/process.js +++ b/process/migrateKeywordContainers/process.js @@ -1,44 +1,149 @@ +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"); +}