From 9c536fc68b56deb972852a4a3a1a97d7fc636630 Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Thu, 2 Dec 2010 09:37:29 +0000 Subject: [PATCH 1/5] gitignore eclipse workspace --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 309d121..e21e2a9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ target .idea *~ +.project +.classpath +.settings From 75a7d317f207515d71ed45713175cac6038dc230 Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Thu, 2 Dec 2010 10:26:26 +0000 Subject: [PATCH 2/5] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e21e2a9..4eabd3e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ target .project .classpath .settings +classes From 13fd5d874622636af84234b1c72dbd01aa96fc11 Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Thu, 2 Dec 2010 15:46:05 +0000 Subject: [PATCH 3/5] changes to api towards targetVersion --- .../src/main/java/com/dbdeploy/AntTarget.java | 6 +- .../com/dbdeploy/ChangeScriptApplier.java | 9 - .../main/java/com/dbdeploy/Controller.java | 98 +++++--- .../src/main/java/com/dbdeploy/DbDeploy.java | 37 +-- .../dbdeploy/appliers/DirectToDbApplier.java | 10 +- .../appliers/TemplateBasedApplier.java | 25 +- .../appliers/UndoTemplateBasedApplier.java | 17 -- .../java/com/dbdeploy/ControllerTest.java | 54 +++-- .../appliers/TemplateBasedApplierTest.java | 13 +- .../database/ScriptGenerationTest.java | 217 +++++++++--------- 10 files changed, 253 insertions(+), 233 deletions(-) delete mode 100644 dbdeploy-core/src/main/java/com/dbdeploy/ChangeScriptApplier.java delete mode 100644 dbdeploy-core/src/main/java/com/dbdeploy/appliers/UndoTemplateBasedApplier.java diff --git a/dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java b/dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java index d198c2a..d7bdb8c 100644 --- a/dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java +++ b/dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java @@ -21,7 +21,7 @@ public class AntTarget extends Task { + "\n\t\ttemplatedir=\"[DIRECTORY FOR DBMS TEMPLATE SCRIPTS, IF NOT USING BUILT-IN]\"" + "\n\t\tdir=\"[YOUR SCRIPT FOLDER]\" *" + "\n\t\toutputfile=\"[OUTPUT SCRIPT PATH + NAME]\"" - + "\n\t\tlastChangeToApply=\"[NUMBER OF THE LAST SCRIPT TO APPLY]\"" + + "\n\t\ttargetVersion=\"[TARGET VERSION OF DATABASE]\"" + "\n\t\tundoOutputfile=\"[UNDO SCRIPT PATH + NAME]\"" + "\n\t\tchangeLogTableName=\"[CHANGE LOG TABLE NAME]\"" + "\n\t\tdelimiter=\"[STATEMENT DELIMITER - default ;]\"" @@ -69,8 +69,8 @@ public void setDbms(String dbms) { dbDeploy.setDbms(dbms); } - public void setLastChangeToApply(Integer lastChangeToApply) { - dbDeploy.setLastChangeToApply(lastChangeToApply); + public void setTargetVersion(Integer targetVersion) { + dbDeploy.setTargetVersion(targetVersion); } public void setUndoOutputfile(File undoOutputfile) { diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/ChangeScriptApplier.java b/dbdeploy-core/src/main/java/com/dbdeploy/ChangeScriptApplier.java deleted file mode 100644 index 41b8154..0000000 --- a/dbdeploy-core/src/main/java/com/dbdeploy/ChangeScriptApplier.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.dbdeploy; - -import com.dbdeploy.scripts.ChangeScript; - -import java.util.List; - -public interface ChangeScriptApplier { - void apply(List changeScript); -} diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/Controller.java b/dbdeploy-core/src/main/java/com/dbdeploy/Controller.java index b45f47e..5d7868f 100644 --- a/dbdeploy-core/src/main/java/com/dbdeploy/Controller.java +++ b/dbdeploy-core/src/main/java/com/dbdeploy/Controller.java @@ -1,5 +1,7 @@ package com.dbdeploy; +import com.dbdeploy.appliers.ApplyMode; +import com.dbdeploy.appliers.ChangeScriptApplier; import com.dbdeploy.exceptions.DbDeployException; import com.dbdeploy.scripts.ChangeScript; @@ -13,59 +15,81 @@ public class Controller { private final AvailableChangeScriptsProvider availableChangeScriptsProvider; private final AppliedChangesProvider appliedChangesProvider; private final ChangeScriptApplier changeScriptApplier; - private final ChangeScriptApplier undoScriptApplier; private final PrettyPrinter prettyPrinter = new PrettyPrinter(); public Controller(AvailableChangeScriptsProvider availableChangeScriptsProvider, AppliedChangesProvider appliedChangesProvider, - ChangeScriptApplier changeScriptApplier, ChangeScriptApplier undoScriptApplier) { + ChangeScriptApplier changeScriptApplier) { this.availableChangeScriptsProvider = availableChangeScriptsProvider; this.appliedChangesProvider = appliedChangesProvider; this.changeScriptApplier = changeScriptApplier; - this.undoScriptApplier = undoScriptApplier; } - public void processChangeScripts(Integer lastChangeToApply) throws DbDeployException, IOException { - if (lastChangeToApply != Integer.MAX_VALUE) { - info("Only applying changes up and including change script #" + lastChangeToApply); - } - - List scripts = availableChangeScriptsProvider.getAvailableChangeScripts(); - List applied = appliedChangesProvider.getAppliedChanges(); - List toApply = identifyChangesToApply(lastChangeToApply, scripts, applied); - - logStatus(scripts, applied, toApply); - - changeScriptApplier.apply(Collections.unmodifiableList(toApply)); - - if (undoScriptApplier != null) { - info("Generating undo scripts..."); - Collections.reverse(toApply); - undoScriptApplier.apply(Collections.unmodifiableList(toApply)); - } - } - - private void logStatus(List scripts, List applied, List toApply) { - info("Changes currently applied to database:\n " + prettyPrinter.format(applied)); - info("Scripts available:\n " + prettyPrinter.formatChangeScriptList(scripts)); - info("To be applied:\n " + prettyPrinter.formatChangeScriptList(toApply)); + public void processChangeScripts(int targetVersion) throws DbDeployException, IOException { + if (targetVersion == Integer.MAX_VALUE) + info("Upgrading database to the latest and greatest"); + else + info("Target version of database: " + targetVersion); + + + List availableScripts = availableChangeScriptsProvider.getAvailableChangeScripts(); + List appliedScripts = appliedChangesProvider.getAppliedChanges(); + + int latestScriptApplied = getLatestScriptApplied(appliedScripts); + ApplyMode applyMode = getApplyMode(latestScriptApplied, targetVersion); + List toApply = identifyChangesToApply(targetVersion, appliedScripts, availableScripts, applyMode); + + if (applyMode == ApplyMode.UNDO) Collections.reverse(toApply); + logStatus(availableScripts, appliedScripts, toApply, applyMode); + changeScriptApplier.apply(Collections.unmodifiableList(toApply), applyMode); } - private List identifyChangesToApply(Integer lastChangeToApply, List scripts, List applied) { - List result = new ArrayList(); + /** + * returns the current version of the database + */ + private int getLatestScriptApplied(List applied) { + int latestScriptApplied = 0; - for (ChangeScript script : scripts) { - if (script.getId() > lastChangeToApply) - break; - - if (!applied.contains(script.getId())) { - result.add(script); - } + for (Integer i : applied) { + if (i > latestScriptApplied) + latestScriptApplied = i; + } + + return latestScriptApplied; + } + + private List identifyChangesToApply(int targetVersion, List appliedScripts + , List availableScripts, ApplyMode applyMode) { + List changesToApply = new ArrayList(); + + for (ChangeScript script : availableScripts) { + if (applyMode == ApplyMode.DO) { + if (script.getId() <= targetVersion && !appliedScripts.contains(script.getId())) + changesToApply.add(script); + } + else if (applyMode == ApplyMode.UNDO) { + if (script.getId() > targetVersion && appliedScripts.contains(script.getId())) + changesToApply.add(script); + } } - return result; + return changesToApply; } + + private ApplyMode getApplyMode(int currentVersion, int targetVersion) { + if (currentVersion <= targetVersion) + return ApplyMode.DO; + else + return ApplyMode.UNDO; + } + + private void logStatus(List scripts, List applied, List toApply, ApplyMode applyMode) { + info("Changes currently applied to database:\n " + prettyPrinter.format(applied)); + info("Scripts available:\n " + prettyPrinter.formatChangeScriptList(scripts)); + info("To be applied:\n " + prettyPrinter.formatChangeScriptList(toApply)); + info("Apply mode: " + applyMode); + } private void info(String string) { System.err.println(string); diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java b/dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java index a63b2e0..be914d6 100644 --- a/dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java +++ b/dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java @@ -1,8 +1,10 @@ package com.dbdeploy; +import java.io.File; + +import com.dbdeploy.appliers.ChangeScriptApplier; import com.dbdeploy.appliers.DirectToDbApplier; import com.dbdeploy.appliers.TemplateBasedApplier; -import com.dbdeploy.appliers.UndoTemplateBasedApplier; import com.dbdeploy.database.DelimiterType; import com.dbdeploy.database.QueryStatementSplitter; import com.dbdeploy.database.changelog.DatabaseSchemaVersionManager; @@ -11,9 +13,6 @@ import com.dbdeploy.scripts.ChangeScriptRepository; import com.dbdeploy.scripts.DirectoryScanner; -import java.io.File; -import java.io.PrintStream; - public class DbDeploy { private String url; private String userid; @@ -22,7 +21,7 @@ public class DbDeploy { private File outputfile; private File undoOutputfile; private String dbms; - private Integer lastChangeToApply = Integer.MAX_VALUE; + private Integer targetVersion = Integer.MAX_VALUE; // based on the target version we decide if we need to upgrade or downgrade the database private String driver; private String changeLogTableName = "changelog"; private String delimiter = ";"; @@ -57,8 +56,8 @@ public void setDbms(String dbms) { this.dbms = dbms; } - public void setLastChangeToApply(Integer lastChangeToApply) { - this.lastChangeToApply = lastChangeToApply; + public void setTargetVersion(Integer targetVersion) { + this.targetVersion = targetVersion; } public void setUndoOutputfile(File undoOutputfile) { @@ -71,9 +70,7 @@ public void setChangeLogTableName(String changeLogTableName) { public void go() throws Exception { System.err.println(getWelcomeString()); - validate(); - Class.forName(driver); QueryExecuter queryExecuter = new QueryExecuter(url, userid, password); @@ -87,9 +84,8 @@ public void go() throws Exception { ChangeScriptApplier doScriptApplier; if (outputfile != null) { - doScriptApplier = new TemplateBasedApplier( - new PrintStream(outputfile), dbms, - changeLogTableName, getTemplatedir()); + doScriptApplier = new TemplateBasedApplier(outputfile, undoOutputfile, + dbms, changeLogTableName, getTemplatedir()); } else { QueryStatementSplitter splitter = new QueryStatementSplitter(); splitter.setDelimiter(getDelimiter()); @@ -97,18 +93,9 @@ public void go() throws Exception { doScriptApplier = new DirectToDbApplier(queryExecuter, databaseSchemaVersionManager, splitter); } - ChangeScriptApplier undoScriptApplier = null; - - if (undoOutputfile != null) { - undoScriptApplier = - new UndoTemplateBasedApplier( - new PrintStream(undoOutputfile), dbms, changeLogTableName, getTemplatedir()); - - } - - Controller controller = new Controller(changeScriptRepository, databaseSchemaVersionManager, doScriptApplier, undoScriptApplier); + Controller controller = new Controller(changeScriptRepository, databaseSchemaVersionManager, doScriptApplier); - controller.processChangeScripts(lastChangeToApply); + controller.processChangeScripts(targetVersion); queryExecuter.close(); } @@ -164,8 +151,8 @@ public String getDbms() { return dbms; } - public Integer getLastChangeToApply() { - return lastChangeToApply; + public Integer getTargetVersion() { + return targetVersion; } public String getDriver() { diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/DirectToDbApplier.java b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/DirectToDbApplier.java index 96b170e..e3cca0e 100644 --- a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/DirectToDbApplier.java +++ b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/DirectToDbApplier.java @@ -1,6 +1,5 @@ package com.dbdeploy.appliers; -import com.dbdeploy.ChangeScriptApplier; import com.dbdeploy.database.QueryStatementSplitter; import com.dbdeploy.database.changelog.DatabaseSchemaVersionManager; import com.dbdeploy.database.changelog.QueryExecuter; @@ -20,15 +19,18 @@ public DirectToDbApplier(QueryExecuter queryExecuter, DatabaseSchemaVersionManag this.splitter = splitter; } - public void apply(List changeScript) { + public void apply(List changeScript, ApplyMode applyMode) { begin(); for (ChangeScript script : changeScript) { System.err.println("Applying " + script + "..."); - applyChangeScriptContent(script.getContent()); + if (applyMode == ApplyMode.DO) + applyChangeScriptContent(script.getContent()); + else if (applyMode == ApplyMode.UNDO) + applyChangeScriptContent(script.getUndoContent()); + insertToSchemaVersionTable(script); - commitTransaction(); } } diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/TemplateBasedApplier.java b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/TemplateBasedApplier.java index 3fb553b..630a296 100644 --- a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/TemplateBasedApplier.java +++ b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/TemplateBasedApplier.java @@ -1,6 +1,5 @@ package com.dbdeploy.appliers; -import com.dbdeploy.ChangeScriptApplier; import com.dbdeploy.exceptions.UsageException; import com.dbdeploy.scripts.ChangeScript; import freemarker.cache.ClassTemplateLoader; @@ -19,13 +18,15 @@ public class TemplateBasedApplier implements ChangeScriptApplier { private Configuration configuration; private Writer writer; + private Writer undoWriter; private String syntax; private String changeLogTableName; - public TemplateBasedApplier(OutputStream outputStream, String syntax, String changeLogTableName, File templateDirectory) throws IOException { + public TemplateBasedApplier(File outputfile, File undoOutputfile, String syntax, String changeLogTableName, File templateDirectory) throws IOException { this.syntax = syntax; this.changeLogTableName = changeLogTableName; - this.writer = new PrintWriter(outputStream); + this.writer = new PrintWriter(new PrintStream(outputfile)); + if (undoOutputfile != null) this.undoWriter= new PrintWriter(new PrintStream(undoOutputfile)); this.configuration = new Configuration(); FileTemplateLoader fileTemplateLoader = createFileTemplateLoader(templateDirectory); @@ -36,7 +37,7 @@ public TemplateBasedApplier(OutputStream outputStream, String syntax, String cha })); } - private FileTemplateLoader createFileTemplateLoader(File templateDirectory) throws IOException { + private FileTemplateLoader createFileTemplateLoader(File templateDirectory) throws IOException { if (templateDirectory == null) { return new FileTemplateLoader(); } else { @@ -44,8 +45,8 @@ private FileTemplateLoader createFileTemplateLoader(File templateDirectory) thro } } - public void apply(List changeScripts) { - String filename = syntax + "_" + getTemplateQualifier() + ".ftl"; + public void apply(List changeScripts, ApplyMode applyMode) { + String filename = syntax + "_" + getTemplateQualifier(applyMode) + ".ftl"; try { Map model = new HashMap(); @@ -53,7 +54,10 @@ public void apply(List changeScripts) { model.put("changeLogTableName", changeLogTableName); Template template = configuration.getTemplate(filename); - template.process(model, writer); + if (applyMode == ApplyMode.DO) + template.process(model, writer); + else if (undoWriter != null) + template.process(model, undoWriter); } catch (FileNotFoundException ex) { throw new UsageException("Could not find template named " + filename + "\n" + "Check that you have got the name of the database syntax correct.", ex); @@ -62,8 +66,11 @@ public void apply(List changeScripts) { } } - protected String getTemplateQualifier() { - return "apply"; + protected String getTemplateQualifier(ApplyMode applyMode) { + if (applyMode == ApplyMode.UNDO) + return "undo"; + else + return "apply"; } } diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/UndoTemplateBasedApplier.java b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/UndoTemplateBasedApplier.java deleted file mode 100644 index 4739823..0000000 --- a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/UndoTemplateBasedApplier.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.dbdeploy.appliers; - -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; - -public class UndoTemplateBasedApplier extends TemplateBasedApplier { - public UndoTemplateBasedApplier(OutputStream outputStream, String syntax, - String changeLogTableName, File templateDirectory) throws IOException { - super(outputStream, syntax, changeLogTableName, templateDirectory); - } - - @Override - protected String getTemplateQualifier() { - return "undo"; - } -} diff --git a/dbdeploy-core/src/test/java/com/dbdeploy/ControllerTest.java b/dbdeploy-core/src/test/java/com/dbdeploy/ControllerTest.java index 3199b07..aca95ee 100644 --- a/dbdeploy-core/src/test/java/com/dbdeploy/ControllerTest.java +++ b/dbdeploy-core/src/test/java/com/dbdeploy/ControllerTest.java @@ -1,5 +1,7 @@ package com.dbdeploy; +import com.dbdeploy.appliers.ApplyMode; +import com.dbdeploy.appliers.ChangeScriptApplier; import com.dbdeploy.scripts.ChangeScript; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -24,13 +26,11 @@ public class ControllerTest { private ChangeScript change1; private ChangeScript change2; private ChangeScript change3; - - private StubChangeScriptApplier applier = new StubChangeScriptApplier(); - private StubChangeScriptApplier undoApplier = new StubChangeScriptApplier(); + private StubChangeScriptApplier applier = new StubChangeScriptApplier(); @Before public void setUp() { - controller = new Controller(availableChangeScriptsProvider, appliedChangesProvider, applier, undoApplier); + controller = new Controller(availableChangeScriptsProvider, appliedChangesProvider, applier); change1 = new ChangeScript(1); change2 = new ChangeScript(2); @@ -54,24 +54,45 @@ public void shouldApplyChangeScriptsInOrder() throws Exception { @Test public void shouldNotCrashWhenPassedANullUndoApplier() throws Exception { - controller = new Controller(availableChangeScriptsProvider, appliedChangesProvider, applier, null); - - when(appliedChangesProvider.getAppliedChanges()).thenReturn(Collections.emptyList()); + controller = new Controller(availableChangeScriptsProvider, appliedChangesProvider, applier); + when(appliedChangesProvider.getAppliedChanges()).thenReturn(Collections.emptyList()); controller.processChangeScripts(Integer.MAX_VALUE); } @Test public void shouldApplyUndoScriptsInReverseOrder() throws Exception { - when(appliedChangesProvider.getAppliedChanges()).thenReturn(Collections.emptyList()); + when(appliedChangesProvider.getAppliedChanges()).thenReturn(Arrays.asList(1,2,3)); - controller.processChangeScripts(Integer.MAX_VALUE); + controller.processChangeScripts(0); - assertThat(undoApplier.changeScripts.size(), is(3)); - assertThat(undoApplier.changeScripts.get(0), is(change3)); - assertThat(undoApplier.changeScripts.get(1), is(change2)); - assertThat(undoApplier.changeScripts.get(2), is(change1)); + assertThat(applier.changeScripts.size(), is(3)); + assertThat(applier.changeScripts.get(0), is(change3)); + assertThat(applier.changeScripts.get(1), is(change2)); + assertThat(applier.changeScripts.get(2), is(change1)); } + + @Test + public void shouldApplyPartialUndoScriptsInReverseOrder() throws Exception { + when(appliedChangesProvider.getAppliedChanges()).thenReturn(Arrays.asList(1,2,3)); + + controller.processChangeScripts(1); + + assertThat(applier.changeScripts.size(), is(2)); + assertThat(applier.changeScripts.get(0), is(change3)); + assertThat(applier.changeScripts.get(1), is(change2)); + } + + @Test + public void shouldApplyPartialUndoScriptsInReverseOrder2() throws Exception { + when(appliedChangesProvider.getAppliedChanges()).thenReturn(Arrays.asList(1,3)); + + controller.processChangeScripts(0); + + assertThat(applier.changeScripts.size(), is(2)); + assertThat(applier.changeScripts.get(0), is(change3)); + assertThat(applier.changeScripts.get(1), is(change1)); + } @Test @@ -97,12 +118,13 @@ public void shouldNotApplyChangesGreaterThanTheMaxChangeToApply() throws Excepti } - - private class StubChangeScriptApplier implements ChangeScriptApplier { + private class StubChangeScriptApplier implements ChangeScriptApplier { private List changeScripts; + private ApplyMode applyMode; - public void apply(List changeScripts) { + public void apply(List changeScripts, ApplyMode applyMode) { this.changeScripts = new ArrayList(changeScripts); + this.applyMode = applyMode; } } diff --git a/dbdeploy-core/src/test/java/com/dbdeploy/appliers/TemplateBasedApplierTest.java b/dbdeploy-core/src/test/java/com/dbdeploy/appliers/TemplateBasedApplierTest.java index e555973..6a589a8 100644 --- a/dbdeploy-core/src/test/java/com/dbdeploy/appliers/TemplateBasedApplierTest.java +++ b/dbdeploy-core/src/test/java/com/dbdeploy/appliers/TemplateBasedApplierTest.java @@ -1,19 +1,22 @@ package com.dbdeploy.appliers; -import com.dbdeploy.exceptions.UsageException; -import org.apache.commons.io.output.NullOutputStream; import static org.hamcrest.Matchers.is; -import org.junit.Assert; import static org.junit.Assert.assertThat; + +import java.io.File; + +import org.junit.Assert; import org.junit.Test; +import com.dbdeploy.exceptions.UsageException; + public class TemplateBasedApplierTest { @Test public void shouldThrowUsageExceptionWhenTemplateNotFound() throws Exception { - TemplateBasedApplier applier = new TemplateBasedApplier(new NullOutputStream(), "some_complete_rubbish", null, null); + TemplateBasedApplier applier = new TemplateBasedApplier(new File("outfile"), null, "some_complete_rubbish", null, null); try { - applier.apply(null); + applier.apply(null, null); Assert.fail("expected exception"); } catch (UsageException e) { assertThat(e.getMessage(), is("Could not find template named some_complete_rubbish_apply.ftl\n" + diff --git a/dbdeploy-core/src/test/java/com/dbdeploy/database/ScriptGenerationTest.java b/dbdeploy-core/src/test/java/com/dbdeploy/database/ScriptGenerationTest.java index 7f7e571..a5abdb3 100644 --- a/dbdeploy-core/src/test/java/com/dbdeploy/database/ScriptGenerationTest.java +++ b/dbdeploy-core/src/test/java/com/dbdeploy/database/ScriptGenerationTest.java @@ -1,108 +1,109 @@ -package com.dbdeploy.database; - -import com.dbdeploy.ChangeScriptApplier; -import com.dbdeploy.Controller; -import com.dbdeploy.appliers.TemplateBasedApplier; -import com.dbdeploy.database.changelog.DatabaseSchemaVersionManager; -import com.dbdeploy.exceptions.SchemaVersionTrackingException; -import com.dbdeploy.scripts.ChangeScript; -import com.dbdeploy.scripts.ChangeScriptRepository; -import static org.junit.Assert.assertEquals; -import org.junit.Test; - -import java.io.*; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -public class ScriptGenerationTest { - - @Test - public void generateConsolidatedChangesScriptForAllDatabasesAndCompareAgainstTemplate() throws Exception { - for (String syntax : Arrays.asList("hsql", "mssql", "mysql", "ora", "syb-ase", "db2", "pgsql")) { - try { - System.out.printf("Testing syntax %s\n", syntax); - runIntegratedTestAndConfirmOutputResults(syntax); - } catch (Exception e) { - throw new RuntimeException("Failed while testing syntax " + syntax, e); - } - } - } - - private void runIntegratedTestAndConfirmOutputResults(String syntaxName) throws Exception { - - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - PrintStream printStream = new PrintStream(outputStream); - - ChangeScript changeOne = new StubChangeScript(1, "001_change.sql", "-- contents of change script 1"); - ChangeScript changeTwo = new StubChangeScript(2, "002_change.sql", "-- contents of change script 2"); - - List changeScripts = Arrays.asList(changeOne, changeTwo); - ChangeScriptRepository changeScriptRepository = new ChangeScriptRepository(changeScripts); - - - - final StubSchemaManager schemaManager = new StubSchemaManager(); - ChangeScriptApplier applier = new TemplateBasedApplier(printStream, syntaxName, "changelog", null); - Controller controller = new Controller(changeScriptRepository, schemaManager, applier, null); - - controller.processChangeScripts(Integer.MAX_VALUE); - - assertEquals(readExpectedFileContents(getExpectedFilename(syntaxName)), outputStream.toString()); - } - - private String getExpectedFilename(String dbSyntaxName) { - return dbSyntaxName + "_expected.sql"; - } - - private String readExpectedFileContents(String expectedFilename) throws IOException { - final InputStream stream = getClass().getResourceAsStream(expectedFilename); - BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - try { - return readEntireStreamIntoAStringWithConversionToSystemDependantLineTerminators(reader); - } finally { - reader.close(); - } - } - - private String readEntireStreamIntoAStringWithConversionToSystemDependantLineTerminators(BufferedReader reader) throws IOException { - StringWriter contentWithSystemDependentLineTerminators = new StringWriter(); - PrintWriter newLineConvertingContentWriter = new PrintWriter(contentWithSystemDependentLineTerminators); - try { - String line; - while ((line = reader.readLine()) != null) { - newLineConvertingContentWriter.println(line); - } - newLineConvertingContentWriter.flush(); - return contentWithSystemDependentLineTerminators.toString(); - } finally { - newLineConvertingContentWriter.close(); - } - } - - - private class StubSchemaManager extends DatabaseSchemaVersionManager { - public StubSchemaManager() { - super(null, "changelog"); - } - - @Override - public List getAppliedChanges() throws SchemaVersionTrackingException { - return Collections.emptyList(); - } - } - - private class StubChangeScript extends ChangeScript { - private final String changeContents; - - public StubChangeScript(int changeNumber, String description, String changeContents) { - super(changeNumber, description); - this.changeContents = changeContents; - } - - @Override - public String getContent() { - return changeContents; - } - } -} +//package com.dbdeploy.database; +// +//import com.dbdeploy.Controller; +//import com.dbdeploy.appliers.ChangeScriptApplier; +//import com.dbdeploy.appliers.TemplateBasedApplier; +//import com.dbdeploy.database.changelog.DatabaseSchemaVersionManager; +//import com.dbdeploy.exceptions.SchemaVersionTrackingException; +//import com.dbdeploy.scripts.ChangeScript; +//import com.dbdeploy.scripts.ChangeScriptRepository; +//import static org.junit.Assert.assertEquals; +//import org.junit.Test; +// +//import java.io.*; +//import java.util.Arrays; +//import java.util.Collections; +//import java.util.List; +// +//// TODO MP enable again +//public class ScriptGenerationTest { +// +// @Test +// public void generateConsolidatedChangesScriptForAllDatabasesAndCompareAgainstTemplate() throws Exception { +// for (String syntax : Arrays.asList("hsql", "mssql", "mysql", "ora", "syb-ase", "db2", "pgsql")) { +// try { +// System.out.printf("Testing syntax %s\n", syntax); +// runIntegratedTestAndConfirmOutputResults(syntax); +// } catch (Exception e) { +// throw new RuntimeException("Failed while testing syntax " + syntax, e); +// } +// } +// } +// +// private void runIntegratedTestAndConfirmOutputResults(String syntaxName) throws Exception { +// +// ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); +// PrintStream printStream = new PrintStream(outputStream); +// +// ChangeScript changeOne = new StubChangeScript(1, "001_change.sql", "-- contents of change script 1"); +// ChangeScript changeTwo = new StubChangeScript(2, "002_change.sql", "-- contents of change script 2"); +// +// List changeScripts = Arrays.asList(changeOne, changeTwo); +// ChangeScriptRepository changeScriptRepository = new ChangeScriptRepository(changeScripts); +// +// +// +// final StubSchemaManager schemaManager = new StubSchemaManager(); +// ChangeScriptApplier applier = new TemplateBasedApplier(printStream, syntaxName, "changelog", null); +// Controller controller = new Controller(changeScriptRepository, schemaManager, applier, null); +// +// controller.processChangeScripts(Integer.MAX_VALUE); +// +// assertEquals(readExpectedFileContents(getExpectedFilename(syntaxName)), outputStream.toString()); +// } +// +// private String getExpectedFilename(String dbSyntaxName) { +// return dbSyntaxName + "_expected.sql"; +// } +// +// private String readExpectedFileContents(String expectedFilename) throws IOException { +// final InputStream stream = getClass().getResourceAsStream(expectedFilename); +// BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); +// try { +// return readEntireStreamIntoAStringWithConversionToSystemDependantLineTerminators(reader); +// } finally { +// reader.close(); +// } +// } +// +// private String readEntireStreamIntoAStringWithConversionToSystemDependantLineTerminators(BufferedReader reader) throws IOException { +// StringWriter contentWithSystemDependentLineTerminators = new StringWriter(); +// PrintWriter newLineConvertingContentWriter = new PrintWriter(contentWithSystemDependentLineTerminators); +// try { +// String line; +// while ((line = reader.readLine()) != null) { +// newLineConvertingContentWriter.println(line); +// } +// newLineConvertingContentWriter.flush(); +// return contentWithSystemDependentLineTerminators.toString(); +// } finally { +// newLineConvertingContentWriter.close(); +// } +// } +// +// +// private class StubSchemaManager extends DatabaseSchemaVersionManager { +// public StubSchemaManager() { +// super(null, "changelog"); +// } +// +// @Override +// public List getAppliedChanges() throws SchemaVersionTrackingException { +// return Collections.emptyList(); +// } +// } +// +// private class StubChangeScript extends ChangeScript { +// private final String changeContents; +// +// public StubChangeScript(int changeNumber, String description, String changeContents) { +// super(changeNumber, description); +// this.changeContents = changeContents; +// } +// +// @Override +// public String getContent() { +// return changeContents; +// } +// } +//} From b771d41234c20653ca68f21bf5c4c6bf98ca6e9e Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Fri, 3 Dec 2010 09:01:38 +0000 Subject: [PATCH 4/5] added changeScriptApplier --- .../java/com/dbdeploy/appliers/ChangeScriptApplier.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 dbdeploy-core/src/main/java/com/dbdeploy/appliers/ChangeScriptApplier.java diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/ChangeScriptApplier.java b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/ChangeScriptApplier.java new file mode 100644 index 0000000..3472264 --- /dev/null +++ b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/ChangeScriptApplier.java @@ -0,0 +1,9 @@ +package com.dbdeploy.appliers; + +import com.dbdeploy.scripts.ChangeScript; + +import java.util.List; + +public interface ChangeScriptApplier { + void apply(List changeScript, ApplyMode applyMode); +} From 6f7e7dc80918e3fa409ea0d425b5b039b89d57a4 Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Fri, 3 Dec 2010 11:12:55 +0000 Subject: [PATCH 5/5] changed api not to have undo file, not needed as we have the notion of targetVersion now --- .../src/main/java/com/dbdeploy/AntTarget.java | 5 - .../src/main/java/com/dbdeploy/DbDeploy.java | 12 +- .../dbdeploy/appliers/DirectToDbApplier.java | 9 +- .../appliers/TemplateBasedApplier.java | 13 +- .../DatabaseSchemaVersionManager.java | 13 +- .../appliers/DirectToDbApplierTest.java | 2 +- .../appliers/TemplateBasedApplierTest.java | 5 +- .../database/ScriptGenerationTest.java | 218 +++++++++--------- dbdeploy-dist/src/main/example/build.xml | 34 ++- 9 files changed, 169 insertions(+), 142 deletions(-) diff --git a/dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java b/dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java index d7bdb8c..0097f9b 100644 --- a/dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java +++ b/dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java @@ -22,7 +22,6 @@ public class AntTarget extends Task { + "\n\t\tdir=\"[YOUR SCRIPT FOLDER]\" *" + "\n\t\toutputfile=\"[OUTPUT SCRIPT PATH + NAME]\"" + "\n\t\ttargetVersion=\"[TARGET VERSION OF DATABASE]\"" - + "\n\t\tundoOutputfile=\"[UNDO SCRIPT PATH + NAME]\"" + "\n\t\tchangeLogTableName=\"[CHANGE LOG TABLE NAME]\"" + "\n\t\tdelimiter=\"[STATEMENT DELIMITER - default ;]\"" + "\n\t\tdelimitertype=\"[STATEMENT DELIMITER TYPE - row or normal, default normal]\"" @@ -73,10 +72,6 @@ public void setTargetVersion(Integer targetVersion) { dbDeploy.setTargetVersion(targetVersion); } - public void setUndoOutputfile(File undoOutputfile) { - dbDeploy.setUndoOutputfile(undoOutputfile); - } - public void setChangeLogTableName(String changeLogTableName) { dbDeploy.setChangeLogTableName(changeLogTableName); } diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java b/dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java index be914d6..27ae479 100644 --- a/dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java +++ b/dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java @@ -19,7 +19,6 @@ public class DbDeploy { private String password; private File scriptdirectory; private File outputfile; - private File undoOutputfile; private String dbms; private Integer targetVersion = Integer.MAX_VALUE; // based on the target version we decide if we need to upgrade or downgrade the database private String driver; @@ -60,10 +59,6 @@ public void setTargetVersion(Integer targetVersion) { this.targetVersion = targetVersion; } - public void setUndoOutputfile(File undoOutputfile) { - this.undoOutputfile = undoOutputfile; - } - public void setChangeLogTableName(String changeLogTableName) { this.changeLogTableName = changeLogTableName; } @@ -84,8 +79,7 @@ public void go() throws Exception { ChangeScriptApplier doScriptApplier; if (outputfile != null) { - doScriptApplier = new TemplateBasedApplier(outputfile, undoOutputfile, - dbms, changeLogTableName, getTemplatedir()); + doScriptApplier = new TemplateBasedApplier(outputfile, dbms, changeLogTableName, getTemplatedir()); } else { QueryStatementSplitter splitter = new QueryStatementSplitter(); splitter.setDelimiter(getDelimiter()); @@ -143,10 +137,6 @@ public File getOutputfile() { return outputfile; } - public File getUndoOutputfile() { - return undoOutputfile; - } - public String getDbms() { return dbms; } diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/DirectToDbApplier.java b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/DirectToDbApplier.java index e3cca0e..810c939 100644 --- a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/DirectToDbApplier.java +++ b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/DirectToDbApplier.java @@ -30,7 +30,7 @@ public void apply(List changeScript, ApplyMode applyMode) { else if (applyMode == ApplyMode.UNDO) applyChangeScriptContent(script.getUndoContent()); - insertToSchemaVersionTable(script); + insertToSchemaVersionTable(script, applyMode); commitTransaction(); } } @@ -53,8 +53,11 @@ protected void applyChangeScriptContent(String scriptContent) { } } - protected void insertToSchemaVersionTable(ChangeScript changeScript) { - schemaVersionManager.recordScriptApplied(changeScript); + protected void insertToSchemaVersionTable(ChangeScript changeScript, ApplyMode applyMode) { + if (applyMode == ApplyMode.DO) + schemaVersionManager.recordScriptApplied(changeScript); + else if (applyMode == ApplyMode.UNDO) + schemaVersionManager.recordUndoScriptApplied(changeScript); } protected void commitTransaction() { diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/TemplateBasedApplier.java b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/TemplateBasedApplier.java index 630a296..d7a9bdb 100644 --- a/dbdeploy-core/src/main/java/com/dbdeploy/appliers/TemplateBasedApplier.java +++ b/dbdeploy-core/src/main/java/com/dbdeploy/appliers/TemplateBasedApplier.java @@ -17,16 +17,14 @@ public class TemplateBasedApplier implements ChangeScriptApplier { private Configuration configuration; - private Writer writer; - private Writer undoWriter; + private Writer outputFileWriter; private String syntax; private String changeLogTableName; - public TemplateBasedApplier(File outputfile, File undoOutputfile, String syntax, String changeLogTableName, File templateDirectory) throws IOException { + public TemplateBasedApplier(File outputfile, String syntax, String changeLogTableName, File templateDirectory) throws IOException { this.syntax = syntax; this.changeLogTableName = changeLogTableName; - this.writer = new PrintWriter(new PrintStream(outputfile)); - if (undoOutputfile != null) this.undoWriter= new PrintWriter(new PrintStream(undoOutputfile)); + this.outputFileWriter = new PrintWriter(new PrintStream(outputfile)); this.configuration = new Configuration(); FileTemplateLoader fileTemplateLoader = createFileTemplateLoader(templateDirectory); @@ -54,10 +52,7 @@ public void apply(List changeScripts, ApplyMode applyMode) { model.put("changeLogTableName", changeLogTableName); Template template = configuration.getTemplate(filename); - if (applyMode == ApplyMode.DO) - template.process(model, writer); - else if (undoWriter != null) - template.process(model, undoWriter); + template.process(model, outputFileWriter); } catch (FileNotFoundException ex) { throw new UsageException("Could not find template named " + filename + "\n" + "Check that you have got the name of the database syntax correct.", ex); diff --git a/dbdeploy-core/src/main/java/com/dbdeploy/database/changelog/DatabaseSchemaVersionManager.java b/dbdeploy-core/src/main/java/com/dbdeploy/database/changelog/DatabaseSchemaVersionManager.java index 43d3fc1..195ea59 100644 --- a/dbdeploy-core/src/main/java/com/dbdeploy/database/changelog/DatabaseSchemaVersionManager.java +++ b/dbdeploy-core/src/main/java/com/dbdeploy/database/changelog/DatabaseSchemaVersionManager.java @@ -62,8 +62,17 @@ public void recordScriptApplied(ChangeScript script) { script.getDescription() ); } catch (SQLException e) { - throw new SchemaVersionTrackingException("Could not update change log because: " - + e.getMessage(), e); + throw new SchemaVersionTrackingException("Could not update changelog!", e); + } + } + + public void recordUndoScriptApplied(ChangeScript script) { + try { + queryExecuter.execute( + "DELETE FROM " + changeLogTableName + " WHERE change_number = ?", + script.getId()); + } catch (SQLException e) { + throw new SchemaVersionTrackingException("Could not remove entry from changelog!", e); } } diff --git a/dbdeploy-core/src/test/java/com/dbdeploy/appliers/DirectToDbApplierTest.java b/dbdeploy-core/src/test/java/com/dbdeploy/appliers/DirectToDbApplierTest.java index 3dbd485..4bb68d9 100644 --- a/dbdeploy-core/src/test/java/com/dbdeploy/appliers/DirectToDbApplierTest.java +++ b/dbdeploy-core/src/test/java/com/dbdeploy/appliers/DirectToDbApplierTest.java @@ -47,7 +47,7 @@ public void shouldApplyChangeScriptBySplittingContentUsingTheSplitter() throws E public void shouldInsertToSchemaVersionTable() throws Exception { ChangeScript changeScript = new ChangeScript(1, "script.sql"); - applier.insertToSchemaVersionTable(changeScript); + applier.insertToSchemaVersionTable(changeScript, ApplyMode.DO); verify(schemaVersionManager).recordScriptApplied(changeScript); diff --git a/dbdeploy-core/src/test/java/com/dbdeploy/appliers/TemplateBasedApplierTest.java b/dbdeploy-core/src/test/java/com/dbdeploy/appliers/TemplateBasedApplierTest.java index 6a589a8..82fd669 100644 --- a/dbdeploy-core/src/test/java/com/dbdeploy/appliers/TemplateBasedApplierTest.java +++ b/dbdeploy-core/src/test/java/com/dbdeploy/appliers/TemplateBasedApplierTest.java @@ -14,7 +14,10 @@ public class TemplateBasedApplierTest { @Test public void shouldThrowUsageExceptionWhenTemplateNotFound() throws Exception { - TemplateBasedApplier applier = new TemplateBasedApplier(new File("outfile"), null, "some_complete_rubbish", null, null); + File outfile = new File("outfile"); + outfile.deleteOnExit(); + + TemplateBasedApplier applier = new TemplateBasedApplier(outfile , "some_complete_rubbish", null, null); try { applier.apply(null, null); Assert.fail("expected exception"); diff --git a/dbdeploy-core/src/test/java/com/dbdeploy/database/ScriptGenerationTest.java b/dbdeploy-core/src/test/java/com/dbdeploy/database/ScriptGenerationTest.java index a5abdb3..4568cf2 100644 --- a/dbdeploy-core/src/test/java/com/dbdeploy/database/ScriptGenerationTest.java +++ b/dbdeploy-core/src/test/java/com/dbdeploy/database/ScriptGenerationTest.java @@ -1,109 +1,109 @@ -//package com.dbdeploy.database; -// -//import com.dbdeploy.Controller; -//import com.dbdeploy.appliers.ChangeScriptApplier; -//import com.dbdeploy.appliers.TemplateBasedApplier; -//import com.dbdeploy.database.changelog.DatabaseSchemaVersionManager; -//import com.dbdeploy.exceptions.SchemaVersionTrackingException; -//import com.dbdeploy.scripts.ChangeScript; -//import com.dbdeploy.scripts.ChangeScriptRepository; -//import static org.junit.Assert.assertEquals; -//import org.junit.Test; -// -//import java.io.*; -//import java.util.Arrays; -//import java.util.Collections; -//import java.util.List; -// -//// TODO MP enable again -//public class ScriptGenerationTest { -// -// @Test -// public void generateConsolidatedChangesScriptForAllDatabasesAndCompareAgainstTemplate() throws Exception { -// for (String syntax : Arrays.asList("hsql", "mssql", "mysql", "ora", "syb-ase", "db2", "pgsql")) { -// try { -// System.out.printf("Testing syntax %s\n", syntax); -// runIntegratedTestAndConfirmOutputResults(syntax); -// } catch (Exception e) { -// throw new RuntimeException("Failed while testing syntax " + syntax, e); -// } -// } -// } -// -// private void runIntegratedTestAndConfirmOutputResults(String syntaxName) throws Exception { -// -// ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); -// PrintStream printStream = new PrintStream(outputStream); -// -// ChangeScript changeOne = new StubChangeScript(1, "001_change.sql", "-- contents of change script 1"); -// ChangeScript changeTwo = new StubChangeScript(2, "002_change.sql", "-- contents of change script 2"); -// -// List changeScripts = Arrays.asList(changeOne, changeTwo); -// ChangeScriptRepository changeScriptRepository = new ChangeScriptRepository(changeScripts); -// -// -// -// final StubSchemaManager schemaManager = new StubSchemaManager(); -// ChangeScriptApplier applier = new TemplateBasedApplier(printStream, syntaxName, "changelog", null); -// Controller controller = new Controller(changeScriptRepository, schemaManager, applier, null); -// -// controller.processChangeScripts(Integer.MAX_VALUE); -// -// assertEquals(readExpectedFileContents(getExpectedFilename(syntaxName)), outputStream.toString()); -// } -// -// private String getExpectedFilename(String dbSyntaxName) { -// return dbSyntaxName + "_expected.sql"; -// } -// -// private String readExpectedFileContents(String expectedFilename) throws IOException { -// final InputStream stream = getClass().getResourceAsStream(expectedFilename); -// BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); -// try { -// return readEntireStreamIntoAStringWithConversionToSystemDependantLineTerminators(reader); -// } finally { -// reader.close(); -// } -// } -// -// private String readEntireStreamIntoAStringWithConversionToSystemDependantLineTerminators(BufferedReader reader) throws IOException { -// StringWriter contentWithSystemDependentLineTerminators = new StringWriter(); -// PrintWriter newLineConvertingContentWriter = new PrintWriter(contentWithSystemDependentLineTerminators); -// try { -// String line; -// while ((line = reader.readLine()) != null) { -// newLineConvertingContentWriter.println(line); -// } -// newLineConvertingContentWriter.flush(); -// return contentWithSystemDependentLineTerminators.toString(); -// } finally { -// newLineConvertingContentWriter.close(); -// } -// } -// -// -// private class StubSchemaManager extends DatabaseSchemaVersionManager { -// public StubSchemaManager() { -// super(null, "changelog"); -// } -// -// @Override -// public List getAppliedChanges() throws SchemaVersionTrackingException { -// return Collections.emptyList(); -// } -// } -// -// private class StubChangeScript extends ChangeScript { -// private final String changeContents; -// -// public StubChangeScript(int changeNumber, String description, String changeContents) { -// super(changeNumber, description); -// this.changeContents = changeContents; -// } -// -// @Override -// public String getContent() { -// return changeContents; -// } -// } -//} +package com.dbdeploy.database; + +import com.dbdeploy.Controller; +import com.dbdeploy.appliers.ChangeScriptApplier; +import com.dbdeploy.appliers.TemplateBasedApplier; +import com.dbdeploy.database.changelog.DatabaseSchemaVersionManager; +import com.dbdeploy.exceptions.SchemaVersionTrackingException; +import com.dbdeploy.scripts.ChangeScript; +import com.dbdeploy.scripts.ChangeScriptRepository; +import static org.junit.Assert.assertEquals; + +import org.apache.commons.io.FileUtils; +import org.junit.Test; + +import java.io.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class ScriptGenerationTest { + + @Test + public void generateConsolidatedChangesScriptForAllDatabasesAndCompareAgainstTemplate() throws Exception { + for (String syntax : Arrays.asList("hsql", "mssql", "mysql", "ora", "syb-ase", "db2", "pgsql")) { + try { + System.out.printf("Testing syntax %s\n", syntax); + runIntegratedTestAndConfirmOutputResults(syntax); + } catch (Exception e) { + throw new RuntimeException("Failed while testing syntax " + syntax, e); + } + } + } + + private void runIntegratedTestAndConfirmOutputResults(String syntaxName) throws Exception { + + File outputfile = new File("outputfile"); + outputfile.deleteOnExit(); + + ChangeScript changeOne = new StubChangeScript(1, "001_change.sql", "-- contents of change script 1"); + ChangeScript changeTwo = new StubChangeScript(2, "002_change.sql", "-- contents of change script 2"); + + List changeScripts = Arrays.asList(changeOne, changeTwo); + ChangeScriptRepository changeScriptRepository = new ChangeScriptRepository(changeScripts); + + final StubSchemaManager schemaManager = new StubSchemaManager(); + ChangeScriptApplier applier = new TemplateBasedApplier(outputfile, syntaxName, "changelog", null); + Controller controller = new Controller(changeScriptRepository, schemaManager, applier); + + controller.processChangeScripts(Integer.MAX_VALUE); + + String outputfileContent = FileUtils.readFileToString(outputfile); + assertEquals(readExpectedFileContents(getExpectedFilename(syntaxName)), outputfileContent); + } + + private String getExpectedFilename(String dbSyntaxName) { + return dbSyntaxName + "_expected.sql"; + } + + private String readExpectedFileContents(String expectedFilename) throws IOException { + final InputStream stream = getClass().getResourceAsStream(expectedFilename); + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); + try { + return readEntireStreamIntoAStringWithConversionToSystemDependantLineTerminators(reader); + } finally { + reader.close(); + } + } + + private String readEntireStreamIntoAStringWithConversionToSystemDependantLineTerminators(BufferedReader reader) throws IOException { + StringWriter contentWithSystemDependentLineTerminators = new StringWriter(); + PrintWriter newLineConvertingContentWriter = new PrintWriter(contentWithSystemDependentLineTerminators); + try { + String line; + while ((line = reader.readLine()) != null) { + newLineConvertingContentWriter.println(line); + } + newLineConvertingContentWriter.flush(); + return contentWithSystemDependentLineTerminators.toString(); + } finally { + newLineConvertingContentWriter.close(); + } + } + + + private class StubSchemaManager extends DatabaseSchemaVersionManager { + public StubSchemaManager() { + super(null, "changelog"); + } + + @Override + public List getAppliedChanges() throws SchemaVersionTrackingException { + return Collections.emptyList(); + } + } + + private class StubChangeScript extends ChangeScript { + private final String changeContents; + + public StubChangeScript(int changeNumber, String description, String changeContents) { + super(changeNumber, description); + this.changeContents = changeContents; + } + + @Override + public String getContent() { + return changeContents; + } + } +} diff --git a/dbdeploy-dist/src/main/example/build.xml b/dbdeploy-dist/src/main/example/build.xml index 819bbd0..e946ba0 100644 --- a/dbdeploy-dist/src/main/example/build.xml +++ b/dbdeploy-dist/src/main/example/build.xml @@ -58,7 +58,6 @@ password="" dir="." outputfile="output.sql" - undoOutputfile="undo.sql" dbms="hsql" /> @@ -69,6 +68,39 @@ + + + + + + + + + + + + + + + +