Skip to content

Commit

Permalink
Added support for zipped files
Browse files Browse the repository at this point in the history
  • Loading branch information
rkorytkowski committed Nov 30, 2016
1 parent 892ed11 commit d1d5697
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 561 deletions.
Expand Up @@ -25,6 +25,8 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import liquibase.ClassLoaderFileOpener;
import liquibase.FileSystemFileOpener;
Expand Down Expand Up @@ -95,8 +97,15 @@ private String createTempChangeLogFile(final String changeLogFile) throws IOExce
input = ClassLoader.getSystemResourceAsStream("sqlfile-changeset.xml");
String changeset = IOUtils.toString(input, "UTF-8");
input.close();

changeset = changeset.replace("${sqlfile}", changeLogFile);

File file = new File(changeLogFile);
if (file.getName().endsWith(".zip")) {
File unzippedFile = extractSqlFromZip(file);

changeset = changeset.replace("${sqlfile}", unzippedFile.getAbsolutePath());
} else {
changeset = changeset.replace("${sqlfile}", file.getAbsolutePath());
}

tempFile = File.createTempFile("liquibaserunner", ".xml");

Expand All @@ -114,6 +123,45 @@ private String createTempChangeLogFile(final String changeLogFile) throws IOExce
IOUtils.closeQuietly(output);
}
}

private File extractSqlFromZip(File file) throws IOException {
ZipFile zipFile = null;
FileOutputStream unzippedFileOut = null;
try {
zipFile = new ZipFile(file);
ZipEntry zipEntry = null;
while (zipFile.entries().hasMoreElements()) {
zipEntry = zipFile.entries().nextElement();
if (zipEntry.getName().endsWith(".sql")) {
break;
} else {
zipEntry = null;
}
}
if (zipEntry == null) {
throw new IllegalArgumentException(file.getName() + " does not contain any .sql file");
}

File unzippedFile = File.createTempFile(StringUtils.stripEnd(file.getName(), ".zip"), ".sql");
unzippedFileOut = new FileOutputStream(unzippedFile);

IOUtils.copy(zipFile.getInputStream(zipEntry), unzippedFileOut);

zipFile.close();
unzippedFileOut.close();

return unzippedFile;
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (Exception e) {
//close quietly
}
}
IOUtils.closeQuietly(unzippedFileOut);
}
}

public void update() throws LiquibaseRunnerException {
try {
Expand Down
Expand Up @@ -25,6 +25,8 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import liquibase.Liquibase;
import liquibase.database.Database;
Expand Down Expand Up @@ -97,8 +99,15 @@ private String createTempChangeLogFile(final String changeLogFile) throws IOExce
input = ClassLoader.getSystemResourceAsStream("sqlfile-changeset.xml");
String changeset = IOUtils.toString(input, "UTF-8");
input.close();

changeset = changeset.replace("${sqlfile}", changeLogFile);

File file = new File(changeLogFile);
if (file.getName().endsWith(".zip")) {
File unzippedFile = extractSqlFromZip(file);

changeset = changeset.replace("${sqlfile}", unzippedFile.getAbsolutePath());
} else {
changeset = changeset.replace("${sqlfile}", file.getAbsolutePath());
}

tempFile = File.createTempFile("liquibaserunner", ".xml");

Expand All @@ -116,6 +125,45 @@ private String createTempChangeLogFile(final String changeLogFile) throws IOExce
IOUtils.closeQuietly(output);
}
}

private File extractSqlFromZip(File file) throws IOException {
ZipFile zipFile = null;
FileOutputStream unzippedFileOut = null;
try {
zipFile = new ZipFile(file);
ZipEntry zipEntry = null;
while (zipFile.entries().hasMoreElements()) {
zipEntry = zipFile.entries().nextElement();
if (zipEntry.getName().endsWith(".sql")) {
break;
} else {
zipEntry = null;
}
}
if (zipEntry == null) {
throw new IllegalArgumentException(file.getName() + " does not contain any .sql file");
}

File unzippedFile = File.createTempFile(StringUtils.stripEnd(file.getName(), ".zip"), ".sql");
unzippedFileOut = new FileOutputStream(unzippedFile);

IOUtils.copy(zipFile.getInputStream(zipEntry), unzippedFileOut);

zipFile.close();
unzippedFileOut.close();

return unzippedFile;
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (Exception e) {
//close quietly
}
}
IOUtils.closeQuietly(unzippedFileOut);
}
}

public void update() throws LiquibaseRunnerException {
try {
Expand Down
557 changes: 0 additions & 557 deletions openmrs_concepts_1_6.sql

This file was deleted.

Binary file added openmrs_concepts_1_6.zip
Binary file not shown.

0 comments on commit d1d5697

Please sign in to comment.