From a8dd39cfa521c9b6a848be580e158e23a73a5967 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Thu, 1 Feb 2018 00:14:34 +0900 Subject: [PATCH] Add before/after-new hooks. --- .../apache/ibatis/migration/Environment.java | 20 +++- .../ibatis/migration/commands/NewCommand.java | 24 ++++ .../ibatis/migration/hook/NewHookContext.java | 45 ++++++++ src/site/xdoc/hooks.xml | 24 +++- .../apache/ibatis/migration/MigratorTest.java | 26 ++--- .../ibatis/migration/hook/NewHookTest.java | 105 ++++++++++++++++++ .../migration/hook/testdir/hooks/NewHook.js | 40 +++++++ .../ibatis/migration/utils/TestUtil.java | 16 ++- 8 files changed, 278 insertions(+), 22 deletions(-) create mode 100644 src/main/java/org/apache/ibatis/migration/hook/NewHookContext.java create mode 100644 src/test/java/org/apache/ibatis/migration/hook/NewHookTest.java create mode 100644 src/test/java/org/apache/ibatis/migration/hook/testdir/hooks/NewHook.js diff --git a/src/main/java/org/apache/ibatis/migration/Environment.java b/src/main/java/org/apache/ibatis/migration/Environment.java index ac384899..2655b643 100644 --- a/src/main/java/org/apache/ibatis/migration/Environment.java +++ b/src/main/java/org/apache/ibatis/migration/Environment.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. @@ -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; @@ -89,6 +91,9 @@ private enum SETTING_KEY { private final String hookAfterEachDown; private final String hookAfterDown; + private final String hookBeforeNew; + private final String hookAfterNew; + private final Properties variables = new Properties(); public Environment(File file) { @@ -122,6 +127,9 @@ 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.hookBeforeNew = prop.getProperty(SETTING_KEY.hook_before_new.name()); + this.hookAfterNew = prop.getProperty(SETTING_KEY.hook_after_new.name()); + // User defined variables. Set> entries = prop.entrySet(); for (Entry entry : entries) { @@ -229,6 +237,14 @@ public String getHookAfterDown() { return hookAfterDown; } + public String getHookBeforeNew() { + return hookBeforeNew; + } + + public String getHookAfterNew() { + return hookAfterNew; + } + public Properties getVariables() { return variables; } 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 79811edb..52c542cb 100644 --- a/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java +++ b/src/main/java/org/apache/ibatis/migration/commands/NewCommand.java @@ -16,9 +16,13 @@ package org.apache.ibatis.migration.commands; import java.io.File; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; import org.apache.ibatis.migration.MigrationException; +import org.apache.ibatis.migration.hook.MigrationHook; +import org.apache.ibatis.migration.hook.NewHookContext; import org.apache.ibatis.migration.options.SelectedOptions; import org.apache.ibatis.migration.utils.Util; @@ -41,6 +45,13 @@ public void execute(String... params) { existingEnvironmentFile(); String filename = getNextIDAsString() + "_" + description.replace(' ', '_') + ".sql"; + Map hookBindings = new HashMap(); + MigrationHook hook = createNewHook(); + if (hook != null) { + hookBindings.put(MigrationHook.HOOK_CONTEXT, new NewHookContext(description, filename)); + hook.before(hookBindings); + } + if (options.getTemplate() != null) { copyExternalResourceTo(options.getTemplate(), Util.file(paths.getScriptPath(), filename), variables); } else { @@ -53,6 +64,10 @@ public void execute(String... params) { .append("Your migrations configuration did not find your custom template. Using the default template."); copyDefaultTemplate(variables, filename); } + if (hook != null) { + hookBindings.put(MigrationHook.HOOK_CONTEXT, new NewHookContext(description, filename)); + hook.after(hookBindings); + } } printStream.println("Done!"); printStream.println(); @@ -62,4 +77,13 @@ private void copyDefaultTemplate(Properties variables, String filename) { copyResourceTo("org/apache/ibatis/migration/template_migration.sql", Util.file(paths.getScriptPath(), filename), variables); } + + private MigrationHook createNewHook() { + String before = environment().getHookBeforeNew(); + String after = environment().getHookAfterNew(); + if (before == null && after == null) { + return null; + } + return createFileMigrationHook(before, null, null, after); + } } diff --git a/src/main/java/org/apache/ibatis/migration/hook/NewHookContext.java b/src/main/java/org/apache/ibatis/migration/hook/NewHookContext.java new file mode 100644 index 00000000..65a085ea --- /dev/null +++ b/src/main/java/org/apache/ibatis/migration/hook/NewHookContext.java @@ -0,0 +1,45 @@ +/** + * 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; + +/** + * Hook context object that is available to before_new and after_new hooks. + */ +public class NewHookContext { + private String description; + private String filename; + + public NewHookContext(String description, String filename) { + super(); + this.description = description; + this.filename = filename; + } + + /** + * @return The specified description. + */ + public String getDescription() { + return description; + } + + /** + * @return The name of the file that is created by new command.
+ * In before_new hook, the file is not created yet. + */ + public String getFilename() { + return filename; + } +} diff --git a/src/site/xdoc/hooks.xml b/src/site/xdoc/hooks.xml index 889450a6..9f030c28 100644 --- a/src/site/xdoc/hooks.xml +++ b/src/site/xdoc/hooks.xml @@ -1,7 +1,7 @@