VersionOperation.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.operations;

  17. import java.io.PrintStream;
  18. import java.math.BigDecimal;
  19. import java.sql.Connection;
  20. import java.sql.SQLException;
  21. import java.util.Collections;
  22. import java.util.List;

  23. import org.apache.ibatis.migration.Change;
  24. import org.apache.ibatis.migration.ConnectionProvider;
  25. import org.apache.ibatis.migration.MigrationException;
  26. import org.apache.ibatis.migration.MigrationLoader;
  27. import org.apache.ibatis.migration.hook.MigrationHook;
  28. import org.apache.ibatis.migration.options.DatabaseOperationOption;

  29. public final class VersionOperation extends DatabaseOperation {
  30.   private BigDecimal version;

  31.   public VersionOperation(BigDecimal version) {
  32.     this.version = version;
  33.     if (version == null) {
  34.       throw new IllegalArgumentException("The version must be null.");
  35.     }
  36.   }

  37.   public VersionOperation operate(ConnectionProvider connectionProvider, MigrationLoader migrationsLoader,
  38.       DatabaseOperationOption option, PrintStream printStream) {
  39.     return operate(connectionProvider, migrationsLoader, option, printStream, null, null);
  40.   }

  41.   public VersionOperation operate(ConnectionProvider connectionProvider, MigrationLoader migrationsLoader,
  42.       DatabaseOperationOption option, PrintStream printStream, MigrationHook upHook, MigrationHook downHook) {
  43.     if (option == null) {
  44.       option = new DatabaseOperationOption();
  45.     }
  46.     try (Connection con = connectionProvider.getConnection()) {
  47.       List<Change> changesInDb = changelogExists(con, option) ? getChangelog(con, option) : Collections.emptyList();
  48.       List<Change> migrations = migrationsLoader.getMigrations();
  49.       Change specified = new Change(version);
  50.       if (!migrations.contains(specified)) {
  51.         throw new MigrationException("A migration for the specified version number does not exist.");
  52.       }
  53.       Change lastChangeInDb = changesInDb.isEmpty() ? null : changesInDb.get(changesInDb.size() - 1);
  54.       if (lastChangeInDb == null || specified.compareTo(lastChangeInDb) > 0) {
  55.         println(printStream, "Upgrading to: " + version);
  56.         int steps = 0;
  57.         for (Change change : migrations) {
  58.           if ((lastChangeInDb == null || change.compareTo(lastChangeInDb) > 0) && change.compareTo(specified) < 1) {
  59.             steps++;
  60.           }
  61.         }
  62.         new UpOperation(steps).operate(connectionProvider, migrationsLoader, option, printStream, upHook);
  63.       } else if (specified.compareTo(lastChangeInDb) < 0) {
  64.         println(printStream, "Downgrading to: " + version);
  65.         int steps = 0;
  66.         for (Change change : migrations) {
  67.           if (change.compareTo(specified) > -1 && change.compareTo(lastChangeInDb) < 0) {
  68.             steps++;
  69.           }
  70.         }
  71.         new DownOperation(steps).operate(connectionProvider, migrationsLoader, option, printStream, downHook);
  72.       } else {
  73.         println(printStream, "Already at version: " + version);
  74.       }
  75.       println(printStream);
  76.       return this;
  77.     } catch (SQLException e) {
  78.       throw new MigrationException("Error creating connection.  Cause: " + e, e);
  79.     }
  80.   }
  81. }