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
This commit is contained in:
Carlos Ruiz 2021-02-19 17:52:33 +01:00 committed by GitHub
parent a292fea5d9
commit d188e563fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 85 additions and 3 deletions

View File

@ -29,6 +29,10 @@ import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException; 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.MTable;
import org.compiere.model.M_Element; import org.compiere.model.M_Element;
import org.compiere.model.Query; import org.compiere.model.Query;
@ -59,9 +63,10 @@ public class DatabaseTableRename extends SvrProcess {
@Override @Override
protected String doIt() throws Exception { protected String doIt() throws Exception {
MTable table = new MTable(getCtx(), p_AD_Table_ID, get_TrxName()); MTable table = new MTable(getCtx(), p_AD_Table_ID, get_TrxName());
String oldTableName = table.getTableName();
log.info(table.toString()); log.info(table.toString());
if ( Util.isEmpty(p_NewTableName, true) 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@"))); throw new AdempiereException(Util.cleanAmp(Msg.parseTranslation(getCtx(), "@NotValid@: @NewTableName@")));
} }
int cnt = DB.getSQLValueEx(get_TrxName(), 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))); 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<MTab> 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<MRefTable> 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<MSequence> 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<M_Element> elements = new Query(getCtx(), M_Element.Table_Name, "LOWER(ColumnName) IN (?, ?)", get_TrxName()) List<M_Element> elements = new Query(getCtx(), M_Element.Table_Name, "LOWER(ColumnName) IN (?, ?)", get_TrxName())
.setParameters(colPrefix+"_id", colPrefix+"_uu") .setParameters(colPrefix+"_id", colPrefix+"_uu")
.setOrderBy("AD_Element_ID") .setOrderBy("AD_Element_ID")
@ -87,7 +169,7 @@ public class DatabaseTableRename extends SvrProcess {
element.saveEx(); 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()); int rvalue = DB.executeUpdateEx(sql, get_TrxName());
addLog(rvalue + " - " + sql); addLog(rvalue + " - " + sql);