Skip to content

Commit

Permalink
Refactor liquify to follow new argument format.
Browse files Browse the repository at this point in the history
Refactor argument parser to parse arguments in new format.
Create target file name builder to create the file name based on arguments.
Modify usage printer to update usage display.
Add new database parameter to ConversionArguments.
Modify Liquify to handle exceptions better.
Modify README to reflect the project better.
  • Loading branch information
daquino committed Jun 26, 2015
1 parent 3f05cc0 commit aed1ff0
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 31 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# liquifier

Liquifier is a command-line tool to convert Liquibase changelog files
between the supported file formats(xml,yaml,json,sql).

Usage:

liquify [-options] <source>

Options:
Required:
-t, --type Change log file type to convert to (xml,yaml,json,sql).

Optional:
-db, --database Database type to use when using the sql type (i.e oracle, h2, etc).

Example Usage:
liquify -t sql -db oracle db.changelog.xml (creates db.changelog.oracle.sql with the oracle dialect).
38 changes: 29 additions & 9 deletions src/main/java/com/refactify/Liquify.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.refactify.arguments.ConversionArguments;
import com.refactify.arguments.ConversionArgumentsParser;
import com.refactify.arguments.TargetFileNameBuilder;
import com.refactify.printer.UsagePrinter;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.exception.LiquibaseException;
Expand All @@ -16,13 +18,16 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Liquify {
private final static ConversionArgumentsParser parser = new ConversionArgumentsParser();
private final static UsagePrinter usagePrinter = new UsagePrinter();
private final static TargetFileNameBuilder targetFileNameBuilder = new TargetFileNameBuilder();

public static void main(final String[] args) {
ConversionArguments conversionArguments = parser.parseArguments(args);

if(conversionArguments.areValid()) {
convertDatabaseChangeLog(conversionArguments);
}
Expand All @@ -32,24 +37,39 @@ public static void main(final String[] args) {
}

private static void convertDatabaseChangeLog(final ConversionArguments conversionArguments) {
String targetFileName = targetFileNameBuilder.buildFilename(conversionArguments);
try {
ResourceAccessor resourceAccessor = new FileSystemResourceAccessor(System.getProperty("user.dir"));
ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser(conversionArguments.getSource(), resourceAccessor);
DatabaseChangeLog changeLog = parser.parse(conversionArguments.getSource(), null, resourceAccessor);
ChangeLogSerializer serializer = ChangeLogSerializerFactory.getInstance().getSerializer(conversionArguments.getDestination());
DatabaseChangeLog changeLog = parser.parse(conversionArguments.getSource(), new ChangeLogParameters(), resourceAccessor);
ChangeLogSerializer serializer = ChangeLogSerializerFactory.getInstance().getSerializer(targetFileName);
for (ChangeSet set : changeLog.getChangeSets()) {
set.setFilePath(conversionArguments.getDestination());
set.setFilePath(targetFileName);
}
serializer.write(changeLog.getChangeSets(), new FileOutputStream(conversionArguments.getDestination()));
serializer.write(changeLog.getChangeSets(), new FileOutputStream(targetFileName));
}
catch (LiquibaseException e) {
e.printStackTrace();
}
catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("There was a problem parsing the source file.");
deleteTargetFile(targetFileName);
}
catch (IOException e) {
System.out.println("There was a problem serializing the source file.");
deleteTargetFile(targetFileName);
}
catch(IllegalStateException e) {
e.printStackTrace();
System.out.println(String.format("Database generator for type '%s' was not found.",
conversionArguments.getDatabase()));
deleteTargetFile(targetFileName);
}
}

private static void deleteTargetFile(final String targetFileName) {
try {
Files.deleteIfExists(Paths.get(targetFileName));
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
85 changes: 78 additions & 7 deletions src/main/java/com/refactify/arguments/ConversionArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

public class ConversionArguments {
private String source;
private String destination;
private static final String[] validFileExtensions = new String[] {"xml", "yaml", "json", "sql"};
private ConversionType type;
private String database;
private static final String[] validFileExtensions = new String[] {".xml", ".yaml", ".json", ".sql"};
private static final String[] validDatabases =
new String[] {"db2", "derby", "firebird", "h2", "hsql", "informix", "mssql", "mariadb", "mysql", "oracle",
"postgresql", "sqlite", "asany", "sybase"};

public String getSource() {
return source;
Expand All @@ -13,16 +17,24 @@ public void setSource(final String source) {
this.source = source;
}

public String getDestination() {
return destination;
public ConversionType getConversionType() {
return type;
}

public void setDestination(final String destination) {
this.destination = destination;
public void setConversionType(final ConversionType type) {
this.type = type;
}

public String getDatabase() {
return database;
}

public void setDatabase(final String database) {
this.database = database;
}

public boolean areValid() {
return isValidPath(source) && isValidPath(destination);
return isValidPath(source) && hasValidType();
}

private boolean isValidPath(final String path) {
Expand All @@ -39,4 +51,63 @@ private boolean hasValidExtension(final String path) {
}
return validExtension;
}

public boolean hasValidType() {
if(type == null) {
return false;
}
else if(!type.equals(ConversionType.SQL)) {
return true;
}
else if(type.equals(ConversionType.SQL) && databaseIsSupported()) {
return true;
}
else {
return false;
}

}

public boolean databaseIsSupported() {
boolean validDatabase = false;
if(database != null) {
for (String db : validDatabases) {
if (database.equals(db)) {
validDatabase = true;
break;
}
}
}
return validDatabase;
}

public enum ConversionType {
XML("xml"), YAML("yaml"), JSON("json"), SQL("sql");

ConversionType(final String extension) {
this.extension = extension;
}
private String extension;

public String getExtension() {
return extension;
}

public static ConversionType fromString(final String value) {
if("xml".equals(value)) {
return XML;
}
else if("yaml".equals(value)) {
return YAML;
}
else if("json".equals(value)) {
return JSON;
}
else if ("sql".equals(value)) {
return SQL;
}
else
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package com.refactify.arguments;

import java.util.ArrayList;
import java.util.List;

public class ConversionArgumentsParser {

public ConversionArguments parseArguments(String[] arguments) {
ConversionArguments conversionArguments = new ConversionArguments();
try {
for (int i = 0; i < arguments.length; i++) {
conversionArguments.setSource(arguments[arguments.length - 1]);
for (int i = 0; i < arguments.length - 1; i++) {
String argument = arguments[i];
if (argument.equals("-s") || argument.equals("--source")) {
if (argument.equals("-t") || argument.equals("--type")) {
String value = arguments[++i];
conversionArguments.setSource(value);
conversionArguments.setConversionType(ConversionArguments.ConversionType.fromString(value));
}
else if (argument.equals("-d") || argument.equals("--dest")) {
else if (argument.equals("-db") || argument.equals("--database")) {
String value = arguments[++i];
conversionArguments.setDestination(value);
conversionArguments.setDatabase(value);
}
}
}
catch(ArrayIndexOutOfBoundsException exc) {
catch (ArrayIndexOutOfBoundsException exc) {
System.out.println("Invalid arguments.");
}
return conversionArguments;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/refactify/arguments/TargetFileNameBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.refactify.arguments;

public class TargetFileNameBuilder {
public String buildFilename(final ConversionArguments arguments) {
String fileName;
String source = arguments.getSource();
String baseFileName = source.substring(0, source.lastIndexOf("."));
if(arguments.getConversionType().equals(ConversionArguments.ConversionType.SQL)) {
fileName = String.format("%s.%s.%s", baseFileName, arguments.getDatabase(),
arguments.getConversionType().getExtension());
}
else {
fileName = String.format("%s.%s", baseFileName, arguments.getConversionType().getExtension());
}
return fileName;
}
}
16 changes: 10 additions & 6 deletions src/main/java/com/refactify/printer/UsagePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

public class UsagePrinter {
public void printUsage() {
System.out.println("Liquify can be use to convert Liquibase changelog " +
"files between the supported file formats: xml,yaml,json,sql.");
System.out.println("====================================");
System.out.println();
System.out.println("Usage: ");
System.out.println(" liquify -s <source> -d <dest>");
System.out.println(" liquify [-options] <source>");
System.out.println("");
System.out.println(" Options:");
System.out.println(" -s,--source Database changelog source file.");
System.out.println(" -d,--dest Database changelog destination file.");
System.out.println(" Required:");
System.out.println(" -t,--type Target changelog file type(xml,yaml,json,sql).");
System.out.println(" Optional:");
System.out.println(" -db,--database Database type to use when using the sql type (i.e oracle, h2, etc).");
System.out.println("\n" +
"Example Usage:\n" +
"liquify -t sql -db oracle db.changelog.xml (creates db.changelog.oracle.sql with the oracle dialect).");
System.out.println();
}
}

0 comments on commit aed1ff0

Please sign in to comment.