From 0a08c82a75b09126784bf7d9bcceffb2ec6d9259 Mon Sep 17 00:00:00 2001 From: Christian Bongiorno Date: Fri, 22 Dec 2017 16:28:10 -0800 Subject: [PATCH 1/6] new hooks working --- .../org/apache/ibatis/migration/Change.java | 7 +++ .../apache/ibatis/migration/Environment.java | 19 +++++++- .../migration/commands/BaseCommand.java | 39 +++++++++++++++++ .../ibatis/migration/commands/NewCommand.java | 43 ++++++++++++++++--- .../migration/hook/FileMigrationHook.java | 4 ++ .../operations/DatabaseOperation.java | 2 +- .../migration/hook/MigrationHookTest.java | 13 ++++++ .../environments/development.properties | 5 ++- .../migration/hook/testdir/hooks/after_new.js | 16 +++++++ .../hook/testdir/hooks/before_new.js | 16 +++++++ 10 files changed, 154 insertions(+), 10 deletions(-) create mode 100644 src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js create mode 100644 src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js diff --git a/src/main/java/org/apache/ibatis/migration/Change.java b/src/main/java/org/apache/ibatis/migration/Change.java index bc817513..0d7c4d58 100644 --- a/src/main/java/org/apache/ibatis/migration/Change.java +++ b/src/main/java/org/apache/ibatis/migration/Change.java @@ -37,6 +37,13 @@ public Change(BigDecimal id, String appliedTimestamp, String description) { this.description = description; } + public Change(BigDecimal id, String appliedTimestamp, String description, String filename) { + this.id = id; + this.appliedTimestamp = appliedTimestamp; + this.description = description; + this.filename = filename; + } + public BigDecimal getId() { return id; } diff --git a/src/main/java/org/apache/ibatis/migration/Environment.java b/src/main/java/org/apache/ibatis/migration/Environment.java index ac384899..53e99620 100644 --- a/src/main/java/org/apache/ibatis/migration/Environment.java +++ b/src/main/java/org/apache/ibatis/migration/Environment.java @@ -52,7 +52,9 @@ private enum SETTING_KEY { hook_before_down, hook_before_each_down, hook_after_each_down, - hook_after_down + hook_after_down, + hook_before_new, + hook_after_new } private static final List SETTING_KEYS; @@ -88,6 +90,8 @@ private enum SETTING_KEY { private final String hookBeforeEachDown; private final String hookAfterEachDown; private final String hookAfterDown; + private final String hookBeforeNew; + private final String hookAfterNew; private final Properties variables = new Properties(); @@ -113,7 +117,9 @@ public Environment(File file) { this.username = prop.getProperty(SETTING_KEY.username.name()); this.password = prop.getProperty(SETTING_KEY.password.name()); - this.hookBeforeUp = prop.getProperty(SETTING_KEY.hook_before_up.name()); + this.hookBeforeNew = prop.getProperty(SETTING_KEY.hook_before_new.name()); + this.hookAfterNew = prop.getProperty(SETTING_KEY.hook_after_new.name()); + this.hookBeforeEachUp = prop.getProperty(SETTING_KEY.hook_before_each_up.name()); this.hookAfterEachUp = prop.getProperty(SETTING_KEY.hook_after_each_up.name()); this.hookAfterUp = prop.getProperty(SETTING_KEY.hook_after_up.name()); @@ -122,6 +128,7 @@ public Environment(File file) { this.hookAfterEachDown = prop.getProperty(SETTING_KEY.hook_after_each_down.name()); this.hookAfterDown = prop.getProperty(SETTING_KEY.hook_after_down.name()); + this.hookBeforeUp = prop.getProperty(SETTING_KEY.hook_before_up.name()); // User defined variables. Set> entries = prop.entrySet(); for (Entry entry : entries) { @@ -197,6 +204,14 @@ public String getPassword() { return password; } + public String getBeforeNewHook() { + return hookBeforeNew; + } + + public String getAfterNewHook() { + return hookAfterNew; + } + public String getHookBeforeUp() { return hookBeforeUp; } diff --git a/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java b/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java index 90e7a5c7..6ec3840d 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.ServiceLoader; import java.util.TimeZone; @@ -315,6 +316,39 @@ protected MigrationLoader getMigrationLoader() { : new FileMigrationLoader(paths.getScriptPath(), env.getScriptCharset(), env.getVariables()); } + protected MigrationHook createNewHook() { + String before = environment().getBeforeNewHook(); + String after = environment().getAfterNewHook(); + if (before == null && after == null) { + return createNullHook(); + } + return createFileMigrationHook(before, after); + } + + protected MigrationHook createNullHook() { + return new MigrationHook() { + @Override + public void before(Map bindingMap) { + + } + + @Override + public void beforeEach(Map bindingMap) { + + } + + @Override + public void afterEach(Map bindingMap) { + + } + + @Override + public void after(Map bindingMap) { + + } + }; + } + protected MigrationHook createUpHook() { String before = environment().getHookBeforeUp(); String beforeEach = environment().getHookBeforeEachUp(); @@ -343,6 +377,11 @@ protected MigrationHook createFileMigrationHook(String before, String beforeEach factory.create(after)); } + protected MigrationHook createFileMigrationHook(String before, String after) { + HookScriptFactory factory = new FileHookScriptFactory(options.getPaths(), environment(), printStream); + return new FileMigrationHook(factory.create(before), factory.create(after)); + } + protected DatabaseOperationOption getDatabaseOperationOption() { DatabaseOperationOption option = new DatabaseOperationOption(); option.setChangelogTable(changelogTable()); diff --git a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java index fbc30ee6..7449d6d9 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java @@ -15,10 +15,23 @@ */ package org.apache.ibatis.migration.commands; +import static org.apache.ibatis.migration.hook.MigrationHook.HOOK_CONTEXT; +import static org.apache.ibatis.migration.operations.DatabaseOperation.getScriptRunner; + +import java.io.File; import java.io.FileNotFoundException; +import java.math.BigDecimal; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; - +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.migration.Change; +import org.apache.ibatis.migration.ConnectionProvider; import org.apache.ibatis.migration.MigrationException; +import org.apache.ibatis.migration.hook.HookContext; +import org.apache.ibatis.migration.hook.MigrationHook; +import org.apache.ibatis.migration.options.DatabaseOperationOption; import org.apache.ibatis.migration.options.SelectedOptions; import org.apache.ibatis.migration.utils.Util; @@ -39,17 +52,33 @@ public void execute(String... params) { Properties variables = new Properties(); variables.setProperty("description", description); existingEnvironmentFile(); - String filename = getNextIDAsString() + "_" + description.replace(' ', '_') + ".sql"; + String nextId = getNextIDAsString(); + String filename = nextId + "_" + description.replace(' ', '_') + ".sql"; + + File templateCopy = Util.file(paths.getScriptPath(), filename); + ConnectionProvider connectionProvider = getConnectionProvider(); + DatabaseOperationOption option = getDatabaseOperationOption(); + ScriptRunner runner = getScriptRunner(connectionProvider, option, printStream); + + MigrationHook hook = createNewHook(); + Map hookBindings = new HashMap(); + Change change = new Change(new BigDecimal(nextId), new Date().toString(), description, + templateCopy.getAbsolutePath()); + + hookBindings.put(HOOK_CONTEXT, new HookContext(connectionProvider, runner, change)); if (options.getTemplate() != null) { - copyExternalResourceTo(options.getTemplate(), Util.file(paths.getScriptPath(), filename), variables); + hook.before(hookBindings); + copyExternalResourceTo(options.getTemplate(), templateCopy, variables); } else { try { String customConfiguredTemplate = getPropertyOption(CUSTOM_NEW_COMMAND_TEMPLATE_PROPERTY); if (customConfiguredTemplate != null) { - copyExternalResourceTo(migrationsHome() + "/" + customConfiguredTemplate, - Util.file(paths.getScriptPath(), filename), variables); + hook.before(hookBindings); + copyExternalResourceTo(migrationsHome() + "/" + customConfiguredTemplate, templateCopy, variables); } else { + change.setFilename(filename); + hook.before(hookBindings); copyDefaultTemplate(variables, filename); } } catch (FileNotFoundException e) { @@ -58,8 +87,12 @@ public void execute(String... params) { copyDefaultTemplate(variables, filename); } } + + hook.after(hookBindings); + printStream.println("Done!"); printStream.println(); + } private void copyDefaultTemplate(Properties variables, String filename) { diff --git a/src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java b/src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java index cc6fce22..72bec97c 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java +++ b/src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java @@ -32,6 +32,10 @@ public FileMigrationHook(HookScript beforeScript, HookScript beforeEachScript, H this.afterScript = afterScript; } + public FileMigrationHook(HookScript beforeScript, HookScript afterScript) { + this(beforeScript, null, null, afterScript); + } + @Override public void before(Map bindingMap) { if (beforeScript != null) { diff --git a/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java b/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java index 833cd760..53352f0a 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java @@ -91,7 +91,7 @@ protected SqlRunner getSqlRunner(ConnectionProvider connectionProvider) { } } - protected ScriptRunner getScriptRunner(ConnectionProvider connectionProvider, DatabaseOperationOption option, + public static ScriptRunner getScriptRunner(ConnectionProvider connectionProvider, DatabaseOperationOption option, PrintStream printStream) { try { PrintWriter outWriter = printStream == null ? null : new PrintWriter(printStream); diff --git a/src/test/java/org/apache/ibatis/migration/hook/MigrationHookTest.java b/src/test/java/org/apache/ibatis/migration/hook/MigrationHookTest.java index 26cca3b1..3897486f 100644 --- a/src/test/java/org/apache/ibatis/migration/hook/MigrationHookTest.java +++ b/src/test/java/org/apache/ibatis/migration/hook/MigrationHookTest.java @@ -23,6 +23,7 @@ import java.util.Map; import java.util.Properties; +import java.util.UUID; import org.apache.ibatis.io.Resources; import org.apache.ibatis.jdbc.SqlRunner; import org.apache.ibatis.migration.Migrator; @@ -73,11 +74,23 @@ public void checkAssertion() { assertWorklogRowCount(++worklogCounter); versionUp(); assertWorklogRowCount(++worklogCounter); + newHook(); + assertWorklogRowCount(++worklogCounter); out.clearLog(); System.exit(0); } + private void newHook() throws Exception { + out.clearLog(); + String description = UUID.randomUUID().toString(); + Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "new", description)); + assertTrue(out.getLog().contains("SUCCESS")); + assertTrue(out.getLog().contains("before new " + description)); + assertTrue(out.getLog().contains("after new " + description)); + // before + } + private void bootstrap() throws Exception { out.clearLog(); // bootstrap creates a table used in a hook script later diff --git a/src/test/java/org/apache/ibatis/migration/hook/testdir/environments/development.properties b/src/test/java/org/apache/ibatis/migration/hook/testdir/environments/development.properties index dc45445d..17dbde55 100644 --- a/src/test/java/org/apache/ibatis/migration/hook/testdir/environments/development.properties +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/environments/development.properties @@ -1,5 +1,5 @@ # -# Copyright 2010-2016 the original author or authors. +# Copyright 2010-2017 the original author or authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -89,5 +89,6 @@ hook_after_each_up=sql:InsertWorklog.sql:local_var1=LOCALVAR1:local_var2=LOCALVA hook_after_up=js:PrintVar.js:_object=dog:_method=bark:_arg=ARG1:_arg=ARG2:local_var1=LOCALVAR1:local_var2=LOCALVAR2 hook_before_each_down=JavaScript:InsertWorklog.js - +hook_before_new=JavaScript:before_new.js +hook_after_new=JavaScript:after_new.js global_var=GLOBALVAR diff --git a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js new file mode 100644 index 00000000..2da38704 --- /dev/null +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +print("after new " + hookContext.change.description) \ No newline at end of file diff --git a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js new file mode 100644 index 00000000..43d37796 --- /dev/null +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +print("before new " + hookContext.change.description) \ No newline at end of file From 36452d4c50a50d906748fc63709d34d338d72957 Mon Sep 17 00:00:00 2001 From: Christian Bongiorno Date: Fri, 29 Dec 2017 15:50:39 -0800 Subject: [PATCH 2/6] new hook working per discussion. Working on tests --- .../org/apache/ibatis/migration/Change.java | 3 +- .../migration/commands/BaseCommand.java | 57 ++++---- .../ibatis/migration/commands/NewCommand.java | 122 +++++++++++------- .../ibatis/migration/hook/BasicHook.java | 57 ++++++++ .../migration/hook/FileMigrationHook.java | 44 +++---- .../apache/ibatis/migration/hook/Hook.java | 27 ++++ .../ibatis/migration/hook/HookContext.java | 7 +- .../ibatis/migration/hook/MigrationHook.java | 6 +- .../ibatis/migration/hook/NoOpHook.java | 53 ++++++++ .../{ => scripts}/FileHookScriptFactory.java | 2 +- .../hook/{ => scripts}/HookScript.java | 2 +- .../hook/{ => scripts}/HookScriptFactory.java | 2 +- .../hook/{ => scripts}/Jsr223HookScript.java | 2 +- .../hook/scripts/NoOpHookScript.java | 38 ++++++ .../hook/{ => scripts}/SqlHookScript.java | 4 +- .../operations/BootstrapOperation.java | 7 +- .../operations/DatabaseOperation.java | 32 +++-- .../migration/operations/DownOperation.java | 9 +- .../operations/PendingOperation.java | 9 +- .../migration/operations/UpOperation.java | 11 +- .../options/DatabaseOperationOption.java | 15 +++ 21 files changed, 369 insertions(+), 140 deletions(-) create mode 100644 src/main/java/org/apache/ibatis/migration/hook/BasicHook.java create mode 100644 src/main/java/org/apache/ibatis/migration/hook/Hook.java create mode 100644 src/main/java/org/apache/ibatis/migration/hook/NoOpHook.java rename src/main/java/org/apache/ibatis/migration/hook/{ => scripts}/FileHookScriptFactory.java (98%) rename src/main/java/org/apache/ibatis/migration/hook/{ => scripts}/HookScript.java (93%) rename src/main/java/org/apache/ibatis/migration/hook/{ => scripts}/HookScriptFactory.java (93%) rename src/main/java/org/apache/ibatis/migration/hook/{ => scripts}/Jsr223HookScript.java (99%) create mode 100644 src/main/java/org/apache/ibatis/migration/hook/scripts/NoOpHookScript.java rename src/main/java/org/apache/ibatis/migration/hook/{ => scripts}/SqlHookScript.java (94%) diff --git a/src/main/java/org/apache/ibatis/migration/Change.java b/src/main/java/org/apache/ibatis/migration/Change.java index 0d7c4d58..fdd26b27 100644 --- a/src/main/java/org/apache/ibatis/migration/Change.java +++ b/src/main/java/org/apache/ibatis/migration/Change.java @@ -78,7 +78,8 @@ public void setFilename(String filename) { @Override public String toString() { - return id + " " + (appliedTimestamp == null ? " ...pending... " : appliedTimestamp) + " " + description; + String ts = appliedTimestamp == null ? " ...pending... " : appliedTimestamp; + return String.format("%s %s %s %s", id, ts, description, filename); } @Override diff --git a/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java b/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java index 6ec3840d..f2c291d1 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java @@ -18,6 +18,7 @@ import static org.apache.ibatis.migration.utils.Util.file; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; @@ -35,7 +36,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.Map; import java.util.Properties; import java.util.ServiceLoader; import java.util.TimeZone; @@ -50,10 +50,13 @@ import org.apache.ibatis.migration.FileMigrationLoaderFactory; import org.apache.ibatis.migration.MigrationException; import org.apache.ibatis.migration.MigrationLoader; -import org.apache.ibatis.migration.hook.FileHookScriptFactory; +import org.apache.ibatis.migration.hook.BasicHook; +import org.apache.ibatis.migration.hook.Hook; +import org.apache.ibatis.migration.hook.scripts.FileHookScriptFactory; import org.apache.ibatis.migration.hook.FileMigrationHook; -import org.apache.ibatis.migration.hook.HookScriptFactory; +import org.apache.ibatis.migration.hook.scripts.HookScriptFactory; import org.apache.ibatis.migration.hook.MigrationHook; +import org.apache.ibatis.migration.hook.NoOpHook; import org.apache.ibatis.migration.io.ExternalResources; import org.apache.ibatis.migration.options.DatabaseOperationOption; import org.apache.ibatis.migration.options.Options; @@ -153,7 +156,7 @@ private String generatePatternedId(String pattern) { } } - private String generateTimestampId() { + protected String generateTimestampId() { final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); final Date now = new Date(); dateFormat.setTimeZone(TimeZone.getTimeZone(environment().getTimeZone())); @@ -239,6 +242,17 @@ protected File existingEnvironmentFile() { return envFile; } + protected Properties environmentProperties() { + File envFile = existingEnvironmentFile(); + Properties props = new Properties(); + try { + props.load(new FileInputStream(envFile)); + } catch (IOException e) { + throw new MigrationException("Failed to load environment file " + envFile.getAbsolutePath(), e); + } + return props; + } + protected Environment environment() { if (environment != null) { return environment; @@ -316,37 +330,13 @@ protected MigrationLoader getMigrationLoader() { : new FileMigrationLoader(paths.getScriptPath(), env.getScriptCharset(), env.getVariables()); } - protected MigrationHook createNewHook() { + protected Hook createNewMigrationHook() { String before = environment().getBeforeNewHook(); String after = environment().getAfterNewHook(); if (before == null && after == null) { - return createNullHook(); + return NoOpHook.getInstance(); } - return createFileMigrationHook(before, after); - } - - protected MigrationHook createNullHook() { - return new MigrationHook() { - @Override - public void before(Map bindingMap) { - - } - - @Override - public void beforeEach(Map bindingMap) { - - } - - @Override - public void afterEach(Map bindingMap) { - - } - - @Override - public void after(Map bindingMap) { - - } - }; + return createBasicHook(before, after); } protected MigrationHook createUpHook() { @@ -377,9 +367,9 @@ protected MigrationHook createFileMigrationHook(String before, String beforeEach factory.create(after)); } - protected MigrationHook createFileMigrationHook(String before, String after) { + protected BasicHook createBasicHook(String before, String after) { HookScriptFactory factory = new FileHookScriptFactory(options.getPaths(), environment(), printStream); - return new FileMigrationHook(factory.create(before), factory.create(after)); + return new BasicHook(factory.create(before), factory.create(after)); } protected DatabaseOperationOption getDatabaseOperationOption() { @@ -395,4 +385,5 @@ protected DatabaseOperationOption getDatabaseOperationOption() { option.setDelimiter(environment().getDelimiter()); return option; } + } diff --git a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java index 7449d6d9..fd202b5a 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java @@ -15,25 +15,23 @@ */ package org.apache.ibatis.migration.commands; -import static org.apache.ibatis.migration.hook.MigrationHook.HOOK_CONTEXT; -import static org.apache.ibatis.migration.operations.DatabaseOperation.getScriptRunner; - import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; import java.math.BigDecimal; -import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; -import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.io.Resources; import org.apache.ibatis.migration.Change; -import org.apache.ibatis.migration.ConnectionProvider; import org.apache.ibatis.migration.MigrationException; -import org.apache.ibatis.migration.hook.HookContext; +import org.apache.ibatis.migration.hook.Hook; import org.apache.ibatis.migration.hook.MigrationHook; -import org.apache.ibatis.migration.options.DatabaseOperationOption; import org.apache.ibatis.migration.options.SelectedOptions; -import org.apache.ibatis.migration.utils.Util; public final class NewCommand extends BaseCommand { @@ -49,54 +47,88 @@ public void execute(String... params) { throw new MigrationException("No description specified for new migration."); } String description = params[0]; - Properties variables = new Properties(); - variables.setProperty("description", description); - existingEnvironmentFile(); - String nextId = getNextIDAsString(); - String filename = nextId + "_" + description.replace(' ', '_') + ".sql"; - File templateCopy = Util.file(paths.getScriptPath(), filename); - ConnectionProvider connectionProvider = getConnectionProvider(); - DatabaseOperationOption option = getDatabaseOperationOption(); - ScriptRunner runner = getScriptRunner(connectionProvider, option, printStream); + Properties variables = getVariables(description); - MigrationHook hook = createNewHook(); - Map hookBindings = new HashMap(); - Change change = new Change(new BigDecimal(nextId), new Date().toString(), description, - templateCopy.getAbsolutePath()); + Hook hook = createNewMigrationHook(); - hookBindings.put(HOOK_CONTEXT, new HookContext(connectionProvider, runner, change)); + String nextId = getNextIDAsString(); + String filename = nextId + "_" + description.replace(' ', '_') + ".sql"; - if (options.getTemplate() != null) { + Map hookBindings = createBinding(nextId, description, new File(filename)); + { + + Reader templateReader = getTemplateReader(); + Change change = (Change) hookBindings.get("change"); hook.before(hookBindings); - copyExternalResourceTo(options.getTemplate(), templateCopy, variables); - } else { + File changeFile = new File(change.getFilename()); try { - String customConfiguredTemplate = getPropertyOption(CUSTOM_NEW_COMMAND_TEMPLATE_PROPERTY); - if (customConfiguredTemplate != null) { - hook.before(hookBindings); - copyExternalResourceTo(migrationsHome() + "/" + customConfiguredTemplate, templateCopy, variables); - } else { - change.setFilename(filename); - hook.before(hookBindings); - copyDefaultTemplate(variables, filename); - } - } catch (FileNotFoundException e) { - printStream - .append("Your migrations configuration did not find your custom template. Using the default template."); - copyDefaultTemplate(variables, filename); + copyTemplate(templateReader, changeFile, variables); + } catch (IOException e) { + throw new MigrationException("Unable to create template file " + changeFile.getAbsolutePath()); } - } - - hook.after(hookBindings); + hook.after(hookBindings); + } printStream.println("Done!"); printStream.println(); } - private void copyDefaultTemplate(Properties variables, String filename) { - copyResourceTo("org/apache/ibatis/migration/template_migration.sql", Util.file(paths.getScriptPath(), filename), - variables); + private Properties getVariables(String description) { + Properties variables = environmentProperties(); + variables.setProperty("description", description); + for (Entry sys : System.getProperties().entrySet()) { + variables.put("sys." + sys.getKey(), "" + sys.getValue()); + } + for (Entry sys : System.getenv().entrySet()) { + variables.put("env." + sys.getKey(), sys.getValue()); + } + return variables; } + + private Reader getTemplateReader() { + String def = "org/apache/ibatis/migration/template_migration.sql"; + Reader templateReader = null; + try { + templateReader = Resources.getResourceAsReader(def); + } catch (IOException e) { + throw new MigrationException(String.format("Default template %s can't be found?! ", def), e); + } + try { + String template = getTemplateFile(); + templateReader = new FileReader(template); + } catch (FileNotFoundException e) { + String msg = String.format( + "Your migrations configuration did not find your custom template: %s. " + "Using the default template.", + e.getMessage()); + printStream.append(msg); + } + return templateReader; + } + + private String getTemplateFile() throws FileNotFoundException { + String template = null; + if (options.getTemplate() != null) { + template = options.getTemplate(); + } else { + String customConfiguredTemplate = getPropertyOption(CUSTOM_NEW_COMMAND_TEMPLATE_PROPERTY); + if (customConfiguredTemplate != null) { + template = migrationsHome() + "/" + customConfiguredTemplate; + } + } + return template; + } + + private Map createBinding(String nextId, String description, File proposedFile) { + Map hookBindings = new HashMap(); + BigDecimal id = new BigDecimal(nextId); + Change change = new Change(id, null, description, proposedFile.getAbsolutePath()); + Properties props = environmentProperties(); + + hookBindings.put("change", change); + hookBindings.put("environment", props); + return hookBindings; + } + } diff --git a/src/main/java/org/apache/ibatis/migration/hook/BasicHook.java b/src/main/java/org/apache/ibatis/migration/hook/BasicHook.java new file mode 100644 index 00000000..6bdaf889 --- /dev/null +++ b/src/main/java/org/apache/ibatis/migration/hook/BasicHook.java @@ -0,0 +1,57 @@ +/** + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.migration.hook; + +import java.util.Map; +import org.apache.ibatis.migration.hook.scripts.HookScript; +import org.apache.ibatis.migration.hook.scripts.NoOpHookScript; + +/** + * @author cbongiorno on 12/29/17. + */ +public class BasicHook implements Hook { + protected static final NoOpHookScript NO_OP = NoOpHookScript.getInstance(); + + private final HookScript beforeScript; + private final HookScript afterScript; + + public BasicHook() { + this(NO_OP, NO_OP); + } + + public BasicHook(HookScript beforeScript, HookScript afterScript) { + this.beforeScript = beforeScript == null ? NO_OP : beforeScript; + this.afterScript = afterScript == null ? NO_OP : beforeScript; + } + + @Override + public void before(Map bindingMap) { + beforeScript.execute(bindingMap); + } + + @Override + public void after(Map bindingMap) { + afterScript.execute(bindingMap); + } + + public HookScript getBeforeScript() { + return beforeScript; + } + + public HookScript getAfterScript() { + return afterScript; + } +} diff --git a/src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java b/src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java index 72bec97c..678c361d 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java +++ b/src/main/java/org/apache/ibatis/migration/hook/FileMigrationHook.java @@ -16,51 +16,39 @@ package org.apache.ibatis.migration.hook; import java.util.Map; +import org.apache.ibatis.migration.hook.scripts.HookScript; -public class FileMigrationHook implements MigrationHook { +public class FileMigrationHook extends BasicHook implements MigrationHook { - protected final HookScript beforeScript; - protected final HookScript beforeEachScript; - protected final HookScript afterEachScript; - protected final HookScript afterScript; + private final HookScript beforeEachScript; + private final HookScript afterEachScript; public FileMigrationHook(HookScript beforeScript, HookScript beforeEachScript, HookScript afterEachScript, HookScript afterScript) { - this.beforeScript = beforeScript; - this.beforeEachScript = beforeEachScript; - this.afterEachScript = afterEachScript; - this.afterScript = afterScript; + super(beforeScript, afterScript); + this.beforeEachScript = beforeEachScript == null ? NO_OP : beforeEachScript; + this.afterEachScript = afterEachScript == null ? NO_OP : afterEachScript; } public FileMigrationHook(HookScript beforeScript, HookScript afterScript) { - this(beforeScript, null, null, afterScript); - } - - @Override - public void before(Map bindingMap) { - if (beforeScript != null) { - beforeScript.execute(bindingMap); - } + this(beforeScript, NO_OP, NO_OP, afterScript); } @Override public void beforeEach(Map bindingMap) { - if (beforeEachScript != null) { - beforeEachScript.execute(bindingMap); - } + beforeEachScript.execute(bindingMap); } @Override public void afterEach(Map bindingMap) { - if (afterEachScript != null) { - afterEachScript.execute(bindingMap); - } + afterEachScript.execute(bindingMap); } - @Override - public void after(Map bindingMap) { - if (afterScript != null) { - afterScript.execute(bindingMap); - } + public HookScript getBeforeEachScript() { + return beforeEachScript; + } + + public HookScript getAfterEachScript() { + return afterEachScript; } } diff --git a/src/main/java/org/apache/ibatis/migration/hook/Hook.java b/src/main/java/org/apache/ibatis/migration/hook/Hook.java new file mode 100644 index 00000000..be500fac --- /dev/null +++ b/src/main/java/org/apache/ibatis/migration/hook/Hook.java @@ -0,0 +1,27 @@ +/** + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.migration.hook; + +import java.util.Map; + +/** + * @author cbongiorno on 12/28/17. + */ +public interface Hook { + void before(Map bindingMap); + + void after(Map bindingMap); +} diff --git a/src/main/java/org/apache/ibatis/migration/hook/HookContext.java b/src/main/java/org/apache/ibatis/migration/hook/HookContext.java index c809f5b4..92ce64c8 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/HookContext.java +++ b/src/main/java/org/apache/ibatis/migration/hook/HookContext.java @@ -23,6 +23,7 @@ import org.apache.ibatis.jdbc.ScriptRunner; import org.apache.ibatis.migration.Change; import org.apache.ibatis.migration.ConnectionProvider; +import org.apache.ibatis.migration.MigrationException; public class HookContext { private ConnectionProvider connectionProvider; @@ -50,7 +51,11 @@ public Connection getConnection() throws SQLException { * Source of the SQL to execute. */ public void executeSql(Reader reader) { - scriptRunner.runScript(reader); + try { + scriptRunner.runScript(reader, connectionProvider.getConnection()); + } catch (SQLException e) { + throw new MigrationException(e); + } } /** diff --git a/src/main/java/org/apache/ibatis/migration/hook/MigrationHook.java b/src/main/java/org/apache/ibatis/migration/hook/MigrationHook.java index 8ca4d3d5..0c410f2f 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/MigrationHook.java +++ b/src/main/java/org/apache/ibatis/migration/hook/MigrationHook.java @@ -17,16 +17,12 @@ import java.util.Map; -public interface MigrationHook { +public interface MigrationHook extends Hook { public static final String HOOK_CONTEXT = "hookContext"; - void before(Map bindingMap); - void beforeEach(Map bindingMap); void afterEach(Map bindingMap); - void after(Map bindingMap); - } diff --git a/src/main/java/org/apache/ibatis/migration/hook/NoOpHook.java b/src/main/java/org/apache/ibatis/migration/hook/NoOpHook.java new file mode 100644 index 00000000..3c52da29 --- /dev/null +++ b/src/main/java/org/apache/ibatis/migration/hook/NoOpHook.java @@ -0,0 +1,53 @@ +/** + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.migration.hook; + +import java.util.Map; + +/** + * @author cbongiorno on 12/29/17. + */ +public final class NoOpHook implements MigrationHook { + + private static final NoOpHook instance = new NoOpHook(); + + public static NoOpHook getInstance() { + return instance; + } + + private NoOpHook() { + } + + @Override + public void before(Map bindingMap) { + + } + + @Override + public void beforeEach(Map bindingMap) { + + } + + @Override + public void afterEach(Map bindingMap) { + + } + + @Override + public void after(Map bindingMap) { + + } +} diff --git a/src/main/java/org/apache/ibatis/migration/hook/FileHookScriptFactory.java b/src/main/java/org/apache/ibatis/migration/hook/scripts/FileHookScriptFactory.java similarity index 98% rename from src/main/java/org/apache/ibatis/migration/hook/FileHookScriptFactory.java rename to src/main/java/org/apache/ibatis/migration/hook/scripts/FileHookScriptFactory.java index 8440d694..b0c51d0a 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/FileHookScriptFactory.java +++ b/src/main/java/org/apache/ibatis/migration/hook/scripts/FileHookScriptFactory.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.ibatis.migration.hook; +package org.apache.ibatis.migration.hook.scripts; import java.io.File; import java.io.PrintStream; diff --git a/src/main/java/org/apache/ibatis/migration/hook/HookScript.java b/src/main/java/org/apache/ibatis/migration/hook/scripts/HookScript.java similarity index 93% rename from src/main/java/org/apache/ibatis/migration/hook/HookScript.java rename to src/main/java/org/apache/ibatis/migration/hook/scripts/HookScript.java index 27f97541..c8cd7473 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/HookScript.java +++ b/src/main/java/org/apache/ibatis/migration/hook/scripts/HookScript.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.ibatis.migration.hook; +package org.apache.ibatis.migration.hook.scripts; import java.util.Map; diff --git a/src/main/java/org/apache/ibatis/migration/hook/HookScriptFactory.java b/src/main/java/org/apache/ibatis/migration/hook/scripts/HookScriptFactory.java similarity index 93% rename from src/main/java/org/apache/ibatis/migration/hook/HookScriptFactory.java rename to src/main/java/org/apache/ibatis/migration/hook/scripts/HookScriptFactory.java index 37e823b5..9ab33133 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/HookScriptFactory.java +++ b/src/main/java/org/apache/ibatis/migration/hook/scripts/HookScriptFactory.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.ibatis.migration.hook; +package org.apache.ibatis.migration.hook.scripts; public interface HookScriptFactory { diff --git a/src/main/java/org/apache/ibatis/migration/hook/Jsr223HookScript.java b/src/main/java/org/apache/ibatis/migration/hook/scripts/Jsr223HookScript.java similarity index 99% rename from src/main/java/org/apache/ibatis/migration/hook/Jsr223HookScript.java rename to src/main/java/org/apache/ibatis/migration/hook/scripts/Jsr223HookScript.java index c1e46e8d..93df87ec 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/Jsr223HookScript.java +++ b/src/main/java/org/apache/ibatis/migration/hook/scripts/Jsr223HookScript.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.ibatis.migration.hook; +package org.apache.ibatis.migration.hook.scripts; import java.io.File; import java.io.FileInputStream; diff --git a/src/main/java/org/apache/ibatis/migration/hook/scripts/NoOpHookScript.java b/src/main/java/org/apache/ibatis/migration/hook/scripts/NoOpHookScript.java new file mode 100644 index 00000000..9368afa7 --- /dev/null +++ b/src/main/java/org/apache/ibatis/migration/hook/scripts/NoOpHookScript.java @@ -0,0 +1,38 @@ +/** + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.migration.hook.scripts; + +import java.util.Map; + +/** + * @author cbongiorno on 12/29/17. + */ +public class NoOpHookScript implements HookScript { + + private static NoOpHookScript instance = new NoOpHookScript(); + + public static NoOpHookScript getInstance() { + return instance; + } + + private NoOpHookScript() { + } + + @Override + public void execute(Map bindingMap) { + + } +} diff --git a/src/main/java/org/apache/ibatis/migration/hook/SqlHookScript.java b/src/main/java/org/apache/ibatis/migration/hook/scripts/SqlHookScript.java similarity index 94% rename from src/main/java/org/apache/ibatis/migration/hook/SqlHookScript.java rename to src/main/java/org/apache/ibatis/migration/hook/scripts/SqlHookScript.java index 0741b3b1..e0059704 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/SqlHookScript.java +++ b/src/main/java/org/apache/ibatis/migration/hook/scripts/SqlHookScript.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.ibatis.migration.hook; +package org.apache.ibatis.migration.hook.scripts; import java.io.ByteArrayOutputStream; import java.io.File; @@ -25,6 +25,8 @@ import java.util.Properties; import org.apache.ibatis.migration.MigrationException; +import org.apache.ibatis.migration.hook.HookContext; +import org.apache.ibatis.migration.hook.MigrationHook; import org.apache.ibatis.migration.utils.Util; import org.apache.ibatis.parsing.PropertyParser; diff --git a/src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java b/src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java index 8dabe15b..f7e05132 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java @@ -18,7 +18,9 @@ import java.io.PrintStream; import java.io.Reader; +import java.sql.Connection; import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.jdbc.ScriptRunnerFactory; import org.apache.ibatis.migration.ConnectionProvider; import org.apache.ibatis.migration.MigrationException; import org.apache.ibatis.migration.MigrationLoader; @@ -50,11 +52,12 @@ public BootstrapOperation operate(ConnectionProvider connectionProvider, Migrati Reader bootstrapReader = migrationsLoader.getBootstrapReader(); if (bootstrapReader != null) { println(printStream, Util.horizontalLine("Applying: bootstrap.sql", 80)); + Connection connection = connectionProvider.getConnection(); ScriptRunner runner = getScriptRunner(connectionProvider, option, printStream); try { - runner.runScript(bootstrapReader); + runner.runScript(bootstrapReader, connection, option.toProperties()); } finally { - runner.closeConnection(); + connection.close(); } println(printStream); } else { diff --git a/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java b/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java index 53352f0a..4d707f03 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java @@ -17,19 +17,29 @@ import java.io.PrintStream; import java.io.PrintWriter; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.math.BigDecimal; +import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; +import java.util.ServiceLoader; import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.jdbc.ScriptRunnerFactory; import org.apache.ibatis.jdbc.SqlRunner; import org.apache.ibatis.migration.Change; import org.apache.ibatis.migration.ConnectionProvider; +import org.apache.ibatis.migration.FileMigrationLoader; +import org.apache.ibatis.migration.FileMigrationLoaderFactory; import org.apache.ibatis.migration.MigrationException; import org.apache.ibatis.migration.options.DatabaseOperationOption; +import sun.font.ScriptRun; public abstract class DatabaseOperation { @@ -94,19 +104,15 @@ protected SqlRunner getSqlRunner(ConnectionProvider connectionProvider) { public static ScriptRunner getScriptRunner(ConnectionProvider connectionProvider, DatabaseOperationOption option, PrintStream printStream) { try { - PrintWriter outWriter = printStream == null ? null : new PrintWriter(printStream); - ScriptRunner scriptRunner = new ScriptRunner(connectionProvider.getConnection()); - scriptRunner.setLogWriter(outWriter); - scriptRunner.setErrorLogWriter(outWriter); - scriptRunner.setStopOnError(option.isStopOnError()); - scriptRunner.setThrowWarning(option.isThrowWarning()); - scriptRunner.setEscapeProcessing(false); - scriptRunner.setAutoCommit(option.isAutoCommit()); - scriptRunner.setDelimiter(option.getDelimiter()); - scriptRunner.setFullLineDelimiter(option.isFullLineDelimiter()); - scriptRunner.setSendFullScript(option.isSendFullScript()); - scriptRunner.setRemoveCRs(option.isRemoveCRs()); - return scriptRunner; + + Map properties = new HashMap(); + if (printStream != null) { + PrintWriter outWriter = new PrintWriter(printStream); + properties.put("logWriter", outWriter); + properties.put("errorLogWriter", outWriter); + } + properties.putAll(option.toProperties()); + return ScriptRunnerFactory.getInstance(connectionProvider.getConnection(), properties); } catch (Exception e) { throw new MigrationException("Error creating ScriptRunner. Cause: " + e, e); } diff --git a/src/main/java/org/apache/ibatis/migration/operations/DownOperation.java b/src/main/java/org/apache/ibatis/migration/operations/DownOperation.java index ab4c0961..70bf69b3 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/DownOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/DownOperation.java @@ -81,7 +81,8 @@ public DownOperation operate(ConnectionProvider connectionProvider, MigrationLoa hook.beforeEach(hookBindings); } println(printStream, Util.horizontalLine("Undoing: " + change.getFilename(), 80)); - runner.runScript(migrationsLoader.getScriptReader(change, true)); + runner.runScript(migrationsLoader.getScriptReader(change, true), connectionProvider.getConnection()); + if (changelogExists(connectionProvider, option)) { deleteChange(connectionProvider, change, option); } else { @@ -107,7 +108,11 @@ public DownOperation operate(ConnectionProvider connectionProvider, MigrationLoa hook.after(hookBindings); } } finally { - runner.closeConnection(); + try { + connectionProvider.getConnection().close(); + } catch (Exception e) { + // ignore as original code did + } } } return this; diff --git a/src/main/java/org/apache/ibatis/migration/operations/PendingOperation.java b/src/main/java/org/apache/ibatis/migration/operations/PendingOperation.java index 1d376788..86770b30 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/PendingOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/PendingOperation.java @@ -17,6 +17,7 @@ import java.io.PrintStream; import java.io.Reader; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -67,7 +68,7 @@ public PendingOperation operate(ConnectionProvider connectionProvider, Migration } println(printStream, Util.horizontalLine("Applying: " + change.getFilename(), 80)); scriptReader = migrationsLoader.getScriptReader(change, false); - runner.runScript(scriptReader); + runner.runScript(scriptReader, connectionProvider.getConnection()); insertChangelog(change, connectionProvider, option); println(printStream); if (hook != null) { @@ -87,7 +88,11 @@ public PendingOperation operate(ConnectionProvider connectionProvider, Migration if (scriptReader != null) { scriptReader.close(); } - runner.closeConnection(); + try { + connectionProvider.getConnection().close(); + } catch (Exception e) { + // ignore as original code did + } } } catch (Throwable e) { while (e instanceof MigrationException) { diff --git a/src/main/java/org/apache/ibatis/migration/operations/UpOperation.java b/src/main/java/org/apache/ibatis/migration/operations/UpOperation.java index ad3ff48d..9da1f834 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/UpOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/UpOperation.java @@ -17,6 +17,7 @@ import java.io.PrintStream; import java.io.Reader; +import java.sql.SQLException; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -88,7 +89,7 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade } println(printStream, Util.horizontalLine("Applying: " + change.getFilename(), 80)); scriptReader = migrationsLoader.getScriptReader(change, false); - runner.runScript(scriptReader); + runner.runScript(scriptReader, connectionProvider.getConnection()); insertChangelog(change, connectionProvider, option); println(printStream); if (hook != null) { @@ -111,7 +112,7 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade if (onAbortScriptReader != null) { println(printStream); println(printStream, Util.horizontalLine("Executing onabort.sql script.", 80)); - runner.runScript(onAbortScriptReader); + runner.runScript(onAbortScriptReader, connectionProvider.getConnection()); println(printStream); } throw e; @@ -122,7 +123,11 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade if (onAbortScriptReader != null) { onAbortScriptReader.close(); } - runner.closeConnection(); + try { + connectionProvider.getConnection().close(); + } catch (Exception e) { + // ignore as original code did + } } } catch (Throwable e) { while (e instanceof MigrationException) { diff --git a/src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java b/src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java index 4d2348ee..b5a84635 100644 --- a/src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java +++ b/src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java @@ -15,6 +15,8 @@ */ package org.apache.ibatis.migration.options; +import java.util.Properties; + public class DatabaseOperationOption { private static final String DEFAULT_CHANGELOG_TABLE = "CHANGELOG"; @@ -109,4 +111,17 @@ public String getDelimiter() { public void setDelimiter(String delimiter) { this.delimiter = delimiter; } + + public Properties toProperties() { + Properties properties = new Properties(); + properties.put("stopOnError", this.isStopOnError()); + properties.put("throwWarning", this.isThrowWarning()); + properties.put("escapeProcessing", false); + properties.put("autoCommit", this.isAutoCommit()); + properties.put("delimiter", this.getDelimiter()); + properties.put("fullLineDelimiter", this.isFullLineDelimiter()); + properties.put("sendFullScript", this.isSendFullScript()); + properties.put("removeCRs", this.isRemoveCRs()); + return properties; + } } From 96e10cbd38dc0a27246cc13be1d5c10176041b0b Mon Sep 17 00:00:00 2001 From: Christian Bongiorno Date: Fri, 29 Dec 2017 16:04:10 -0800 Subject: [PATCH 3/6] Remove linkage to unapproved mybatis changes --- .../ibatis/migration/hook/HookContext.java | 7 +--- .../operations/BootstrapOperation.java | 7 ++-- .../operations/DatabaseOperation.java | 35 ++++++++----------- .../migration/operations/DownOperation.java | 9 ++--- .../operations/PendingOperation.java | 9 ++--- .../migration/operations/UpOperation.java | 11 ++---- 6 files changed, 24 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/apache/ibatis/migration/hook/HookContext.java b/src/main/java/org/apache/ibatis/migration/hook/HookContext.java index 92ce64c8..c809f5b4 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/HookContext.java +++ b/src/main/java/org/apache/ibatis/migration/hook/HookContext.java @@ -23,7 +23,6 @@ import org.apache.ibatis.jdbc.ScriptRunner; import org.apache.ibatis.migration.Change; import org.apache.ibatis.migration.ConnectionProvider; -import org.apache.ibatis.migration.MigrationException; public class HookContext { private ConnectionProvider connectionProvider; @@ -51,11 +50,7 @@ public Connection getConnection() throws SQLException { * Source of the SQL to execute. */ public void executeSql(Reader reader) { - try { - scriptRunner.runScript(reader, connectionProvider.getConnection()); - } catch (SQLException e) { - throw new MigrationException(e); - } + scriptRunner.runScript(reader); } /** diff --git a/src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java b/src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java index f7e05132..8dabe15b 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/BootstrapOperation.java @@ -18,9 +18,7 @@ import java.io.PrintStream; import java.io.Reader; -import java.sql.Connection; import org.apache.ibatis.jdbc.ScriptRunner; -import org.apache.ibatis.jdbc.ScriptRunnerFactory; import org.apache.ibatis.migration.ConnectionProvider; import org.apache.ibatis.migration.MigrationException; import org.apache.ibatis.migration.MigrationLoader; @@ -52,12 +50,11 @@ public BootstrapOperation operate(ConnectionProvider connectionProvider, Migrati Reader bootstrapReader = migrationsLoader.getBootstrapReader(); if (bootstrapReader != null) { println(printStream, Util.horizontalLine("Applying: bootstrap.sql", 80)); - Connection connection = connectionProvider.getConnection(); ScriptRunner runner = getScriptRunner(connectionProvider, option, printStream); try { - runner.runScript(bootstrapReader, connection, option.toProperties()); + runner.runScript(bootstrapReader); } finally { - connection.close(); + runner.closeConnection(); } println(printStream); } else { diff --git a/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java b/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java index 4d707f03..56e5d9a8 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java @@ -17,29 +17,18 @@ import java.io.PrintStream; import java.io.PrintWriter; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.math.BigDecimal; -import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; - -import java.util.Properties; -import java.util.ServiceLoader; import org.apache.ibatis.jdbc.ScriptRunner; -import org.apache.ibatis.jdbc.ScriptRunnerFactory; import org.apache.ibatis.jdbc.SqlRunner; import org.apache.ibatis.migration.Change; import org.apache.ibatis.migration.ConnectionProvider; -import org.apache.ibatis.migration.FileMigrationLoader; -import org.apache.ibatis.migration.FileMigrationLoaderFactory; import org.apache.ibatis.migration.MigrationException; import org.apache.ibatis.migration.options.DatabaseOperationOption; -import sun.font.ScriptRun; public abstract class DatabaseOperation { @@ -101,18 +90,22 @@ protected SqlRunner getSqlRunner(ConnectionProvider connectionProvider) { } } - public static ScriptRunner getScriptRunner(ConnectionProvider connectionProvider, DatabaseOperationOption option, + protected ScriptRunner getScriptRunner(ConnectionProvider connectionProvider, DatabaseOperationOption option, PrintStream printStream) { try { - - Map properties = new HashMap(); - if (printStream != null) { - PrintWriter outWriter = new PrintWriter(printStream); - properties.put("logWriter", outWriter); - properties.put("errorLogWriter", outWriter); - } - properties.putAll(option.toProperties()); - return ScriptRunnerFactory.getInstance(connectionProvider.getConnection(), properties); + PrintWriter outWriter = printStream == null ? null : new PrintWriter(printStream); + ScriptRunner scriptRunner = new ScriptRunner(connectionProvider.getConnection()); + scriptRunner.setLogWriter(outWriter); + scriptRunner.setErrorLogWriter(outWriter); + scriptRunner.setStopOnError(option.isStopOnError()); + scriptRunner.setThrowWarning(option.isThrowWarning()); + scriptRunner.setEscapeProcessing(false); + scriptRunner.setAutoCommit(option.isAutoCommit()); + scriptRunner.setDelimiter(option.getDelimiter()); + scriptRunner.setFullLineDelimiter(option.isFullLineDelimiter()); + scriptRunner.setSendFullScript(option.isSendFullScript()); + scriptRunner.setRemoveCRs(option.isRemoveCRs()); + return scriptRunner; } catch (Exception e) { throw new MigrationException("Error creating ScriptRunner. Cause: " + e, e); } diff --git a/src/main/java/org/apache/ibatis/migration/operations/DownOperation.java b/src/main/java/org/apache/ibatis/migration/operations/DownOperation.java index 70bf69b3..ab4c0961 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/DownOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/DownOperation.java @@ -81,8 +81,7 @@ public DownOperation operate(ConnectionProvider connectionProvider, MigrationLoa hook.beforeEach(hookBindings); } println(printStream, Util.horizontalLine("Undoing: " + change.getFilename(), 80)); - runner.runScript(migrationsLoader.getScriptReader(change, true), connectionProvider.getConnection()); - + runner.runScript(migrationsLoader.getScriptReader(change, true)); if (changelogExists(connectionProvider, option)) { deleteChange(connectionProvider, change, option); } else { @@ -108,11 +107,7 @@ public DownOperation operate(ConnectionProvider connectionProvider, MigrationLoa hook.after(hookBindings); } } finally { - try { - connectionProvider.getConnection().close(); - } catch (Exception e) { - // ignore as original code did - } + runner.closeConnection(); } } return this; diff --git a/src/main/java/org/apache/ibatis/migration/operations/PendingOperation.java b/src/main/java/org/apache/ibatis/migration/operations/PendingOperation.java index 86770b30..1d376788 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/PendingOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/PendingOperation.java @@ -17,7 +17,6 @@ import java.io.PrintStream; import java.io.Reader; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -68,7 +67,7 @@ public PendingOperation operate(ConnectionProvider connectionProvider, Migration } println(printStream, Util.horizontalLine("Applying: " + change.getFilename(), 80)); scriptReader = migrationsLoader.getScriptReader(change, false); - runner.runScript(scriptReader, connectionProvider.getConnection()); + runner.runScript(scriptReader); insertChangelog(change, connectionProvider, option); println(printStream); if (hook != null) { @@ -88,11 +87,7 @@ public PendingOperation operate(ConnectionProvider connectionProvider, Migration if (scriptReader != null) { scriptReader.close(); } - try { - connectionProvider.getConnection().close(); - } catch (Exception e) { - // ignore as original code did - } + runner.closeConnection(); } } catch (Throwable e) { while (e instanceof MigrationException) { diff --git a/src/main/java/org/apache/ibatis/migration/operations/UpOperation.java b/src/main/java/org/apache/ibatis/migration/operations/UpOperation.java index 9da1f834..ad3ff48d 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/UpOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/UpOperation.java @@ -17,7 +17,6 @@ import java.io.PrintStream; import java.io.Reader; -import java.sql.SQLException; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -89,7 +88,7 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade } println(printStream, Util.horizontalLine("Applying: " + change.getFilename(), 80)); scriptReader = migrationsLoader.getScriptReader(change, false); - runner.runScript(scriptReader, connectionProvider.getConnection()); + runner.runScript(scriptReader); insertChangelog(change, connectionProvider, option); println(printStream); if (hook != null) { @@ -112,7 +111,7 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade if (onAbortScriptReader != null) { println(printStream); println(printStream, Util.horizontalLine("Executing onabort.sql script.", 80)); - runner.runScript(onAbortScriptReader, connectionProvider.getConnection()); + runner.runScript(onAbortScriptReader); println(printStream); } throw e; @@ -123,11 +122,7 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade if (onAbortScriptReader != null) { onAbortScriptReader.close(); } - try { - connectionProvider.getConnection().close(); - } catch (Exception e) { - // ignore as original code did - } + runner.closeConnection(); } } catch (Throwable e) { while (e instanceof MigrationException) { From fd30ee5a6676621d0649317b50fe17df0a9d9191 Mon Sep 17 00:00:00 2001 From: Christian Bongiorno Date: Fri, 29 Dec 2017 16:09:37 -0800 Subject: [PATCH 4/6] update before/after new scripts to represent better. Remove some code that, while inert, isn't necessary for this change --- .../migration/options/DatabaseOperationOption.java | 12 ------------ .../ibatis/migration/hook/testdir/hooks/after_new.js | 3 ++- .../migration/hook/testdir/hooks/before_new.js | 4 +++- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java b/src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java index b5a84635..f9ffa59c 100644 --- a/src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java +++ b/src/main/java/org/apache/ibatis/migration/options/DatabaseOperationOption.java @@ -112,16 +112,4 @@ public void setDelimiter(String delimiter) { this.delimiter = delimiter; } - public Properties toProperties() { - Properties properties = new Properties(); - properties.put("stopOnError", this.isStopOnError()); - properties.put("throwWarning", this.isThrowWarning()); - properties.put("escapeProcessing", false); - properties.put("autoCommit", this.isAutoCommit()); - properties.put("delimiter", this.getDelimiter()); - properties.put("fullLineDelimiter", this.isFullLineDelimiter()); - properties.put("sendFullScript", this.isSendFullScript()); - properties.put("removeCRs", this.isRemoveCRs()); - return properties; - } } diff --git a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js index 2da38704..3c441a2c 100644 --- a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js @@ -13,4 +13,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -print("after new " + hookContext.change.description) \ No newline at end of file +print("change supplied " + (change !== null)) +print("environment supplied" + (environment !== null) \ No newline at end of file diff --git a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js index 43d37796..16686012 100644 --- a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js @@ -13,4 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -print("before new " + hookContext.change.description) \ No newline at end of file +print("change supplied " + (change !== null)) +print("environment supplied" + (environment !== null) +change.filename = "foo.sql" \ No newline at end of file From dfbfcd7c85cd8768bd5347af1c1c63b518c8df79 Mon Sep 17 00:00:00 2001 From: Christian Bongiorno Date: Sun, 31 Dec 2017 16:42:17 -0800 Subject: [PATCH 5/6] Remove some potential NPE --- .../apache/ibatis/migration/commands/NewCommand.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java index fd202b5a..78ca1122 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java @@ -79,10 +79,12 @@ private Properties getVariables(String description) { Properties variables = environmentProperties(); variables.setProperty("description", description); for (Entry sys : System.getProperties().entrySet()) { - variables.put("sys." + sys.getKey(), "" + sys.getValue()); + if(sys.getValue() != null) + variables.put("sys." + sys.getKey(), sys.getValue()); } - for (Entry sys : System.getenv().entrySet()) { - variables.put("env." + sys.getKey(), sys.getValue()); + for (Entry env : System.getenv().entrySet()) { + if(env.getValue() != null) + variables.put("env." + env.getKey(), env.getValue()); } return variables; } @@ -100,7 +102,7 @@ private Reader getTemplateReader() { templateReader = new FileReader(template); } catch (FileNotFoundException e) { String msg = String.format( - "Your migrations configuration did not find your custom template: %s. " + "Using the default template.", + "Your migrations configuration did not find your custom template: %s. Using the default template.", e.getMessage()); printStream.append(msg); } From 493f67632c35d16efabb57a0600125192f8914b6 Mon Sep 17 00:00:00 2001 From: Christian Bongiorno Date: Tue, 2 Jan 2018 14:39:24 -0800 Subject: [PATCH 6/6] new hook implementation complete --- .../migration/commands/BaseCommand.java | 18 ++++++++++ .../ibatis/migration/commands/NewCommand.java | 34 ++++++------------- .../ibatis/migration/hook/BasicHook.java | 4 +-- .../hook/scripts/Jsr223HookScript.java | 2 +- .../apache/ibatis/migration/MigratorTest.java | 4 +-- .../migration/hook/MigrationHookTest.java | 11 ++++-- .../migration/hook/testdir/hooks/after_new.js | 8 +++-- .../hook/testdir/hooks/before_new.js | 10 +++--- 8 files changed, 53 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java b/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java index f2c291d1..603a15fa 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/BaseCommand.java @@ -36,6 +36,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Map.Entry; import java.util.Properties; import java.util.ServiceLoader; import java.util.TimeZone; @@ -110,6 +111,23 @@ public void setPrintStream(PrintStream aPrintStream) { printStream = aPrintStream; } + /** + * Use this to define template variables + * @return combined properties of system/environment with 'sys.' and 'env.' appended respectively + */ + protected Properties getVariables() { + Properties variables = environmentProperties(); + for (Entry sys : System.getProperties().entrySet()) { + if (sys.getValue() != null) + variables.put("sys." + sys.getKey(), sys.getValue()); + } + for (Entry env : System.getenv().entrySet()) { + if (env.getValue() != null) + variables.put("env." + env.getKey(), env.getValue()); + } + return variables; + } + protected boolean paramsEmpty(String... params) { return params == null || params.length < 1 || params[0] == null || params[0].length() < 1; } diff --git a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java index 78ca1122..3567e1de 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java @@ -16,7 +16,6 @@ package org.apache.ibatis.migration.commands; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; @@ -24,13 +23,11 @@ import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.Properties; import org.apache.ibatis.io.Resources; import org.apache.ibatis.migration.Change; import org.apache.ibatis.migration.MigrationException; import org.apache.ibatis.migration.hook.Hook; -import org.apache.ibatis.migration.hook.MigrationHook; import org.apache.ibatis.migration.options.SelectedOptions; public final class NewCommand extends BaseCommand { @@ -48,14 +45,14 @@ public void execute(String... params) { } String description = params[0]; - Properties variables = getVariables(description); + Properties variables = getVariables(); + variables.setProperty("description", description); Hook hook = createNewMigrationHook(); String nextId = getNextIDAsString(); String filename = nextId + "_" + description.replace(' ', '_') + ".sql"; - - Map hookBindings = createBinding(nextId, description, new File(filename)); + Map hookBindings = createBinding(nextId, description, filename); { Reader templateReader = getTemplateReader(); @@ -75,20 +72,6 @@ public void execute(String... params) { } - private Properties getVariables(String description) { - Properties variables = environmentProperties(); - variables.setProperty("description", description); - for (Entry sys : System.getProperties().entrySet()) { - if(sys.getValue() != null) - variables.put("sys." + sys.getKey(), sys.getValue()); - } - for (Entry env : System.getenv().entrySet()) { - if(env.getValue() != null) - variables.put("env." + env.getKey(), env.getValue()); - } - return variables; - } - private Reader getTemplateReader() { String def = "org/apache/ibatis/migration/template_migration.sql"; Reader templateReader = null; @@ -99,7 +82,9 @@ private Reader getTemplateReader() { } try { String template = getTemplateFile(); - templateReader = new FileReader(template); + if (template != null) + templateReader = new FileReader(template); + } catch (FileNotFoundException e) { String msg = String.format( "Your migrations configuration did not find your custom template: %s. Using the default template.", @@ -122,13 +107,16 @@ private String getTemplateFile() throws FileNotFoundException { return template; } - private Map createBinding(String nextId, String description, File proposedFile) { + private Map createBinding(String nextId, String description, String proposedFile) { Map hookBindings = new HashMap(); BigDecimal id = new BigDecimal(nextId); - Change change = new Change(id, null, description, proposedFile.getAbsolutePath()); + + File f = new File(String.format("%s%s%s", paths.getScriptPath(), File.separator, proposedFile)); + Change change = new Change(id, null, description, f.getAbsolutePath()); Properties props = environmentProperties(); hookBindings.put("change", change); + hookBindings.put("paths", paths); hookBindings.put("environment", props); return hookBindings; } diff --git a/src/main/java/org/apache/ibatis/migration/hook/BasicHook.java b/src/main/java/org/apache/ibatis/migration/hook/BasicHook.java index 6bdaf889..b50fd27d 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/BasicHook.java +++ b/src/main/java/org/apache/ibatis/migration/hook/BasicHook.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2017 the original author or authors. + * Copyright 2010-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,7 @@ public BasicHook() { public BasicHook(HookScript beforeScript, HookScript afterScript) { this.beforeScript = beforeScript == null ? NO_OP : beforeScript; - this.afterScript = afterScript == null ? NO_OP : beforeScript; + this.afterScript = afterScript == null ? NO_OP : afterScript; } @Override diff --git a/src/main/java/org/apache/ibatis/migration/hook/scripts/Jsr223HookScript.java b/src/main/java/org/apache/ibatis/migration/hook/scripts/Jsr223HookScript.java index 93df87ec..e20a41f6 100644 --- a/src/main/java/org/apache/ibatis/migration/hook/scripts/Jsr223HookScript.java +++ b/src/main/java/org/apache/ibatis/migration/hook/scripts/Jsr223HookScript.java @@ -108,7 +108,7 @@ public void execute(Map bindingMap) { if (functionName != null) { printStream.println(Util.horizontalLine("Invoking function : " + functionName, 80)); invocable.invokeFunction(functionName, args.toArray()); - } else if (objectName != null && methodName != null) { + } else { printStream.println(Util.horizontalLine("Invoking method : " + methodName, 80)); Object targetObject = engine.get(objectName); invocable.invokeMethod(targetObject, methodName, args.toArray()); diff --git a/src/test/java/org/apache/ibatis/migration/MigratorTest.java b/src/test/java/org/apache/ibatis/migration/MigratorTest.java index d82d4dfc..27e573fc 100644 --- a/src/test/java/org/apache/ibatis/migration/MigratorTest.java +++ b/src/test/java/org/apache/ibatis/migration/MigratorTest.java @@ -345,8 +345,8 @@ public void useCustomTemplateWithBadPath() throws Exception { Migrator.main(TestUtil.args("--path=" + basePath.getAbsolutePath(), "new", "test new migration")); String output = out.getLog(); assertEquals(4, scriptPath.list().length); - assertTrue(output.toString() - .contains("Your migrations configuration did not find your custom template. Using the default template.")); + assertTrue(output.contains("Your migrations configuration did not find your custom template")); + assertTrue(output.contains("Using the default template")); } private File getTempDir() throws IOException { diff --git a/src/test/java/org/apache/ibatis/migration/hook/MigrationHookTest.java b/src/test/java/org/apache/ibatis/migration/hook/MigrationHookTest.java index 3897486f..cb411cd2 100644 --- a/src/test/java/org/apache/ibatis/migration/hook/MigrationHookTest.java +++ b/src/test/java/org/apache/ibatis/migration/hook/MigrationHookTest.java @@ -75,7 +75,6 @@ public void checkAssertion() { versionUp(); assertWorklogRowCount(++worklogCounter); newHook(); - assertWorklogRowCount(++worklogCounter); out.clearLog(); System.exit(0); @@ -86,8 +85,14 @@ private void newHook() throws Exception { String description = UUID.randomUUID().toString(); Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "new", description)); assertTrue(out.getLog().contains("SUCCESS")); - assertTrue(out.getLog().contains("before new " + description)); - assertTrue(out.getLog().contains("after new " + description)); + assertTrue(out.getLog().contains("before new change supplied true")); + assertTrue(out.getLog().contains("before new environment supplied true")); + + String path = String.format("%s%s%s.sql", dir.getCanonicalPath(), File.separator, description); + assertTrue(new File(path).exists()); + + assertTrue(out.getLog().contains("after new change supplied true")); + assertTrue(out.getLog().contains("after new environment supplied true")); // before } diff --git a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js index 3c441a2c..45ca4cd8 100644 --- a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js @@ -1,5 +1,5 @@ /* - * Copyright 2010-2017 the original author or authors. + * Copyright 2010-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,5 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -print("change supplied " + (change !== null)) -print("environment supplied" + (environment !== null) \ No newline at end of file +if (typeof println == 'undefined') + this.println = print; +println("after new change supplied " + (change !== null)) +println("after new environment supplied " + (environment !== null)) \ No newline at end of file diff --git a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js index 16686012..4bc4c36a 100644 --- a/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js @@ -1,5 +1,5 @@ /* - * Copyright 2010-2017 the original author or authors. + * Copyright 2010-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -print("change supplied " + (change !== null)) -print("environment supplied" + (environment !== null) -change.filename = "foo.sql" \ No newline at end of file +if (typeof println == 'undefined') + this.println = print; +println("before new change supplied " + (change !== null)) +println("before new environment supplied " + (environment !== null)) +change.filename = paths.basePath + '/' + change.description+'.sql' \ No newline at end of file