Coverage Report - org.liquibase.maven.plugins.LiquibaseRollback
 
Classes in this File Line Coverage Branch Coverage Complexity
LiquibaseRollback
4%
2/41
0%
0/34
9.667
LiquibaseRollback$1
0%
0/1
N/A
9.667
LiquibaseRollback$RollbackType
0%
0/2
N/A
9.667
 
 1  
 // Version: $Id: $
 2  
 // Copyright: Copyright(c) 2007 Trace Financial Limited
 3  
 package org.liquibase.maven.plugins;
 4  
 
 5  
 import java.text.*;
 6  
 import liquibase.exception.LiquibaseException;
 7  
 import liquibase.Liquibase;
 8  
 import org.apache.maven.plugin.MojoFailureException;
 9  
 
 10  
 /**
 11  
  * Invokes Liquibase rollbacks on a database.
 12  
  * 
 13  
  * @author Peter Murray
 14  
  * @goal rollback
 15  
  */
 16  3
 public class LiquibaseRollback extends AbstractLiquibaseChangeLogMojo {
 17  
 
 18  3
     protected enum RollbackType {
 19  
 
 20  0
         TAG, DATE, COUNT
 21  
     }
 22  
 
 23  
     /**
 24  
      * The tag to roll the database back to.
 25  
      * 
 26  
      * @parameter expression="${liquibase.rollbackTag}"
 27  
      */
 28  
     protected String rollbackTag;
 29  
 
 30  
     /**
 31  
      * The number of change sets to rollback.
 32  
      * 
 33  
      * @parameter expression="${liquibase.rollbackCount}" default-value="-1"
 34  
      */
 35  
     protected int rollbackCount;
 36  
 
 37  
     /**
 38  
      * The date to rollback the database to. The format of the date must match that of the
 39  
      * <code>DateFormat.getDateInstance()</code> for the platform the plugin is executing on.
 40  
      * 
 41  
      * @parameter expression="${liquibase.rollbackDate}"
 42  
      */
 43  
     protected String rollbackDate;
 44  
 
 45  
     /** The type of the rollback that is being performed. */
 46  
     protected RollbackType type;
 47  
 
 48  
     @Override
 49  
     protected void checkRequiredParametersAreSpecified() throws MojoFailureException {
 50  0
         super.checkRequiredParametersAreSpecified();
 51  
 
 52  0
         if (rollbackCount == -1 && rollbackDate == null && rollbackTag == null) {
 53  0
             throw new MojoFailureException("One of the rollback options must be specified, "
 54  
                     + "please specify one of rollbackTag, rollbackCount " + "or rollbackDate");
 55  
         }
 56  
 
 57  0
         if (rollbackCount != -1 && rollbackCount <= 0) {
 58  0
             throw new MojoFailureException("A rollback count of " + rollbackCount + " is meaningless, please "
 59  
                     + "select a value greater than 0");
 60  
         }
 61  
 
 62  0
         String message = "Cannot specify multiple rollbackXXX options, please select only"
 63  
                 + " one of rollbackTag, rollbackCount, rollbackDate.";
 64  
 
 65  0
         if (rollbackCount > 0) {
 66  0
             if (rollbackDate != null || rollbackTag != null) {
 67  0
                 throw new MojoFailureException(message);
 68  
             }
 69  0
             type = RollbackType.COUNT;
 70  0
         } else if (rollbackDate != null) {
 71  0
             if (rollbackTag != null || rollbackCount > 0) {
 72  0
                 throw new MojoFailureException(message);
 73  
             }
 74  0
             type = RollbackType.DATE;
 75  0
         } else if (rollbackTag != null) {
 76  0
             if (rollbackCount > 0 || rollbackDate != null) {
 77  0
                 throw new MojoFailureException(message);
 78  
             }
 79  0
             type = RollbackType.TAG;
 80  
         }
 81  0
     }
 82  
 
 83  
     @Override
 84  
     protected void printSettings(String indent) {
 85  0
         super.printSettings(indent);
 86  0
         getLog().info(indent + "rollback Count: " + rollbackCount);
 87  0
         getLog().info(indent + "rollback Date: " + rollbackDate);
 88  0
         getLog().info(indent + "rollback Tag: " + rollbackTag);
 89  0
     }
 90  
 
 91  
     @Override
 92  
     protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
 93  0
         switch (type) {
 94  
         case COUNT: {
 95  0
             liquibase.rollback(rollbackCount, contexts);
 96  0
             break;
 97  
         }
 98  
         case DATE: {
 99  0
             DateFormat format = DateFormat.getDateInstance();
 100  
             try {
 101  0
                 liquibase.rollback(format.parse(rollbackDate), contexts);
 102  0
             } catch (ParseException e) {
 103  0
                 String message = "Error parsing rollbackDate: " + e.getMessage();
 104  0
                 if (format instanceof SimpleDateFormat) {
 105  0
                     message += "\nDate must match pattern: " + ((SimpleDateFormat) format).toPattern();
 106  
                 }
 107  0
                 throw new LiquibaseException(message, e);
 108  0
             }
 109  
             break;
 110  
         }
 111  
         case TAG: {
 112  0
             liquibase.rollback(rollbackTag, contexts);
 113  0
             break;
 114  
         }
 115  
         default: {
 116  0
             throw new IllegalStateException("Unexpected rollback type, " + type);
 117  
         }
 118  
         }
 119  0
     }
 120  
 }