mirror of
https://github.com/google/nomulus.git
synced 2025-05-21 19:59:34 +02:00
Drop postgresql schema instead of database in Sql tests (#530)
* Drop schema instead of database in Sql tests Speed up the database cleanup between tests by dropping the schema instead of the database. The new approach is much faster. Ad hoc measurement on my desktop shows that :core:sqlIntegrationTest improves from 73 seconds to 48 seconds, and :core:standardTest improves from 12m40 to 7m40.
This commit is contained in:
parent
d663bf4db5
commit
f1c46b8030
2 changed files with 8 additions and 7 deletions
|
@ -62,7 +62,6 @@ import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
abstract class JpaTransactionManagerRule extends ExternalResource {
|
abstract class JpaTransactionManagerRule extends ExternalResource {
|
||||||
private static final String DB_CLEANUP_SQL_PATH =
|
private static final String DB_CLEANUP_SQL_PATH =
|
||||||
"google/registry/persistence/transaction/cleanup_database.sql";
|
"google/registry/persistence/transaction/cleanup_database.sql";
|
||||||
private static final String MANAGEMENT_DB_NAME = "management";
|
|
||||||
private static final String POSTGRES_DB_NAME = "postgres";
|
private static final String POSTGRES_DB_NAME = "postgres";
|
||||||
// The type of JDBC connections started by the tests. This string value
|
// The type of JDBC connections started by the tests. This string value
|
||||||
// is documented in PSQL's official user guide.
|
// is documented in PSQL's official user guide.
|
||||||
|
@ -95,14 +94,14 @@ abstract class JpaTransactionManagerRule extends ExternalResource {
|
||||||
private static JdbcDatabaseContainer create() {
|
private static JdbcDatabaseContainer create() {
|
||||||
PostgreSQLContainer container =
|
PostgreSQLContainer container =
|
||||||
new PostgreSQLContainer(NomulusPostgreSql.getDockerTag())
|
new PostgreSQLContainer(NomulusPostgreSql.getDockerTag())
|
||||||
.withDatabaseName(MANAGEMENT_DB_NAME);
|
.withDatabaseName(POSTGRES_DB_NAME);
|
||||||
container.start();
|
container.start();
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void before() throws Exception {
|
public void before() throws Exception {
|
||||||
executeSql(MANAGEMENT_DB_NAME, readSqlInClassPath(DB_CLEANUP_SQL_PATH));
|
executeSql(POSTGRES_DB_NAME, readSqlInClassPath(DB_CLEANUP_SQL_PATH));
|
||||||
initScriptPath.ifPresent(path -> executeSql(POSTGRES_DB_NAME, readSqlInClassPath(path)));
|
initScriptPath.ifPresent(path -> executeSql(POSTGRES_DB_NAME, readSqlInClassPath(path)));
|
||||||
if (!extraEntityClasses.isEmpty()) {
|
if (!extraEntityClasses.isEmpty()) {
|
||||||
File tempSqlFile = File.createTempFile("tempSqlFile", ".sql");
|
File tempSqlFile = File.createTempFile("tempSqlFile", ".sql");
|
||||||
|
@ -156,8 +155,7 @@ abstract class JpaTransactionManagerRule extends ExternalResource {
|
||||||
* is less than 5 to reduce flakiness.
|
* is less than 5 to reduce flakiness.
|
||||||
*/
|
*/
|
||||||
private void assertReasonableNumDbConnections() {
|
private void assertReasonableNumDbConnections() {
|
||||||
// Use the 'management' db to connect so that this connection needs not to be accounted for.
|
try (Connection conn = createConnection(POSTGRES_DB_NAME);
|
||||||
try (Connection conn = createConnection(MANAGEMENT_DB_NAME);
|
|
||||||
Statement statement = conn.createStatement()) {
|
Statement statement = conn.createStatement()) {
|
||||||
// Note: Since we use the admin user (returned by container's getUserName() method)
|
// Note: Since we use the admin user (returned by container's getUserName() method)
|
||||||
// in tests, we need to filter connections by database name and/or backend type to filter out
|
// in tests, we need to filter connections by database name and/or backend type to filter out
|
||||||
|
|
|
@ -12,5 +12,8 @@
|
||||||
-- See the License for the specific language governing permissions and
|
-- See the License for the specific language governing permissions and
|
||||||
-- limitations under the License.
|
-- limitations under the License.
|
||||||
|
|
||||||
DROP DATABASE IF EXISTS postgres;
|
-- In Postgresql, recreating schema is faster than recreating the database.
|
||||||
CREATE DATABASE postgres;
|
DROP SCHEMA public CASCADE;
|
||||||
|
CREATE SCHEMA public;
|
||||||
|
GRANT USAGE ON SCHEMA public to PUBLIC;
|
||||||
|
GRANT CREATE ON SCHEMA public to PUBLIC;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue