Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Allow command-line arguments for all processes #151

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ postgres.start(cachedRuntimeConfig("C:\\Users\\vasya\\pgembedded-installation"))

* 11.1: on Mac OS X and Windows 64 bit
* 10.6, 9.6.11, 9.5.15: on Linux, Windows, Mac OS X
* any custom version
* any custom version
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,13 @@ protected final void onAfterProcessStart(ProcessControl process,
} while (trial++ < MAX_CREATEDB_TRIALS);
}

/**
* Import into database from file
*
* @param file The file to import into database
*/
public void importFromFile(File file) {
importFromFileWithArgs(file);
}

/**
* Import into database from file with additional args
*
* @param file
* @param cliArgs additional arguments for psql (be sure to separate args from their values)
*/
public void importFromFileWithArgs(File file, String... cliArgs) {
public void importFromFile(File file, String... cliArgs) {
if (file.exists()) {
String[] args = {
"-U", getConfig().credentials().username(),
Expand Down Expand Up @@ -334,36 +325,51 @@ public void restoreFromFile(File file, String... cliArgs) {
}
}

public void exportToFile(File file) {
runCmd(getConfig(), runtimeConfig, PgDump, "", new HashSet<>(singletonList("export from " + getConfig().storage().dbName() + " failed")),
"-U", getConfig().credentials().username(),
"-d", getConfig().storage().dbName(),
"-h", getConfig().net().host(),
"-p", String.valueOf(getConfig().net().port()),
"-f", file.getAbsolutePath()
);
/**
* Export (dump) database to file with additional args
*
* @param file
* @param cliArgs additional arguments for psql (be sure to separate args from their values)
*/
public void exportToFile(File file, String... cliArgs) {
String[] args = {
"-U", getConfig().credentials().username(),
"-d", getConfig().storage().dbName(),
"-h", getConfig().net().host(),
"-p", String.valueOf(getConfig().net().port()),
"-f", file.getAbsolutePath()};
if (cliArgs != null && cliArgs.length != 0) {
args = ArrayUtils.addAll(args, cliArgs);
}
runCmd(getConfig(), runtimeConfig, PgDump, "", new HashSet<>(singletonList("export from " + getConfig().storage().dbName() + " failed")), args);
}

/**
* Export (dump) database schema to file
*
* @param file
*/
public void exportSchemeToFile(File file) {
runCmd(getConfig(), runtimeConfig, PgDump, "", new HashSet<>(singletonList("export from " + getConfig().storage().dbName() + " failed")),
"-U", getConfig().credentials().username(),
"-d", getConfig().storage().dbName(),
"-h", getConfig().net().host(),
"-p", String.valueOf(getConfig().net().port()),
"-f", file.getAbsolutePath(),
"-s"
);
exportToFile(file, "-s");
}

/**
* Export (dump) database schema to file
*
* @param file
*/
/* Alias for English speakers */
public void exportSchemaToFile(File file) {
exportSchemeToFile(file);
}

/**
* Export (dump) database data to file
*
* @param file
*/
public void exportDataToFile(File file) {
runCmd(getConfig(), runtimeConfig, PgDump, "", new HashSet<>(singletonList("export from " + getConfig().storage().dbName() + " failed")),
"-U", getConfig().credentials().username(),
"-d", getConfig().storage().dbName(),
"-h", getConfig().net().host(),
"-p", String.valueOf(getConfig().net().port()),
"-f", file.getAbsolutePath(),
"-a"
);
exportToFile(file, "-a");
}

public boolean isProcessReady() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static IRuntimeConfig runtimeConfig(Command cmd) {

public static <E extends AbstractPGExecutable<PostgresConfig, P>, P extends AbstractPGProcess<E, P>>
PostgresStarter<E, P> getCommand(Command command, IRuntimeConfig config) {
return new PostgresStarter<>(command.executableClass(), config);
return new PostgresStarter<E, P>(command.executableClass(), config);
}

public static <E extends AbstractPGExecutable<PostgresConfig, P>, P extends AbstractPGProcess<E, P>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ru.yandex.qatools.embed.postgresql;

import org.junit.Test;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class TestPsqlDumpEndToEnd extends AbstractPsqlTest {

@Test
public void testPsqlDumpEndToEnd() throws Exception {

// Load dump
process.importFromFile(new File("src/test/resources/test.backup"));
assertThat(conn, not(nullValue()));

assertSchemaAndData();

// Create binary dump
File fullExportDump = File.createTempFile("full_", ".dmp");
process.exportToFile(fullExportDump, "-Fc");
assertTrue(fullExportDump.exists());
assertTrue(fullExportDump.length() > 0);

// Create new connection
tearDown();
setUp();

// Load binary dump into a fresh database
assertTrue(fullExportDump.exists());
process.restoreFromFile(fullExportDump);
assertThat(conn, not(nullValue()));

assertSchemaAndData();

}

private void assertSchemaAndData() throws SQLException {

String expected;
try (Statement statement = conn.createStatement();
ResultSet res = statement.executeQuery("SELECT * FROM table1;")) {
assertThat(res, not(nullValue()));
String tableString = readTable(res);

assertThat("Missing content in relation 'table1' in dump file!", tableString, not(nullValue()));

expected = "test\t1\ta\n" + "test\t2\tb\n" + "test\t3\tc\n" + "test\t4\td\n";
assertEquals(expected, tableString);
}
}

private String readTable(ResultSet res) throws SQLException {
StringBuilder sb = null;
while (res.next()) {
if (null == sb)
sb = new StringBuilder();
sb.append(res.getString("col1"));
sb.append("\t");
sb.append(res.getInt("col2"));
sb.append("\t");
sb.append(res.getString("col3"));
sb.append("\n");
}
return null != sb ? sb.toString() : null;
}
}