Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add general jdbc exporter for non H2 and sqlite use cases. #545

Merged
merged 2 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions data-migration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
<artifactId>data-migration</artifactId>
<packaging>jar</packaging>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.mockrunner</groupId>
<artifactId>mockrunner-jdbc</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

Expand All @@ -31,7 +42,7 @@
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<scope>compile</scope>
<scope>runtime</scope>
</dependency>

<dependency>
Expand All @@ -44,7 +55,7 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>compile</scope>
<scope>runtime</scope>
</dependency>

<dependency>
Expand All @@ -58,6 +69,13 @@
<artifactId>system-rules</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.mockrunner</groupId>
<artifactId>mockrunner-jdbc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@

package com.quorum.tessera.data.migration;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;


public class CmdLineExecutor {

private CmdLineExecutor() {
throw new UnsupportedOperationException("");
}


protected static int execute(String... args) throws Exception {
protected static int execute(String... args) throws Exception {

Options options = new Options();

Expand All @@ -47,6 +49,8 @@ protected static int execute(String... args) throws Exception {
.required()
.build());



options.addOption(
Option.builder()
.longOpt("exporttype")
Expand All @@ -57,6 +61,16 @@ protected static int execute(String... args) throws Exception {
.argName("TYPE")
.required()
.build());

options.addOption(
Option.builder()
.longOpt("dbconfig")
.desc("Properties file with create table, insert row and jdbc url")
.hasArg(true)
.optionalArg(false)
.numberOfArgs(1)
.argName("PATH")
.build());

options.addOption(
Option.builder()
Expand All @@ -71,25 +85,27 @@ protected static int execute(String... args) throws Exception {

options.addOption(
Option.builder()
.longOpt("dbuser")
.desc("Database username to use")
.hasArg(true)
.optionalArg(true)
.numberOfArgs(1)
.argName("PATH")
.required()
.build());
.longOpt("dbuser")
.desc("Database username to use")
.hasArg(true)
.optionalArg(true)
.numberOfArgs(1)
.argName("USER")
.required()
.build());

options.addOption(
Option.builder()
.longOpt("dbpass")
.desc("Database password to use")
.hasArg(true)
.optionalArg(true)
.numberOfArgs(1)
.argName("PATH")
.required()
.build());
.longOpt("dbpass")
.desc("Database password to use")
.hasArg(true)
.optionalArg(true)
.numberOfArgs(1)
.argName("PASS")
.required()
.build());



if (Arrays.asList(args).contains("help")) {
HelpFormatter formatter = new HelpFormatter();
Expand All @@ -110,10 +126,41 @@ protected static int execute(String... args) throws Exception {
final String password = line.getOptionValue("dbpass");

final String exportTypeStr = line.getOptionValue("exporttype");
final ExportType exportType = ExportType.valueOf(exportTypeStr.toUpperCase());

final ExportType exportType = Optional.ofNullable(exportTypeStr)
.map(String::toUpperCase)
.map(ExportType::valueOf).get();

final Path outputFile = Paths.get(line.getOptionValue("outputfile")).toAbsolutePath();
final DataExporter dataExporter = DataExporterFactory.create(exportType);

final DataExporter dataExporter;
if (exportType == ExportType.JDBC) {
if (!line.hasOption("dbconfig")) {
throw new MissingOptionException("dbconfig file path is required when no export type is defined.");
}

String dbconfig = line.getOptionValue("dbconfig");

Properties properties = new Properties();
try (InputStream inStream = Files.newInputStream(Paths.get(dbconfig))) {
properties.load(inStream);
}

String insertRow = Objects.requireNonNull(properties.getProperty("insertRow",null),
"No insertRow value defined in config file. ");

String createTable = Objects.requireNonNull(properties.getProperty("createTable",null),
"No createTable value defined in config file. ");

String jdbcUrl = Objects.requireNonNull(properties.getProperty("jdbcUrl",null),
"No jdbcUrl value defined in config file. ");

dataExporter = new JdbcDataExporter(jdbcUrl, insertRow, createTable);

} else {
dataExporter = DataExporterFactory.create(exportType);
}

dataExporter.export(data, outputFile, username, password);

System.out.printf("Exported data to %s", Objects.toString(outputFile));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@

package com.quorum.tessera.data.migration;

import java.sql.Driver;


public enum ExportType {
H2(org.h2.Driver.class),
SQLITE(org.sqlite.JDBC.class);

final Class<? extends Driver> driver;
H2,
SQLITE,
JDBC;

ExportType(Class<? extends Driver> driver) {
this.driver = driver;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.quorum.tessera.data.migration;

import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Map.Entry;

public class JdbcDataExporter implements DataExporter {

private final String jdbcUrl;

private final String insertRow;

private final String createTable;

public JdbcDataExporter(String jdbcUrl, String insertRow, String createTable) {
this.jdbcUrl = jdbcUrl;
this.insertRow = insertRow;
this.createTable = createTable;
}

@Override
public void export(Map<byte[], byte[]> data, Path output, String username, String password) throws SQLException {

try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {

try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(createTable);
}

try (PreparedStatement insertStatement = conn.prepareStatement(insertRow)) {
for (Entry<byte[], byte[]> values : data.entrySet()) {
insertStatement.setBytes(1, values.getKey());
insertStatement.setBytes(2, values.getValue());
insertStatement.execute();
}
}

}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quorum.tessera.data.migration;

import com.mockrunner.mock.jdbc.JDBCMockObjectFactory;
import org.apache.commons.cli.MissingOptionException;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -36,9 +37,9 @@ public void onSetup() throws Exception {
public void onTearDown() throws IOException {
if (Files.exists(outputPath)) {
Files.walk(outputPath)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}

Expand All @@ -60,7 +61,7 @@ public void noOptions() {

assertThat(throwable).isInstanceOf(MissingOptionException.class);
assertThat(((MissingOptionException) throwable).getMissingOptions())
.containsExactlyInAnyOrder("storetype", "inputpath", "exporttype", "outputfile", "dbpass", "dbuser");
.containsExactlyInAnyOrder("storetype", "inputpath", "exporttype", "outputfile", "dbpass", "dbuser");

}

Expand Down Expand Up @@ -150,8 +151,53 @@ public void cannotBeConstructed() throws Exception {

final Throwable throwable = catchThrowable(constructor::newInstance);
assertThat(throwable)
.isInstanceOf(InvocationTargetException.class)
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class);
.isInstanceOf(InvocationTargetException.class)
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class);
}

@Test(expected = org.apache.commons.cli.MissingOptionException.class)
public void exportTypeJdbcNoDbConfigProvided() throws Exception {

final Path inputFile = Paths.get(getClass().getResource("/dir/").toURI());

final String[] args = new String[]{
"-storetype", "dir",
"-inputpath", inputFile.toString(),
"-outputfile", outputPath.toString(),
"-exporttype", "jdbc",
"-dbpass", "-dbuser"
};

CmdLineExecutor.execute(args);

}

@Test
public void exportTypeJdbc() throws Exception {

JDBCMockObjectFactory mockObjectFactory = new JDBCMockObjectFactory();

try {
mockObjectFactory.registerMockDriver();

String dbConfigPath = getClass().getResource("/dbconfig.properties").getFile();

final Path inputFile = Paths.get(getClass().getResource("/dir/").toURI());

final String[] args = new String[]{
"-storetype", "dir",
"-inputpath", inputFile.toString(),
"-outputfile", outputPath.toString(),
"-exporttype", "jdbc",
"-dbconfig", dbConfigPath,
"-dbpass", "-dbuser"
};

CmdLineExecutor.execute(args);

assertThat(outputPath).isNotNull();
} finally {
mockObjectFactory.restoreDrivers();
}
}
}
Loading