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.IOException;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.SQLWarning;
|
import java.sql.SQLWarning;
|
||||||
|
@ -29,6 +31,7 @@ import java.sql.Statement;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
@ -36,6 +39,8 @@ import java.util.logging.Level;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.adempiere.exceptions.AdempiereException;
|
||||||
|
import org.compiere.Adempiere;
|
||||||
import org.compiere.db.Database;
|
import org.compiere.db.Database;
|
||||||
import org.compiere.util.CLogger;
|
import org.compiere.util.CLogger;
|
||||||
import org.compiere.util.DisplayType;
|
import org.compiere.util.DisplayType;
|
||||||
|
@ -76,9 +81,9 @@ public abstract class Convert
|
||||||
/** Logger */
|
/** Logger */
|
||||||
private static final CLogger log = CLogger.getCLogger (Convert.class);
|
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 Writer writerOr;
|
||||||
private static FileOutputStream tempFilePg = null;
|
private static FileOutputStream fosScriptPg = null;
|
||||||
private static Writer writerPg;
|
private static Writer writerPg;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -443,34 +448,65 @@ public abstract class Convert
|
||||||
if (logMigrationScript) {
|
if (logMigrationScript) {
|
||||||
if (dontLog(oraStatement))
|
if (dontLog(oraStatement))
|
||||||
return;
|
return;
|
||||||
// Log oracle and postgres migration scripts in temp directory
|
// Log oracle and postgres migration scripts in migration/iD[version]/[oracle|postgresql] directory
|
||||||
// migration_script_oracle.sql and migration_script_postgresql.sql
|
// [timestamp]_[ticket].sql
|
||||||
|
String fileName = null;
|
||||||
|
String folderOr = null;
|
||||||
|
String folderPg = null;
|
||||||
|
String prm_COMMENT = null;
|
||||||
try {
|
try {
|
||||||
if (tempFileOr == null) {
|
if (fosScriptOr == null || fosScriptPg == null) {
|
||||||
File fileNameOr = File.createTempFile("migration_script_", "_oracle.sql");
|
String now = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
|
||||||
tempFileOr = new FileOutputStream(fileNameOr, true);
|
prm_COMMENT = Env.getContext(Env.getCtx(), "MigrationScriptComment");
|
||||||
writerOr = new BufferedWriter(new OutputStreamWriter(tempFileOr, "UTF8"));
|
String pattern = "(IDEMPIERE-[0-9]*)";
|
||||||
writerOr.append("SET SQLBLANKLINES ON\nSET DEFINE OFF\n\n");
|
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);
|
writeLogMigrationScript(writerOr, oraStatement);
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (pgStatement == null) {
|
if (pgStatement == null) {
|
||||||
// if oracle call convert for postgres before logging
|
// if oracle call convert for postgres before logging
|
||||||
Convert convert = Database.getDatabase(Database.DB_POSTGRESQL).getConvert();
|
Convert convert = Database.getDatabase(Database.DB_POSTGRESQL).getConvert();
|
||||||
String[] r = convert.convert(oraStatement);
|
String[] r = convert.convert(oraStatement);
|
||||||
pgStatement = r[0];
|
pgStatement = r[0];
|
||||||
}
|
}
|
||||||
if (tempFilePg == null) {
|
if (fosScriptPg == null) {
|
||||||
File fileNamePg = File.createTempFile("migration_script_", "_postgresql.sql");
|
File filePg = new File(folderPg + fileName);
|
||||||
tempFilePg = new FileOutputStream(fileNamePg, true);
|
fosScriptPg = new FileOutputStream(filePg, true);
|
||||||
writerPg = new BufferedWriter(new OutputStreamWriter(tempFilePg, "UTF8"));
|
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);
|
writeLogMigrationScript(writerPg, pgStatement);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
throw new AdempiereException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -583,20 +619,8 @@ public abstract class Convert
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String m_oldprm_COMMENT = "";
|
|
||||||
|
|
||||||
private static void writeLogMigrationScript(Writer w, String statement) throws IOException
|
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
|
// log time and date
|
||||||
SimpleDateFormat format = DisplayType.getDateFormat(DisplayType.DateTime);
|
SimpleDateFormat format = DisplayType.getDateFormat(DisplayType.DateTime);
|
||||||
String dateTimeText = format.format(new Timestamp(System.currentTimeMillis()));
|
String dateTimeText = format.format(new Timestamp(System.currentTimeMillis()));
|
||||||
|
|
Loading…
Reference in New Issue