All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mybatis.maven.mvnmigrate.CheckCommandMojo Maven / Gradle / Ivy

Go to download

Migration-plugin is a Apache Maven 2 plugin that aims to integrate into the maven life cycle, the myBatis 3 Migration tool. MyBatis 3 Migration Schema is a tool that helps you to manage database schema changes.

There is a newer version: 1.2.0
Show newest version
/*
 *    Copyright 2010 The myBatis Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.maven.mvnmigrate;

import java.text.MessageFormat;
import java.util.List;

import org.apache.ibatis.migration.Change;
import org.apache.ibatis.migration.commands.StatusCommand;
import org.apache.ibatis.migration.operations.StatusOperation;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * Goal which check the presence of pending migration.
 *
 * @goal check
 * @phase test
 * @version $Id$
 */
public final class CheckCommandMojo extends StatusCommandMojo {

    private static final String LINE_SEPARATOR = System.getProperty("line.separator","\n");

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (isSkip()) return;

        init();

        if (getCommand() instanceof StatusCommand) {
            StatusCommand command = (StatusCommand)getCommand();
            command.execute();
            StatusOperation operation = command.getOperation();
            List changes = operation.getCurrentStatus();
            int pendings = operation.getPendingCount();
            if ( pendings > 0 ) {
                Integer[] args = { pendings };
                MessageFormat format = new MessageFormat(getBundle(this.getLocale()).getString("migration.plugin.execution.check.failed"));
                throw new MojoFailureException(this, LINE_SEPARATOR + format.format(args), createLongMessage(changes));
            }
        }
    }

    /**
     * Creates a user information message about all migration pending changes
     *
     * @param changes List of migration changes
     * @return User information message about all migration pending changes.
     */
    private String createLongMessage(List changes) {
        StringBuilder builder = new StringBuilder();
        builder.append("ID             Applied At          Description");
        builder.append(LINE_SEPARATOR);
        for (Change change : changes) {
            builder.append(change);
            builder.append(LINE_SEPARATOR);
        }
        return builder.toString();
    }

    @Override
    public String toString() {
        return getBundle(this.getLocale()).getString("migration.plugin.name") + " " + this.getClass().getSimpleName();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy