• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / migrations / 847

01 Jan 2026 09:03PM UTC coverage: 79.903% (-1.0%) from 80.917%
847

push

github

web-flow
Merge pull request #490 from hazendaz/master

[mvn] Add hsqldb method class names back to maven.config

679 of 986 branches covered (68.86%)

1813 of 2269 relevant lines covered (79.9%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.31
/src/main/java/org/apache/ibatis/migration/CommandLine.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

18
import static org.apache.ibatis.migration.options.OptionsParser.parse;
19

20
import java.io.File;
21
import java.io.PrintStream;
22
import java.util.Date;
23

24
import org.apache.ibatis.migration.commands.Command;
25
import org.apache.ibatis.migration.commands.Commands;
26
import org.apache.ibatis.migration.commands.InfoCommand;
27
import org.apache.ibatis.migration.commands.InitializeCommand;
28
import org.apache.ibatis.migration.options.Options;
29
import org.apache.ibatis.migration.options.SelectedOptions;
30
import org.apache.ibatis.migration.utils.Util;
31

32
public class CommandLine {
33
  // Leave this non static as it is messed with in this project tests
34
  private final PrintStream console = System.out;
1✔
35
  private final String[] args;
36

37
  public CommandLine(String[] args) {
1✔
38
    this.args = args;
1✔
39
  }
1✔
40

41
  public void execute() {
42
    final SelectedOptions selectedOptions = parse(args);
1✔
43
    if (selectedOptions.needsHelp()) {
1✔
44
      printUsage();
1✔
45
      return;
1✔
46
    }
47
    if (selectedOptions.getCommand() == null) {
1!
48
      console.printf("No command specified.%n");
×
49
      printUsage();
×
50
      return;
×
51
    }
52
    try {
53
      Command command = Commands.resolveCommand(selectedOptions.getCommand(), selectedOptions);
1✔
54
      if (command instanceof InitializeCommand || command instanceof InfoCommand
1✔
55
          || validBasePath(selectedOptions.getPaths().getBasePath())) {
1!
56
        runCommand(command, selectedOptions);
1✔
57
      }
58
    } catch (Exception e) {
×
59
      String errorMessage = e.getMessage();
×
60

61
      if (hasColor(selectedOptions)) {
×
62
        console.printf(ConsoleColors.RED + "%nERROR: %s%n", errorMessage + ConsoleColors.RESET);
×
63
      } else {
64
        console.printf("%nERROR: %s%n", errorMessage);
×
65
      }
66

67
      if (selectedOptions.isTrace()) {
×
68
        e.printStackTrace();
×
69
      }
70
      console.close();
×
71
      System.exit(1); // Issue 730
×
72
    }
1✔
73
    console.close();
1✔
74
  }
1✔
75

76
  private void runCommand(Command command, SelectedOptions selectedOptions) {
77
    console.printf("------------------------------------------------------------------------%n");
1✔
78
    console.printf("-- MyBatis Migrations - %s%n", selectedOptions.getCommand());
1✔
79
    console.printf("------------------------------------------------------------------------%n");
1✔
80

81
    long start = System.currentTimeMillis();
1✔
82
    boolean exceptionCaught = false;
1✔
83

84
    try {
85
      command.execute(selectedOptions.getParams());
1✔
86
    } catch (Throwable t) {
×
87
      exceptionCaught = true;
×
88
      if (t instanceof MigrationException) {
×
89
        throw (MigrationException) t;
×
90
      }
91
      throw new MigrationException(t);
×
92
    } finally {
93
      console.printf("------------------------------------------------------------------------%n");
1✔
94

95
      if (hasColor(selectedOptions)) {
1✔
96
        console.printf("-- MyBatis Migrations %s%s%s%n", exceptionCaught ? ConsoleColors.RED : ConsoleColors.GREEN,
1!
97
            exceptionCaught ? "FAILURE" : "SUCCESS", ConsoleColors.RESET);
1!
98
      } else {
99
        console.printf("-- MyBatis Migrations %s%n", exceptionCaught ? "FAILURE" : "SUCCESS");
1!
100
      }
101

102
      console.printf("-- Total time: %ss%n", (System.currentTimeMillis() - start) / 1000);
1✔
103
      console.printf("-- Finished at: %s%n", new Date());
1✔
104
      printMemoryUsage();
1✔
105
      console.printf("------------------------------------------------------------------------%n");
1✔
106
    }
107
  }
1✔
108

109
  protected boolean hasColor(SelectedOptions selectedOptions) {
110
    return selectedOptions.hasColor() || Util.getPropertyOptionAsBoolean(Options.COLOR.toString().toLowerCase());
1!
111
  }
112

113
  private void printMemoryUsage() {
114
    final Runtime runtime = Runtime.getRuntime();
1✔
115
    final int megaUnit = 1024 * 1024;
1✔
116
    final long usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / megaUnit;
1✔
117
    final long totalMemory = runtime.totalMemory() / megaUnit;
1✔
118

119
    console.printf("-- Final Memory: %sM/%sM%n", usedMemory, totalMemory);
1✔
120
  }
1✔
121

122
  private boolean validBasePath(File basePath) {
123
    final boolean validDirectory = basePath.exists() && basePath.isDirectory();
1!
124

125
    if (!validDirectory) {
1!
126
      console.printf("Migrations path must be a directory: %s%n", basePath.getAbsolutePath());
×
127
    }
128

129
    return validDirectory;
1✔
130
  }
131

132
  private void printUsage() {
133
    console.printf(
1✔
134
        "%nUsage: migrate command [parameter] [--path=<directory>] [--env=<environment>] [--template=<path to custom template>]%n%n");
135
    console.printf("--path=<directory>   Path to repository.  Default current working directory.%n");
1✔
136
    console.printf("--env=<environment>  Environment to configure. Default environment is 'development'.%n");
1✔
137
    console.printf("--template=<template>  Path to custom template for creating new sql scripts.%n");
1✔
138
    console.printf("--force              Forces script to continue even if SQL errors are encountered.%n");
1✔
139
    console.printf("--help               Displays this usage message.%n");
1✔
140
    console.printf("--trace              Shows additional error details (if any).%n");
1✔
141
    console.printf("--quiet              Suppresses output.%n");
1✔
142
    console.printf("--color              Colorize output.%n");
1✔
143
    console.printf("%n");
1✔
144
    console.printf("Commands:%n");
1✔
145
    console.printf("  info               Display build version informations.%n");
1✔
146
    console.printf("  init               Creates (if necessary) and initializes a migration path.%n");
1✔
147
    console.printf("  bootstrap          Runs the bootstrap SQL script (see scripts/bootstrap.sql for more).%n");
1✔
148
    console.printf("  new <description>  Creates a new migration with the provided description.%n");
1✔
149
    console.printf("  up [n]             Run unapplied migrations, ALL by default, or 'n' specified.%n");
1✔
150
    console
1✔
151
        .printf("  down [n]           Undoes migrations applied to the database. ONE by default or 'n' specified.%n");
1✔
152
    console.printf("  version <version>  Migrates the database up or down to the specified version.%n");
1✔
153
    console.printf("  pending            Force executes pending migrations out of order (not recommended).%n");
1✔
154
    console.printf("  status             Prints the changelog from the database if the changelog table exists.%n");
1✔
155
    console
1✔
156
        .printf("  script <v1> <v2>   Generates a delta migration script from version v1 to v2 (undo if v1 > v2).%n");
1✔
157
    console.printf("%n");
1✔
158
    console.printf("  * Shortcuts are accepted by using the first few (unambiguous) letters of each command..%n");
1✔
159
    console.printf("%n");
1✔
160
  }
1✔
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc