View Javadoc
1   /*
2    *    Copyright 2010-2024 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.mybatis.maven.mvnmigrate;
17  
18  import java.text.MessageFormat;
19  import java.util.List;
20  
21  import org.apache.ibatis.migration.Change;
22  import org.apache.ibatis.migration.commands.StatusCommand;
23  import org.apache.ibatis.migration.operations.StatusOperation;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.LifecyclePhase;
27  import org.apache.maven.plugins.annotations.Mojo;
28  
29  /**
30   * Goal which check the presence of pending migration.
31   */
32  @Mojo(name = "check", defaultPhase = LifecyclePhase.TEST)
33  public final class CheckCommandMojo extends StatusCommandMojo {
34  
35    private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
36  
37    /**
38     * {@inheritDoc}
39     */
40    @Override
41    public void execute() throws MojoExecutionException, MojoFailureException {
42      if (isSkip()) {
43        return;
44      }
45  
46      init();
47  
48      if (getCommand() instanceof StatusCommand) {
49        StatusCommand command = getCommand();
50        command.execute();
51        StatusOperation operation = command.getOperation();
52        List<Change> changes = operation.getCurrentStatus();
53        int pendings = operation.getPendingCount();
54        if (pendings > 0) {
55          Integer[] args = { pendings };
56          MessageFormat format = new MessageFormat(
57              getBundle(this.getLocale()).getString("migration.plugin.execution.check.failed"));
58          throw new MojoFailureException(this, LINE_SEPARATOR + format.format(args), createLongMessage(changes));
59        }
60      }
61    }
62  
63    /**
64     * Creates a user information message about all migration pending changes
65     *
66     * @param changes
67     *          List of migration changes
68     *
69     * @return User information message about all migration pending changes.
70     */
71    private String createLongMessage(List<Change> changes) {
72      StringBuilder builder = new StringBuilder();
73      builder.append("ID             Applied At          Description");
74      builder.append(LINE_SEPARATOR);
75      for (Change change : changes) {
76        builder.append(change);
77        builder.append(LINE_SEPARATOR);
78      }
79      return builder.toString();
80    }
81  
82    @Override
83    public String toString() {
84      return getBundle(this.getLocale()).getString("migration.plugin.name") + " " + this.getClass().getSimpleName();
85    }
86  
87  }