IDEMPIERE-5190 Improve migration script generator (#1183)
This commit is contained in:
parent
69d11d0370
commit
0e66985292
|
@ -22,6 +22,8 @@ import java.io.FileOutputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLWarning;
|
||||
|
@ -29,6 +31,7 @@ import java.sql.Statement;
|
|||
import java.sql.Timestamp;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
|
@ -36,6 +39,8 @@ import java.util.logging.Level;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.adempiere.exceptions.AdempiereException;
|
||||
import org.compiere.Adempiere;
|
||||
import org.compiere.db.Database;
|
||||
import org.compiere.util.CLogger;
|
||||
import org.compiere.util.DisplayType;
|
||||
|
@ -76,9 +81,9 @@ public abstract class Convert
|
|||
/** Logger */
|
||||
private static final CLogger log = CLogger.getCLogger (Convert.class);
|
||||
|
||||
private static FileOutputStream tempFileOr = null;
|
||||
private static FileOutputStream fosScriptOr = null;
|
||||
private static Writer writerOr;
|
||||
private static FileOutputStream tempFilePg = null;
|
||||
private static FileOutputStream fosScriptPg = null;
|
||||
private static Writer writerPg;
|
||||
|
||||
/**
|
||||
|
@ -443,34 +448,65 @@ public abstract class Convert
|
|||
if (logMigrationScript) {
|
||||
if (dontLog(oraStatement))
|
||||
return;
|
||||
// Log oracle and postgres migration scripts in temp directory
|
||||
// migration_script_oracle.sql and migration_script_postgresql.sql
|
||||
// Log oracle and postgres migration scripts in migration/iD[version]/[oracle|postgresql] directory
|
||||
// [timestamp]_[ticket].sql
|
||||
String fileName = null;
|
||||
String folderOr = null;
|
||||
String folderPg = null;
|
||||
String prm_COMMENT = null;
|
||||
try {
|
||||
if (tempFileOr == null) {
|
||||
File fileNameOr = File.createTempFile("migration_script_", "_oracle.sql");
|
||||
tempFileOr = new FileOutputStream(fileNameOr, true);
|
||||
writerOr = new BufferedWriter(new OutputStreamWriter(tempFileOr, "UTF8"));
|
||||
writerOr.append("SET SQLBLANKLINES ON\nSET DEFINE OFF\n\n");
|
||||
if (fosScriptOr == null || fosScriptPg == null) {
|
||||
String now = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
|
||||
prm_COMMENT = Env.getContext(Env.getCtx(), "MigrationScriptComment");
|
||||
String pattern = "(IDEMPIERE-[0-9]*)";
|
||||
Pattern p = Pattern.compile(pattern);
|
||||
Matcher m = p.matcher(prm_COMMENT);
|
||||
String ticket = null;
|
||||
if (m.find())
|
||||
ticket = m.group(1);
|
||||
if (ticket == null)
|
||||
ticket = "PlaceholderForTicket";
|
||||
fileName = now + "_" + ticket + ".sql";
|
||||
String version = Adempiere.MAIN_VERSION.substring(8);
|
||||
boolean isIDE = Files.isDirectory(Paths.get(Adempiere.getAdempiereHome() + File.separator + "org.adempiere.base"));
|
||||
String homeScript;
|
||||
if (isIDE)
|
||||
homeScript = Adempiere.getAdempiereHome() + File.separator;
|
||||
else
|
||||
homeScript = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
folderOr = homeScript + "migration" + File.separator + "iD" + version + File.separator + "oracle" + File.separator;
|
||||
folderPg = homeScript + "migration" + File.separator + "iD" + version + File.separator + "postgresql" + File.separator;
|
||||
Files.createDirectories(Paths.get(folderOr));
|
||||
Files.createDirectories(Paths.get(folderPg));
|
||||
}
|
||||
if (fosScriptOr == null) {
|
||||
File fileOr = new File(folderOr + fileName);
|
||||
fosScriptOr = new FileOutputStream(fileOr, true);
|
||||
writerOr = new BufferedWriter(new OutputStreamWriter(fosScriptOr, "UTF8"));
|
||||
writerOr.append("-- ");
|
||||
writerOr.append(prm_COMMENT);
|
||||
writerOr.append("\nSELECT register_migration_script('").append(fileName).append("') FROM dual;\n\n");
|
||||
// adding these in prevention because some oracle scripts have multiline strings or @ that are misinterpreted by sqlplus
|
||||
writerOr.append("SET SQLBLANKLINES ON\nSET DEFINE OFF\n\n");
|
||||
}
|
||||
writeLogMigrationScript(writerOr, oraStatement);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
if (pgStatement == null) {
|
||||
// if oracle call convert for postgres before logging
|
||||
Convert convert = Database.getDatabase(Database.DB_POSTGRESQL).getConvert();
|
||||
String[] r = convert.convert(oraStatement);
|
||||
pgStatement = r[0];
|
||||
}
|
||||
if (tempFilePg == null) {
|
||||
File fileNamePg = File.createTempFile("migration_script_", "_postgresql.sql");
|
||||
tempFilePg = new FileOutputStream(fileNamePg, true);
|
||||
writerPg = new BufferedWriter(new OutputStreamWriter(tempFilePg, "UTF8"));
|
||||
if (fosScriptPg == null) {
|
||||
File filePg = new File(folderPg + fileName);
|
||||
fosScriptPg = new FileOutputStream(filePg, true);
|
||||
writerPg = new BufferedWriter(new OutputStreamWriter(fosScriptPg, "UTF8"));
|
||||
writerPg.append("-- ");
|
||||
writerPg.append(prm_COMMENT);
|
||||
writerPg.append("\nSELECT register_migration_script('").append(fileName).append("') FROM dual;\n\n");
|
||||
}
|
||||
writeLogMigrationScript(writerPg, pgStatement);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new AdempiereException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -583,20 +619,8 @@ public abstract class Convert
|
|||
return false;
|
||||
}
|
||||
|
||||
private static String m_oldprm_COMMENT = "";
|
||||
|
||||
private static void writeLogMigrationScript(Writer w, String statement) throws IOException
|
||||
{
|
||||
String prm_COMMENT;
|
||||
prm_COMMENT = Env.getContext(Env.getCtx(), "MigrationScriptComment");
|
||||
if (prm_COMMENT != null && ! m_oldprm_COMMENT.equals(prm_COMMENT)) {
|
||||
// log sysconfig comment
|
||||
w.append("-- ");
|
||||
w.append(prm_COMMENT);
|
||||
w.append("\n");
|
||||
if (w == writerPg)
|
||||
m_oldprm_COMMENT = prm_COMMENT;
|
||||
}
|
||||
// log time and date
|
||||
SimpleDateFormat format = DisplayType.getDateFormat(DisplayType.DateTime);
|
||||
String dateTimeText = format.format(new Timestamp(System.currentTimeMillis()));
|
||||
|
|
Loading…
Reference in New Issue