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:
Weimin Yu 2020-03-25 21:03:58 -04:00 committed by GitHub
parent d663bf4db5
commit f1c46b8030
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View file

@ -62,7 +62,6 @@ import org.testcontainers.containers.PostgreSQLContainer;
abstract class JpaTransactionManagerRule extends ExternalResource {
private static final String DB_CLEANUP_SQL_PATH =
"google/registry/persistence/transaction/cleanup_database.sql";
private static final String MANAGEMENT_DB_NAME = "management";
private static final String POSTGRES_DB_NAME = "postgres";
// The type of JDBC connections started by the tests. This string value
// is documented in PSQL's official user guide.
@ -95,14 +94,14 @@ abstract class JpaTransactionManagerRule extends ExternalResource {
private static JdbcDatabaseContainer create() {
PostgreSQLContainer container =
new PostgreSQLContainer(NomulusPostgreSql.getDockerTag())
.withDatabaseName(MANAGEMENT_DB_NAME);
.withDatabaseName(POSTGRES_DB_NAME);
container.start();
return container;
}
@Override
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)));
if (!extraEntityClasses.isEmpty()) {
File tempSqlFile = File.createTempFile("tempSqlFile", ".sql");
@ -156,8 +155,7 @@ abstract class JpaTransactionManagerRule extends ExternalResource {
* is less than 5 to reduce flakiness.
*/
private void assertReasonableNumDbConnections() {
// Use the 'management' db to connect so that this connection needs not to be accounted for.
try (Connection conn = createConnection(MANAGEMENT_DB_NAME);
try (Connection conn = createConnection(POSTGRES_DB_NAME);
Statement statement = conn.createStatement()) {
// 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

View file

@ -12,5 +12,8 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
DROP DATABASE IF EXISTS postgres;
CREATE DATABASE postgres;
-- In Postgresql, recreating schema is faster than recreating the database.
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT USAGE ON SCHEMA public to PUBLIC;
GRANT CREATE ON SCHEMA public to PUBLIC;