From d188e563fc2cb183ea891b25a48def05a98113b7 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Fri, 19 Feb 2021 17:52:33 +0100 Subject: [PATCH] IDEMPIERE-4358 Rename table issue (#590) * IDEMPIERE-4358 Rename table issue Rename the table also in whereclause and orderbyclause in AD_Tab and AD_Ref_Table * * Fix issue with Sequence reported by @d-ruiz * Improve log messages * * Improve reference message --- .../compiere/process/DatabaseTableRename.java | 88 ++++++++++++++++++- 1 file changed, 85 insertions(+), 3 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/process/DatabaseTableRename.java b/org.adempiere.base/src/org/compiere/process/DatabaseTableRename.java index f709ce9d3e..a050e4ca7b 100644 --- a/org.adempiere.base/src/org/compiere/process/DatabaseTableRename.java +++ b/org.adempiere.base/src/org/compiere/process/DatabaseTableRename.java @@ -29,6 +29,10 @@ import java.util.List; import java.util.logging.Level; import org.adempiere.exceptions.AdempiereException; +import org.compiere.model.MClient; +import org.compiere.model.MRefTable; +import org.compiere.model.MSequence; +import org.compiere.model.MTab; import org.compiere.model.MTable; import org.compiere.model.M_Element; import org.compiere.model.Query; @@ -59,9 +63,10 @@ public class DatabaseTableRename extends SvrProcess { @Override protected String doIt() throws Exception { MTable table = new MTable(getCtx(), p_AD_Table_ID, get_TrxName()); + String oldTableName = table.getTableName(); log.info(table.toString()); if ( Util.isEmpty(p_NewTableName, true) - || p_NewTableName.toLowerCase().equals(table.getTableName().toLowerCase())) { + || p_NewTableName.toLowerCase().equals(oldTableName.toLowerCase())) { throw new AdempiereException(Util.cleanAmp(Msg.parseTranslation(getCtx(), "@NotValid@: @NewTableName@"))); } int cnt = DB.getSQLValueEx(get_TrxName(), @@ -71,7 +76,84 @@ public class DatabaseTableRename extends SvrProcess { throw new AdempiereException(Util.cleanAmp(Msg.parseTranslation(getCtx(), "@AlreadyExists@: @TableName@ = " + p_NewTableName))); } - String colPrefix = table.getTableName().toLowerCase(); + String regex = "(?i)\\b" + oldTableName + "\\."; + String fullregex = ".*" + regex + ".*"; + + // Rename table in WhereClause and OrderByClause in AD_Tab + List tabs = new Query(getCtx(), MTab.Table_Name, "AD_Table_ID=? AND (WhereClause IS NOT NULL OR OrderByClause IS NOT NULL)", get_TrxName()) + .setParameters(p_AD_Table_ID) + .list(); + for (MTab tab : tabs) { + boolean changed = false; + String whereClause = tab.getWhereClause(); + if (whereClause != null && whereClause.matches(fullregex)) { + whereClause = whereClause.replaceAll(regex, p_NewTableName + "."); + tab.setWhereClause(whereClause); + changed = true; + } + String orderByClause = tab.getOrderByClause(); + if (orderByClause != null && orderByClause.matches(fullregex)) { + orderByClause = orderByClause.replaceAll(regex, p_NewTableName + "."); + tab.setOrderByClause(orderByClause); + changed = true; + } + if (changed) { + tab.saveEx(); + addLog(0, null, null, "@Updated@ @AD_Tab_ID@ " + tab.getName(), MTab.Table_ID, tab.getAD_Tab_ID()); + } + } + + // Rename table in WhereClause and OrderByClause in AD_Ref_Table + List refts = new Query(getCtx(), MRefTable.Table_Name, "AD_Table_ID=? AND (WhereClause IS NOT NULL OR OrderByClause IS NOT NULL)", get_TrxName()) + .setParameters(p_AD_Table_ID) + .list(); + for (MRefTable reft : refts) { + boolean changed = false; + String whereClause = reft.getWhereClause(); + if (whereClause != null && whereClause.matches(fullregex)) { + whereClause = whereClause.replaceAll(regex, p_NewTableName + "."); + reft.setWhereClause(whereClause); + changed = true; + } + String orderByClause = reft.getOrderByClause(); + if (orderByClause != null && orderByClause.matches(fullregex)) { + orderByClause = orderByClause.replaceAll(regex, p_NewTableName + "."); + reft.setOrderByClause(orderByClause); + changed = true; + } + if (changed) { + reft.saveEx(); + addLog(0, null, null, "@Updated@ @AD_Reference_ID@ " + reft.getAD_Reference().getName(), MRefTable.Table_ID, reft.getAD_Reference_ID()); + } + } + + // Rename table in sequences + String whereSeq = "(Name=? AND Description=? AND IsTableID='Y') OR (Name=? AND Description=? AND IsTableID='N')"; + List seqs = new Query(getCtx(), MSequence.Table_Name, whereSeq, get_TrxName()) + .setParameters( + oldTableName, + "Table "+oldTableName, + "DocumentNo_"+oldTableName, + "DocumentNo/Value for Table "+oldTableName + ) + .list(); + for (MSequence seq : seqs) { + if (seq.isTableID()) { + seq.setName(p_NewTableName); + seq.setDescription("Table "+p_NewTableName); + } else { + seq.setName("DocumentNo_"+p_NewTableName); + seq.setDescription("DocumentNo/Value for Table "+p_NewTableName); + } + seq.saveEx(); + if (seq.getAD_Client_ID() == 0) { + addLog(0, null, null, "@Updated@ @AD_Sequence_ID@ " + seq.getName(), MSequence.Table_ID, seq.getAD_Sequence_ID()); + } else { + addLog(0, null, null, "@Updated@ @AD_Sequence_ID@ " + seq.getName() + ", @AD_Client_ID@ " + MClient.get(seq.getAD_Client_ID()).getName()); + } + } + + String colPrefix = oldTableName.toLowerCase(); List elements = new Query(getCtx(), M_Element.Table_Name, "LOWER(ColumnName) IN (?, ?)", get_TrxName()) .setParameters(colPrefix+"_id", colPrefix+"_uu") .setOrderBy("AD_Element_ID") @@ -87,7 +169,7 @@ public class DatabaseTableRename extends SvrProcess { element.saveEx(); } - String sql = "ALTER TABLE " + table.getTableName() + " RENAME TO " + p_NewTableName; + String sql = "ALTER TABLE " + oldTableName + " RENAME TO " + p_NewTableName; int rvalue = DB.executeUpdateEx(sql, get_TrxName()); addLog(rvalue + " - " + sql);