InitializeCommand.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.Properties;

  19. import org.apache.ibatis.migration.MigrationException;
  20. import org.apache.ibatis.migration.options.SelectedOptions;
  21. import org.apache.ibatis.migration.utils.Util;

  22. public final class InitializeCommand extends BaseCommand {
  23.   public InitializeCommand(SelectedOptions selectedOptions) {
  24.     super(selectedOptions);
  25.   }

  26.   @Override
  27.   public void execute(String... params) {
  28.     final File basePath = paths.getBasePath();
  29.     final File scriptPath = paths.getScriptPath();

  30.     printStream.println("Initializing: " + basePath);

  31.     createDirectoryIfNecessary(basePath);
  32.     ensureDirectoryIsEmpty(basePath);

  33.     createDirectoryIfNecessary(paths.getEnvPath());
  34.     createDirectoryIfNecessary(scriptPath);
  35.     createDirectoryIfNecessary(paths.getDriverPath());

  36.     copyResourceTo("org/apache/ibatis/migration/template_README", Util.file(basePath, "README"));
  37.     copyResourceTo("org/apache/ibatis/migration/template_environment.properties", environmentFile());
  38.     copyResourceTo("org/apache/ibatis/migration/template_bootstrap.sql", Util.file(scriptPath, "bootstrap.sql"));
  39.     copyResourceTo("org/apache/ibatis/migration/template_changelog.sql",
  40.         Util.file(scriptPath, getNextIDAsString() + "_" + DESC_CREATE_CHANGELOG.replace(' ', '_') + ".sql"));

  41.     Properties firstMigration = new Properties();
  42.     firstMigration.setProperty("description", "First migration.");
  43.     copyResourceTo("org/apache/ibatis/migration/template_migration.sql",
  44.         Util.file(scriptPath, getNextIDAsString() + "_first_migration.sql"), firstMigration);
  45.     printStream.println("Done!");
  46.     printStream.println();
  47.   }

  48.   protected void ensureDirectoryIsEmpty(File path) {
  49.     String[] list = path.list();
  50.     if (list.length != 0) {
  51.       for (String entry : list) {
  52.         if (!entry.startsWith(".")) {
  53.           throw new MigrationException("Directory must be empty (.svn etc allowed): " + path.getAbsolutePath());
  54.         }
  55.       }
  56.     }
  57.   }

  58.   protected void createDirectoryIfNecessary(File path) {
  59.     if (!path.exists()) {
  60.       printStream.println("Creating: " + path.getName());
  61.       if (!path.mkdirs()) {
  62.         throw new MigrationException(
  63.             "Could not create directory path for an unknown reason. Make sure you have access to the directory.");
  64.       }
  65.     }
  66.   }

  67. }