FileMigrationLoader.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;

  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.Reader;
  20. import java.math.BigDecimal;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.Properties;

  25. import org.apache.ibatis.migration.utils.Util;

  26. public class FileMigrationLoader implements MigrationLoader {
  27.   protected final File scriptsDir;

  28.   protected final String charset;

  29.   protected final Properties variables;

  30.   public FileMigrationLoader(File scriptsDir, String charset, Properties variables) {
  31.     this.scriptsDir = scriptsDir;
  32.     this.charset = charset;
  33.     this.variables = variables;
  34.   }

  35.   @Override
  36.   public List<Change> getMigrations() {
  37.     List<Change> migrations = new ArrayList<>();
  38.     if (scriptsDir.isDirectory()) {
  39.       String[] filenames = scriptsDir.list();
  40.       if (filenames == null) {
  41.         throw new MigrationException(scriptsDir + " does not exist.");
  42.       }
  43.       Arrays.sort(filenames);
  44.       for (String filename : filenames) {
  45.         if (filename.endsWith(".sql") && !isSpecialFile(filename)) {
  46.           Change change = parseChangeFromFilename(filename);
  47.           migrations.add(change);
  48.         }
  49.       }
  50.     }
  51.     return migrations;
  52.   }

  53.   protected boolean isSpecialFile(String filename) {
  54.     return "bootstrap.sql".equals(filename) || "onabort.sql".equals(filename);
  55.   }

  56.   protected Change parseChangeFromFilename(String filename) {
  57.     try {
  58.       Change change = new Change();
  59.       int lastIndexOfDot = filename.lastIndexOf('.');
  60.       String[] parts = filename.substring(0, lastIndexOfDot).split("_");
  61.       change.setId(new BigDecimal(parts[0]));
  62.       StringBuilder builder = new StringBuilder();
  63.       for (int i = 1; i < parts.length; i++) {
  64.         if (i > 1) {
  65.           builder.append(" ");
  66.         }
  67.         builder.append(parts[i]);
  68.       }
  69.       change.setDescription(builder.toString());
  70.       change.setFilename(filename);
  71.       return change;
  72.     } catch (Exception e) {
  73.       throw new MigrationException("Error parsing change from file.  Cause: " + e, e);
  74.     }
  75.   }

  76.   @Override
  77.   public Reader getScriptReader(Change change, boolean undo) {
  78.     try {
  79.       return new MigrationReader(Util.file(scriptsDir, change.getFilename()), charset, undo, variables);
  80.     } catch (IOException e) {
  81.       throw new MigrationException("Error reading " + change.getFilename(), e);
  82.     }
  83.   }

  84.   @Override
  85.   public Reader getBootstrapReader() {
  86.     String fileName = "bootstrap.sql";
  87.     return getSoleScriptReader(fileName);
  88.   }

  89.   @Override
  90.   public Reader getOnAbortReader() {
  91.     String fileName = "onabort.sql";
  92.     return getSoleScriptReader(fileName);
  93.   }

  94.   protected Reader getSoleScriptReader(String fileName) {
  95.     try {
  96.       File scriptFile = Util.file(scriptsDir, fileName);
  97.       if (scriptFile.exists()) {
  98.         return new MigrationReader(scriptFile, charset, false, variables);
  99.       }
  100.       return null;
  101.     } catch (IOException e) {
  102.       throw new MigrationException("Error reading " + fileName, e);
  103.     }
  104.   }
  105. }