View Javadoc
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  
18  import java.io.File;
19  import java.util.Properties;
20  
21  import org.apache.ibatis.migration.MigrationException;
22  import org.apache.ibatis.migration.options.SelectedOptions;
23  import org.apache.ibatis.migration.utils.Util;
24  
25  public final class InitializeCommand extends BaseCommand {
26    public InitializeCommand(SelectedOptions selectedOptions) {
27      super(selectedOptions);
28    }
29  
30    @Override
31    public void execute(String... params) {
32      final File basePath = paths.getBasePath();
33      final File scriptPath = paths.getScriptPath();
34  
35      printStream.println("Initializing: " + basePath);
36  
37      createDirectoryIfNecessary(basePath);
38      ensureDirectoryIsEmpty(basePath);
39  
40      createDirectoryIfNecessary(paths.getEnvPath());
41      createDirectoryIfNecessary(scriptPath);
42      createDirectoryIfNecessary(paths.getDriverPath());
43  
44      copyResourceTo("org/apache/ibatis/migration/template_README", Util.file(basePath, "README"));
45      copyResourceTo("org/apache/ibatis/migration/template_environment.properties", environmentFile());
46      copyResourceTo("org/apache/ibatis/migration/template_bootstrap.sql", Util.file(scriptPath, "bootstrap.sql"));
47      copyResourceTo("org/apache/ibatis/migration/template_changelog.sql",
48          Util.file(scriptPath, getNextIDAsString() + "_" + DESC_CREATE_CHANGELOG.replace(' ', '_') + ".sql"));
49  
50      Properties firstMigration = new Properties();
51      firstMigration.setProperty("description", "First migration.");
52      copyResourceTo("org/apache/ibatis/migration/template_migration.sql",
53          Util.file(scriptPath, getNextIDAsString() + "_first_migration.sql"), firstMigration);
54      printStream.println("Done!");
55      printStream.println();
56    }
57  
58    protected void ensureDirectoryIsEmpty(File path) {
59      String[] list = path.list();
60      if (list.length != 0) {
61        for (String entry : list) {
62          if (!entry.startsWith(".")) {
63            throw new MigrationException("Directory must be empty (.svn etc allowed): " + path.getAbsolutePath());
64          }
65        }
66      }
67    }
68  
69    protected void createDirectoryIfNecessary(File path) {
70      if (!path.exists()) {
71        printStream.println("Creating: " + path.getName());
72        if (!path.mkdirs()) {
73          throw new MigrationException(
74              "Could not create directory path for an unknown reason. Make sure you have access to the directory.");
75        }
76      }
77    }
78  
79  }