Speed up test database resets and Postgres config (#3238)

- Mount the test Postgres data directory on an in-memory tmpfs and disable fsync, synchronous_commit, and full_page_writes for ephemeral test containers.
- Override the Nomulus SQL dialect to emit 'deferrable initially deferred' on foreign keys exported by HibernateSchemaExporter. This matches prod.
- Replace per-test TRUNCATES (etc) with DELETEs -- the vast majority of
  tests only change a couple tables and DELETEing a few rows is much
faster than having to reallocate all the disk space for each and every
table every time
- Cache the connection to Postgres that we use for resetting data,
  instead of having to re-create it each time
- Add a test for child entity foreign key generation

This drastically speeds up tests, cutting out like 30-40% of total test
time (not including compilation)

(we also update a couple queries to be deterministically ordered to
avoid test flakes)
This commit is contained in:
gbrodman
2026-09-25 17:46:41 +00:00
committed by GitHub
parent 901e6ab789
commit 4e0fccda7f
7 changed files with 127 additions and 46 deletions
@@ -951,49 +951,49 @@
alter table if exists "ConsoleUpdateHistory"
add constraint FKnhl1eolgix64u90xv3pj6xa3x
foreign key (acting_user)
references "User";
references "User" deferrable initially deferred;
alter table if exists "DelegationSignerData"
add constraint FKtr24j9v14ph2mfuw2gsmt12kq
foreign key (domain_repo_id)
references "Domain";
references "Domain" deferrable initially deferred;
alter table if exists "DomainDsDataHistory"
add constraint FKo4ilgyyfnvppbpuivus565i0j
foreign key (domain_repo_id, domain_history_revision_id)
references "DomainHistory";
references "DomainHistory" deferrable initially deferred;
alter table if exists "DomainHistoryHost"
add constraint FKa9woh3hu8gx5x0vly6bai327n
foreign key (domain_history_domain_repo_id, domain_history_history_revision_id)
references "DomainHistory";
references "DomainHistory" deferrable initially deferred;
alter table if exists "DomainHost"
add constraint FKeq1guccbre1yk3oosgp2io554
foreign key (domain_repo_id)
references "Domain";
references "Domain" deferrable initially deferred;
alter table if exists "DomainTransactionRecord"
add constraint FKcjqe54u72kha71vkibvxhjye7
foreign key (domain_repo_id, history_revision_id)
references "DomainHistory";
references "DomainHistory" deferrable initially deferred;
alter table if exists "GracePeriod"
add constraint FK2mys4hojm6ev2g9tmy5aq6m7g
foreign key (domain_repo_id)
references "Domain";
references "Domain" deferrable initially deferred;
alter table if exists "GracePeriodHistory"
add constraint FK7w3cx8d55q8bln80e716tr7b8
foreign key (domain_repo_id, domain_history_revision_id)
references "DomainHistory";
references "DomainHistory" deferrable initially deferred;
alter table if exists "RegistryLock"
add constraint FK2lhcwpxlnqijr96irylrh1707
foreign key (relock_revision_id)
references "RegistryLock";
references "RegistryLock" deferrable initially deferred;
alter table if exists "SignedMarkRevocationEntry"
add constraint FK5ivlhvs3121yx2li5tqh54u4
foreign key (revision_id)
references "SignedMarkRevocationList";
references "SignedMarkRevocationList" deferrable initially deferred;
@@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.truth.TextDiffSubject.assertThat;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.Resources;
import google.registry.persistence.NomulusPostgreSql;
@@ -81,7 +82,9 @@ class SchemaTest {
private final PostgreSQLContainer<?> sqlContainer =
new PostgreSQLContainer<>(NomulusPostgreSql.getDockerImageName())
.withClasspathResourceMapping(
MOUNTED_RESOURCE_PATH, CONTAINER_MOUNT_POINT, BindMode.READ_WRITE);
MOUNTED_RESOURCE_PATH, CONTAINER_MOUNT_POINT, BindMode.READ_WRITE)
// Use tmpFs so that we keep everything in memory. No point in writing to disk
.withTmpFs(ImmutableMap.of("/var/lib/postgresql/data", "rw"));
@Test
@DisabledIfSystemProperty(named = "deploy_to_existing_db", matches = ".*")