diff --git a/src/main/java/org/apache/ibatis/migration/Change.java b/src/main/java/org/apache/ibatis/migration/Change.java index bc817513..fdd26b27 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; } @@ -71,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/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..603a15fa 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,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; @@ -49,10 +51,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; @@ -106,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; } @@ -152,7 +174,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())); @@ -238,6 +260,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; @@ -315,6 +348,15 @@ protected MigrationLoader getMigrationLoader() { : new FileMigrationLoader(paths.getScriptPath(), env.getScriptCharset(), env.getVariables()); } + protected Hook createNewMigrationHook() { + String before = environment().getBeforeNewHook(); + String after = environment().getAfterNewHook(); + if (before == null && after == null) { + return NoOpHook.getInstance(); + } + return createBasicHook(before, after); + } + protected MigrationHook createUpHook() { String before = environment().getHookBeforeUp(); String beforeEach = environment().getHookBeforeEachUp(); @@ -343,6 +385,11 @@ protected MigrationHook createFileMigrationHook(String before, String beforeEach factory.create(after)); } + protected BasicHook createBasicHook(String before, String after) { + HookScriptFactory factory = new FileHookScriptFactory(options.getPaths(), environment(), printStream); + return new BasicHook(factory.create(before), factory.create(after)); + } + protected DatabaseOperationOption getDatabaseOperationOption() { DatabaseOperationOption option = new DatabaseOperationOption(); option.setChangelogTable(changelogTable()); @@ -356,4 +403,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 fbc30ee6..3567e1de 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java @@ -15,12 +15,20 @@ */ package org.apache.ibatis.migration.commands; +import java.io.File; import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Map; 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.options.SelectedOptions; -import org.apache.ibatis.migration.utils.Util; public final class NewCommand extends BaseCommand { @@ -36,34 +44,81 @@ public void execute(String... params) { throw new MigrationException("No description specified for new migration."); } String description = params[0]; - Properties variables = new Properties(); + + Properties variables = getVariables(); variables.setProperty("description", description); - existingEnvironmentFile(); - String filename = getNextIDAsString() + "_" + description.replace(' ', '_') + ".sql"; - if (options.getTemplate() != null) { - copyExternalResourceTo(options.getTemplate(), Util.file(paths.getScriptPath(), filename), variables); - } else { + Hook hook = createNewMigrationHook(); + + String nextId = getNextIDAsString(); + String filename = nextId + "_" + description.replace(' ', '_') + ".sql"; + Map hookBindings = createBinding(nextId, description, filename); + { + + Reader templateReader = getTemplateReader(); + Change change = (Change) hookBindings.get("change"); + hook.before(hookBindings); + File changeFile = new File(change.getFilename()); try { - String customConfiguredTemplate = getPropertyOption(CUSTOM_NEW_COMMAND_TEMPLATE_PROPERTY); - if (customConfiguredTemplate != null) { - copyExternalResourceTo(migrationsHome() + "/" + customConfiguredTemplate, - Util.file(paths.getScriptPath(), filename), variables); - } else { - 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); } printStream.println("Done!"); printStream.println(); + + } + + 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(); + 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.", + e.getMessage()); + printStream.append(msg); + } + return templateReader; } - private void copyDefaultTemplate(Properties variables, String filename) { - copyResourceTo("org/apache/ibatis/migration/template_migration.sql", Util.file(paths.getScriptPath(), filename), - variables); + 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, String proposedFile) { + Map hookBindings = new HashMap(); + BigDecimal id = new BigDecimal(nextId); + + 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 new file mode 100644 index 00000000..b50fd27d --- /dev/null +++ b/src/main/java/org/apache/ibatis/migration/hook/BasicHook.java @@ -0,0 +1,57 @@ +/** + * 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. + * 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 : afterScript; + } + + @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 cc6fce22..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,47 +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; } - @Override - public void before(Map bindingMap) { - if (beforeScript != null) { - beforeScript.execute(bindingMap); - } + public FileMigrationHook(HookScript beforeScript, HookScript afterScript) { + 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/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 98% 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..e20a41f6 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; @@ -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/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/DatabaseOperation.java b/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java index 833cd760..56e5d9a8 100644 --- a/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java +++ b/src/main/java/org/apache/ibatis/migration/operations/DatabaseOperation.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; - import org.apache.ibatis.jdbc.ScriptRunner; import org.apache.ibatis.jdbc.SqlRunner; import org.apache.ibatis.migration.Change; 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..f9ffa59c 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,5 @@ public String getDelimiter() { public void setDelimiter(String delimiter) { this.delimiter = delimiter; } + } 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 26cca3b1..cb411cd2 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,28 @@ public void checkAssertion() { assertWorklogRowCount(++worklogCounter); versionUp(); assertWorklogRowCount(++worklogCounter); + newHook(); 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 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 + } + 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..45ca4cd8 --- /dev/null +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/after_new.js @@ -0,0 +1,19 @@ +/* + * 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. + * 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. + */ +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 new file mode 100644 index 00000000..4bc4c36a --- /dev/null +++ b/src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/before_new.js @@ -0,0 +1,20 @@ +/* + * 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. + * 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. + */ +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