mirror of
https://github.com/google/nomulus.git
synced 2025-06-06 04:27:27 +02:00
Start postgresql container in generate_sql_schema (#249)
* Start postgresql container in generate_sql_schema Add a --start-postgresql option to the nomulus generate_sql_schema command so that users don't have to start their own docker container to run it. * Made default behavior be to give guidance
This commit is contained in:
parent
dc9d9158d8
commit
d3ccad3aa7
3 changed files with 102 additions and 24 deletions
|
@ -226,7 +226,7 @@ dependencies {
|
||||||
testCompile deps['org.seleniumhq.selenium:selenium-chrome-driver']
|
testCompile deps['org.seleniumhq.selenium:selenium-chrome-driver']
|
||||||
testCompile deps['org.seleniumhq.selenium:selenium-java']
|
testCompile deps['org.seleniumhq.selenium:selenium-java']
|
||||||
testCompile deps['org.seleniumhq.selenium:selenium-remote-driver']
|
testCompile deps['org.seleniumhq.selenium:selenium-remote-driver']
|
||||||
testCompile deps['org.testcontainers:postgresql']
|
compile deps['org.testcontainers:postgresql']
|
||||||
testCompile deps['org.testcontainers:selenium']
|
testCompile deps['org.testcontainers:selenium']
|
||||||
compile deps['xerces:xmlParserAPIs']
|
compile deps['xerces:xmlParserAPIs']
|
||||||
compile deps['xpp3:xpp3']
|
compile deps['xpp3:xpp3']
|
||||||
|
|
|
@ -16,6 +16,7 @@ package google.registry.tools;
|
||||||
|
|
||||||
import com.beust.jcommander.Parameter;
|
import com.beust.jcommander.Parameter;
|
||||||
import com.beust.jcommander.Parameters;
|
import com.beust.jcommander.Parameters;
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import google.registry.model.domain.DomainBase;
|
import google.registry.model.domain.DomainBase;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -24,6 +25,7 @@ import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||||
import org.hibernate.tool.schema.TargetType;
|
import org.hibernate.tool.schema.TargetType;
|
||||||
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a schema for JPA annotated classes using hibernate.
|
* Generates a schema for JPA annotated classes using hibernate.
|
||||||
|
@ -35,13 +37,27 @@ import org.hibernate.tool.schema.TargetType;
|
||||||
@Parameters(separators = " =", commandDescription = "Generate postgresql schema.")
|
@Parameters(separators = " =", commandDescription = "Generate postgresql schema.")
|
||||||
public class GenerateSqlSchemaCommand implements Command {
|
public class GenerateSqlSchemaCommand implements Command {
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
public static final String DB_OPTIONS_CLASH =
|
||||||
|
"Database host and port may not be spcified along with the option to start a "
|
||||||
|
+ "postgresql container.";
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
public static final int POSTGRESQL_PORT = 5432;
|
public static final int POSTGRESQL_PORT = 5432;
|
||||||
|
|
||||||
|
private PostgreSQLContainer postgresContainer = null;
|
||||||
|
|
||||||
@Parameter(
|
@Parameter(
|
||||||
names = {"-o", "--out-file"},
|
names = {"-o", "--out-file"},
|
||||||
description = "")
|
description = "Name of the output file.",
|
||||||
|
required = true)
|
||||||
String outFile;
|
String outFile;
|
||||||
|
|
||||||
|
@Parameter(
|
||||||
|
names = {"-s", "--start-postgresql"},
|
||||||
|
description = "If specified, start postgresql in a docker container.")
|
||||||
|
boolean startPostgresql = false;
|
||||||
|
|
||||||
@Parameter(
|
@Parameter(
|
||||||
names = {"-a", "--db-host"},
|
names = {"-a", "--db-host"},
|
||||||
description = "Database host name.")
|
description = "Database host name.")
|
||||||
|
@ -50,12 +66,50 @@ public class GenerateSqlSchemaCommand implements Command {
|
||||||
@Parameter(
|
@Parameter(
|
||||||
names = {"-p", "--db-port"},
|
names = {"-p", "--db-port"},
|
||||||
description = "Database port number. This defaults to the postgresql default port.")
|
description = "Database port number. This defaults to the postgresql default port.")
|
||||||
int databasePort = POSTGRESQL_PORT;
|
Integer databasePort;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// TODO(mmuller): Optionally (and perhaps by default) start a postgresql instance container
|
// Start postgres if requested.
|
||||||
// rather than relying on the user to have one to connect to.
|
if (startPostgresql) {
|
||||||
|
// Complain if the user has also specified either --db-host or --db-port.
|
||||||
|
if (databaseHost != null || databasePort != null) {
|
||||||
|
System.err.println(DB_OPTIONS_CLASH);
|
||||||
|
// TODO: it would be nice to exit(1) here, but this breaks testability.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the container and store the address information.
|
||||||
|
postgresContainer = new PostgreSQLContainer()
|
||||||
|
.withDatabaseName("postgres")
|
||||||
|
.withUsername("postgres")
|
||||||
|
.withPassword("domain-registry");
|
||||||
|
postgresContainer.start();
|
||||||
|
databaseHost = postgresContainer.getContainerIpAddress();
|
||||||
|
databasePort = postgresContainer.getMappedPort(POSTGRESQL_PORT);
|
||||||
|
} else if (databaseHost == null) {
|
||||||
|
System.err.println(
|
||||||
|
"You must specify either --start-postgresql to start a PostgreSQL database in a\n"
|
||||||
|
+ "docker instance, or specify --db-host (and, optionally, --db-port) to identify\n"
|
||||||
|
+ "the location of a running instance. To start a long-lived instance (suitable\n"
|
||||||
|
+ "for running this command multiple times) run this:\n\n"
|
||||||
|
+ " docker run --rm --name some-postgres -e POSTGRES_PASSWORD=domain-registry \\\n"
|
||||||
|
+ " -d postgres:9.6.12\n\n"
|
||||||
|
+ "Copy the container id output from the command, then run:\n\n"
|
||||||
|
+ " docker inspect <container-id> | grep IPAddress\n\n"
|
||||||
|
+ "To obtain the value for --db-host.\n"
|
||||||
|
);
|
||||||
|
// TODO: need exit(1), see above.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// use the default port if non has been defined.
|
||||||
|
if (databasePort == null) {
|
||||||
|
databasePort = POSTGRESQL_PORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Configure hibernate settings.
|
||||||
Map<String, String> settings = new HashMap<>();
|
Map<String, String> settings = new HashMap<>();
|
||||||
settings.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
|
settings.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
|
||||||
settings.put(
|
settings.put(
|
||||||
|
@ -75,5 +129,10 @@ public class GenerateSqlSchemaCommand implements Command {
|
||||||
schemaExport.setDelimiter(";");
|
schemaExport.setDelimiter(";");
|
||||||
schemaExport.setOutputFile(outFile);
|
schemaExport.setOutputFile(outFile);
|
||||||
schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata.buildMetadata());
|
schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata.buildMetadata());
|
||||||
|
} finally {
|
||||||
|
if (postgresContainer != null) {
|
||||||
|
postgresContainer.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.ClassRule;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.TemporaryFolder;
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
@ -35,7 +36,7 @@ public class GenerateSqlSchemaCommandTest extends CommandTestCase<GenerateSqlSch
|
||||||
|
|
||||||
@Rule public TemporaryFolder tmp = new TemporaryFolder();
|
@Rule public TemporaryFolder tmp = new TemporaryFolder();
|
||||||
|
|
||||||
@Rule public PostgreSQLContainer postgres =
|
@ClassRule public static PostgreSQLContainer postgres =
|
||||||
new PostgreSQLContainer()
|
new PostgreSQLContainer()
|
||||||
.withDatabaseName("postgres")
|
.withDatabaseName("postgres")
|
||||||
.withUsername("postgres")
|
.withUsername("postgres")
|
||||||
|
@ -61,4 +62,22 @@ public class GenerateSqlSchemaCommandTest extends CommandTestCase<GenerateSqlSch
|
||||||
// TODO: try running the schema against the test database.
|
// TODO: try running the schema against the test database.
|
||||||
assertThat(new File(tmp.getRoot(), "schema.sql").exists()).isTrue();
|
assertThat(new File(tmp.getRoot(), "schema.sql").exists()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIncompatibleFlags() throws Exception {
|
||||||
|
runCommand(
|
||||||
|
"--out-file=" + tmp.getRoot() + File.separatorChar + "schema.sql",
|
||||||
|
"--db-host=" + containerHostName,
|
||||||
|
"--db-port=" + containerPort,
|
||||||
|
"--start-postgresql");
|
||||||
|
assertInStderr(GenerateSqlSchemaCommand.DB_OPTIONS_CLASH);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDockerPostgresql() throws Exception {
|
||||||
|
runCommand(
|
||||||
|
"--start-postgresql",
|
||||||
|
"--out-file=" + tmp.getRoot() + File.separatorChar + "schema.sql");
|
||||||
|
assertThat(new File(tmp.getRoot(), "schema.sql").exists()).isTrue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue