mirror of
https://github.com/google/nomulus.git
synced 2025-06-02 18:48:41 +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
|
@ -16,6 +16,7 @@ package google.registry.tools;
|
|||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import google.registry.model.domain.DomainBase;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
|
@ -24,6 +25,7 @@ import org.hibernate.boot.MetadataSources;
|
|||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
/**
|
||||
* 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.")
|
||||
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;
|
||||
|
||||
private PostgreSQLContainer postgresContainer = null;
|
||||
|
||||
@Parameter(
|
||||
names = {"-o", "--out-file"},
|
||||
description = "")
|
||||
description = "Name of the output file.",
|
||||
required = true)
|
||||
String outFile;
|
||||
|
||||
@Parameter(
|
||||
names = {"-s", "--start-postgresql"},
|
||||
description = "If specified, start postgresql in a docker container.")
|
||||
boolean startPostgresql = false;
|
||||
|
||||
@Parameter(
|
||||
names = {"-a", "--db-host"},
|
||||
description = "Database host name.")
|
||||
|
@ -50,30 +66,73 @@ public class GenerateSqlSchemaCommand implements Command {
|
|||
@Parameter(
|
||||
names = {"-p", "--db-port"},
|
||||
description = "Database port number. This defaults to the postgresql default port.")
|
||||
int databasePort = POSTGRESQL_PORT;
|
||||
Integer databasePort;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO(mmuller): Optionally (and perhaps by default) start a postgresql instance container
|
||||
// rather than relying on the user to have one to connect to.
|
||||
Map<String, String> settings = new HashMap<>();
|
||||
settings.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
|
||||
settings.put(
|
||||
"hibernate.connection.url",
|
||||
"jdbc:postgresql://" + databaseHost + ":" + databasePort + "/postgres?useSSL=false");
|
||||
settings.put("hibernate.connection.username", "postgres");
|
||||
settings.put("hibernate.connection.password", "domain-registry");
|
||||
settings.put("hibernate.hbm2ddl.auto", "none");
|
||||
settings.put("show_sql", "true");
|
||||
// Start postgres if requested.
|
||||
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;
|
||||
}
|
||||
|
||||
MetadataSources metadata =
|
||||
new MetadataSources(new StandardServiceRegistryBuilder().applySettings(settings).build());
|
||||
metadata.addAnnotatedClass(DomainBase.class);
|
||||
SchemaExport schemaExport = new SchemaExport();
|
||||
schemaExport.setHaltOnError(true);
|
||||
schemaExport.setFormat(true);
|
||||
schemaExport.setDelimiter(";");
|
||||
schemaExport.setOutputFile(outFile);
|
||||
schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata.buildMetadata());
|
||||
// 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<>();
|
||||
settings.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
|
||||
settings.put(
|
||||
"hibernate.connection.url",
|
||||
"jdbc:postgresql://" + databaseHost + ":" + databasePort + "/postgres?useSSL=false");
|
||||
settings.put("hibernate.connection.username", "postgres");
|
||||
settings.put("hibernate.connection.password", "domain-registry");
|
||||
settings.put("hibernate.hbm2ddl.auto", "none");
|
||||
settings.put("show_sql", "true");
|
||||
|
||||
MetadataSources metadata =
|
||||
new MetadataSources(new StandardServiceRegistryBuilder().applySettings(settings).build());
|
||||
metadata.addAnnotatedClass(DomainBase.class);
|
||||
SchemaExport schemaExport = new SchemaExport();
|
||||
schemaExport.setHaltOnError(true);
|
||||
schemaExport.setFormat(true);
|
||||
schemaExport.setDelimiter(";");
|
||||
schemaExport.setOutputFile(outFile);
|
||||
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 org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
@ -35,7 +36,7 @@ public class GenerateSqlSchemaCommandTest extends CommandTestCase<GenerateSqlSch
|
|||
|
||||
@Rule public TemporaryFolder tmp = new TemporaryFolder();
|
||||
|
||||
@Rule public PostgreSQLContainer postgres =
|
||||
@ClassRule public static PostgreSQLContainer postgres =
|
||||
new PostgreSQLContainer()
|
||||
.withDatabaseName("postgres")
|
||||
.withUsername("postgres")
|
||||
|
@ -61,4 +62,22 @@ public class GenerateSqlSchemaCommandTest extends CommandTestCase<GenerateSqlSch
|
|||
// TODO: try running the schema against the test database.
|
||||
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