From e2b1a4e146a403ed6ec18f67f4065d17096d8717 Mon Sep 17 00:00:00 2001 From: Michael Heyes Date: Thu, 4 May 2017 11:20:03 +0100 Subject: [PATCH] Replace DatabaseSourceLoaderWithCommit with a simple constructor switch on the original DatabaseSourceLoader class. Rename loader tag for builder to DatabaseSourceImplicitCommit --- .../scriptrunner2/loader/BuiltInLoader.java | 4 +- .../loader/DatabaseSourceLoader.java | 26 ++++++- .../DatabaseSourceWithCommitLoader.java | 73 ------------------- 3 files changed, 26 insertions(+), 77 deletions(-) delete mode 100644 src/com/fivium/scriptrunner2/loader/DatabaseSourceWithCommitLoader.java diff --git a/src/com/fivium/scriptrunner2/loader/BuiltInLoader.java b/src/com/fivium/scriptrunner2/loader/BuiltInLoader.java index 72b0ead..9281783 100644 --- a/src/com/fivium/scriptrunner2/loader/BuiltInLoader.java +++ b/src/com/fivium/scriptrunner2/loader/BuiltInLoader.java @@ -11,7 +11,7 @@ public abstract class BuiltInLoader implements Loader { public static final String LOADER_NAME_DB_SOURCE = "DatabaseSource"; - public static final String LOADER_NAME_DB_SOURCE_WITH_COMMIT = "DatabaseSourceWithCommit"; + public static final String LOADER_NAME_DB_SOURCE_WITH_COMMIT = "DatabaseSourceImplicitCommit"; public static final String LOADER_NAME_SCRIPTRUNNER_UTIL = "ScriptRunnerUtil"; public static final String LOADER_NAME_PATCH = "Patch"; /** Special loader name for the builder only - files associated with this loader are ignored when constructing the manifest. */ @@ -21,7 +21,7 @@ public abstract class BuiltInLoader static { gBuiltInLoaderMap = new HashMap(); gBuiltInLoaderMap.put(LOADER_NAME_DB_SOURCE, new DatabaseSourceLoader()); - gBuiltInLoaderMap.put(LOADER_NAME_DB_SOURCE_WITH_COMMIT, new DatabaseSourceWithCommitLoader()); + gBuiltInLoaderMap.put(LOADER_NAME_DB_SOURCE_WITH_COMMIT, new DatabaseSourceLoader(true)); gBuiltInLoaderMap.put(LOADER_NAME_SCRIPTRUNNER_UTIL, UtilLoader.getInstance()); gBuiltInLoaderMap.put(LOADER_NAME_PATCH, new PatchScriptLoader()); } diff --git a/src/com/fivium/scriptrunner2/loader/DatabaseSourceLoader.java b/src/com/fivium/scriptrunner2/loader/DatabaseSourceLoader.java index 8dfdf32..3e4539e 100644 --- a/src/com/fivium/scriptrunner2/loader/DatabaseSourceLoader.java +++ b/src/com/fivium/scriptrunner2/loader/DatabaseSourceLoader.java @@ -24,11 +24,29 @@ */ public class DatabaseSourceLoader extends SourceLoader { - + + private boolean implicitCommit = false; + + /** + * Default Constructor + */ + public DatabaseSourceLoader(){ + super(); + } + + /** + * Constructor to force commit of active transaction once file has been loaded + * @param pImplicitCommit + */ + public DatabaseSourceLoader(boolean pImplicitCommit){ + super(); + this.implicitCommit = pImplicitCommit; + } + @Override public void doPromote(ScriptRunner pScriptRunner, PromotionFile pFile) throws ExPromote { - + long lStart = System.currentTimeMillis(); Logger.logInfo("\nPromote DatabaseSource " + pFile.getFilePath()); //Read the DBSource file in @@ -62,6 +80,10 @@ public void doPromote(ScriptRunner pScriptRunner, PromotionFile pFile) for(ScriptExecutable lExecutable : lExecutableList){ lExecutable.execute(pScriptRunner.getDatabaseConnection()); } + // if the transaction is still active at the end of the script AND loader has implicitCommit flag set, perform a commit. + if (implicitCommit) + pScriptRunner.getDatabaseConnection().unsafelyCommit(); + } catch (Throwable th){ throw new ExPromote("Failed to promote DatabaseSource " + pFile.getFilePath() + ": " + th.getMessage(), th); diff --git a/src/com/fivium/scriptrunner2/loader/DatabaseSourceWithCommitLoader.java b/src/com/fivium/scriptrunner2/loader/DatabaseSourceWithCommitLoader.java deleted file mode 100644 index e04326b..0000000 --- a/src/com/fivium/scriptrunner2/loader/DatabaseSourceWithCommitLoader.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.fivium.scriptrunner2.loader; - - -import com.fivium.scriptrunner2.Logger; -import com.fivium.scriptrunner2.PromotionFile; -import com.fivium.scriptrunner2.ScriptRunner; -import com.fivium.scriptrunner2.ex.ExFatalError; -import com.fivium.scriptrunner2.ex.ExParser; -import com.fivium.scriptrunner2.ex.ExPromote; -import com.fivium.scriptrunner2.script.ScriptExecutable; -import com.fivium.scriptrunner2.script.ScriptExecutableParser; -import com.fivium.scriptrunner2.script.ScriptSQL; -import org.apache.commons.io.FileUtils; - -import java.io.IOException; -import java.util.List; - - -/** - * A loader for loading Database Source. Database Source typically includes packages, triggers, views, and etc. A - * DatabaseSource promotion file must consist of single SQL DDL statement to be loaded by this loader. - */ -public class DatabaseSourceWithCommitLoader -extends SourceLoader { - - @Override - public void doPromote(ScriptRunner pScriptRunner, PromotionFile pFile) - throws ExPromote { - long lStart = System.currentTimeMillis(); - Logger.logInfo("\nPromote DatabaseSource " + pFile.getFilePath()); - //Read the DBSource file in - String lFileContents; - try { - lFileContents = FileUtils.readFileToString(pScriptRunner.resolveFile(pFile.getFilePath())); - } - catch (IOException e) { - throw new ExFatalError("Failed to read contents of file " + pFile.getFilePath(), e); - } - - List lExecutableList; - try { - lExecutableList = ScriptExecutableParser.parseScriptExecutables(lFileContents, false); - } - catch (ExParser e) { - throw new ExFatalError("Failed to read contents of file " + pFile.getFilePath() + ": " + e.getMessage(), e); - } - - Logger.logDebug("Validating source file"); - - //Validate contents - for(ScriptExecutable lExecutable : lExecutableList){ - if(!(lExecutable instanceof ScriptSQL)){ - throw new ExPromote("File " + pFile.getFilePath() + " has invalid contents: non-SQL markup not permitted in Database Source, " + - "but a " + lExecutable.getDisplayString() + " command was found"); - } - } - - try { - for(ScriptExecutable lExecutable : lExecutableList){ - lExecutable.execute(pScriptRunner.getDatabaseConnection()); - } - // if the transaction is still active at the end of the script, perform a commit. - pScriptRunner.getDatabaseConnection().unsafelyCommit(); - } - catch (Throwable th){ - throw new ExPromote("Failed to promote DatabaseSource " + pFile.getFilePath() + ": " + th.getMessage(), th); - } - long lTime = System.currentTimeMillis() - lStart; - Logger.logInfo("OK (took " + lTime + "ms)"); - - } - -}