NewCommand.java

  1. /*
  2.  *    Copyright 2010-2023 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.migration.commands;

  17. import java.io.File;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.Properties;

  21. import org.apache.ibatis.migration.MigrationException;
  22. import org.apache.ibatis.migration.hook.MigrationHook;
  23. import org.apache.ibatis.migration.hook.NewHookContext;
  24. import org.apache.ibatis.migration.options.SelectedOptions;
  25. import org.apache.ibatis.migration.utils.Util;

  26. public final class NewCommand extends BaseCommand {

  27.   private static final String CUSTOM_NEW_COMMAND_TEMPLATE_PROPERTY = "new_command.template";

  28.   public NewCommand(SelectedOptions options) {
  29.     super(options);
  30.   }

  31.   @Override
  32.   public void execute(String... params) {
  33.     if (paramsEmpty(params)) {
  34.       throw new MigrationException("No description specified for new migration.");
  35.     }
  36.     String description = params[0];
  37.     Properties variables = new Properties();
  38.     variables.setProperty("description", description);
  39.     existingEnvironmentFile();
  40.     String filename = getNextIDAsString() + '_' + description.replace(' ', '_') + ".sql";

  41.     Map<String, Object> hookBindings = new HashMap<>();
  42.     MigrationHook hook = createNewHook();
  43.     if (hook != null) {
  44.       hookBindings.put(MigrationHook.HOOK_CONTEXT, new NewHookContext(description, filename));
  45.       hook.before(hookBindings);
  46.     }

  47.     if (options.getTemplate() != null) {
  48.       copyExternalResourceTo(options.getTemplate(), Util.file(paths.getScriptPath(), filename), variables);
  49.     } else {
  50.       String customConfiguredTemplate = Util.getPropertyOption(CUSTOM_NEW_COMMAND_TEMPLATE_PROPERTY);
  51.       if (customConfiguredTemplate != null && !customConfiguredTemplate.isEmpty()) {
  52.         copyExternalResourceTo(Util.migrationsHome() + File.separator + customConfiguredTemplate,
  53.             Util.file(paths.getScriptPath(), filename), variables);
  54.       } else {
  55.         printStream
  56.             .append("Your migrations configuration did not find your custom template.  Using the default template.");
  57.         copyDefaultTemplate(variables, filename);
  58.       }
  59.       if (hook != null) {
  60.         hookBindings.put(MigrationHook.HOOK_CONTEXT, new NewHookContext(description, filename));
  61.         hook.after(hookBindings);
  62.       }
  63.     }
  64.     printStream.println("Done!");
  65.     printStream.println();
  66.   }

  67.   private void copyDefaultTemplate(Properties variables, String filename) {
  68.     copyResourceTo("org/apache/ibatis/migration/template_migration.sql", Util.file(paths.getScriptPath(), filename),
  69.         variables);
  70.   }

  71.   private MigrationHook createNewHook() {
  72.     String before = environment().getHookBeforeNew();
  73.     String after = environment().getHookAfterNew();
  74.     if (before == null && after == null) {
  75.       return null;
  76.     }
  77.     return createFileMigrationHook(before, null, null, after);
  78.   }
  79. }