Coverage Report - org.liquibase.maven.plugins.LiquibaseUpdateSQL
 
Classes in this File Line Coverage Branch Coverage Complexity
LiquibaseUpdateSQL
0%
0/28
0%
0/8
2.6
 
 1  
 package org.liquibase.maven.plugins;
 2  
 
 3  
 import java.io.*;
 4  
 import liquibase.resource.ResourceAccessor;
 5  
 import liquibase.Liquibase;
 6  
 import liquibase.database.Database;
 7  
 import liquibase.exception.LiquibaseException;
 8  
 import org.apache.maven.plugin.MojoExecutionException;
 9  
 
 10  
 /**
 11  
  * Generates the SQL that is required to update the database to the current version as specified in the
 12  
  * DatabaseChangeLogs.
 13  
  * 
 14  
  * @author Peter Murray
 15  
  * @description Liquibase UpdateSQL Maven plugin
 16  
  * @goal updateSQL
 17  
  */
 18  0
 public class LiquibaseUpdateSQL extends AbstractLiquibaseUpdateMojo {
 19  
 
 20  
     /**
 21  
      * The file to output the Migration SQL script to, if it exists it will be overwritten.
 22  
      * 
 23  
      * @parameter expression="${liquibase.migrationSqlOutputFile}" default-value=
 24  
      *            "${project.build.directory}/liquibase/migrate.sql"
 25  
      */
 26  
     protected File migrationSqlOutputFile;
 27  
 
 28  
     /** The writer for writing the migration SQL. */
 29  
     private Writer outputWriter;
 30  
 
 31  
     @Override
 32  
     protected boolean isPromptOnNonLocalDatabase() {
 33  
         // Always run on an non-local database as we are not actually modifying
 34  
         // the database
 35  
         // when run on it.
 36  0
         return false;
 37  
     }
 38  
 
 39  
     @Override
 40  
     protected void doUpdate(Liquibase liquibase) throws LiquibaseException {
 41  0
         if (changesToApply > 0) {
 42  0
             liquibase.update(changesToApply, contexts, outputWriter);
 43  
         } else {
 44  0
             liquibase.update(contexts, outputWriter);
 45  
         }
 46  0
     }
 47  
 
 48  
     @Override
 49  
     protected Liquibase createLiquibase(ResourceAccessor fo, Database db) throws MojoExecutionException {
 50  0
         Liquibase liquibase = super.createLiquibase(fo, db);
 51  
 
 52  
         // Setup the output file writer
 53  
         try {
 54  0
             if (!migrationSqlOutputFile.exists()) {
 55  
                 // Ensure the parent directories exist
 56  0
                 migrationSqlOutputFile.getParentFile().mkdirs();
 57  
                 // Create the actual file
 58  0
                 if (!migrationSqlOutputFile.createNewFile()) {
 59  0
                     throw new MojoExecutionException("Cannot create the migration SQL file; "
 60  
                             + migrationSqlOutputFile.getAbsolutePath());
 61  
                 }
 62  
             }
 63  0
             outputWriter = new FileWriter(migrationSqlOutputFile);
 64  0
         } catch (IOException e) {
 65  0
             getLog().error(e);
 66  0
             throw new MojoExecutionException("Failed to create SQL output writer", e);
 67  0
         }
 68  0
         getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath());
 69  0
         return liquibase;
 70  
     }
 71  
 
 72  
     @Override
 73  
     protected void printSettings(String indent) {
 74  0
         super.printSettings(indent);
 75  0
         getLog().info(indent + "migrationSQLOutputFile: " + migrationSqlOutputFile);
 76  0
     }
 77  
 
 78  
     @Override
 79  
     protected void cleanup(Database db) {
 80  0
         super.cleanup(db);
 81  0
         if (outputWriter != null) {
 82  
             try {
 83  0
                 outputWriter.close();
 84  0
             } catch (IOException e) {
 85  0
                 getLog().error(e);
 86  0
             }
 87  
         }
 88  0
     }
 89  
 }