Skip to content

Commit

Permalink
fix(zk): Do not cause recompilation when running 'zk test rust' (#2506)
Browse files Browse the repository at this point in the history
## What ❔

Right now, we're running `cargo sqlx prepare --check` for local
databases always.
It's justified for `zk db setup`, because we compile with `SQLX_OFFLINE
= true` to prevent stale `.sqlx-data`, but it makes no sense for unit
tests.

## Why ❔

Recompilations are long.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
popzxc authored Jul 26, 2024
1 parent 4222d13 commit 245d719
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions infrastructure/zk/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function resetTestDal(dalPath: DalPath, dbUrl: string) {
await utils.spawn('docker compose -f docker-compose-unit-tests.yml up -d');
await waitForDal(dbUrl, 100);
console.log('setting up a database template');
await setupForDal(dalPath, dbUrl);
await setupForDal(dalPath, dbUrl, false);
console.log('disallowing connections to the template');
await utils.spawn(
`psql "${dbUrl}" -c "update pg_database set datallowconn = false where datname = current_database()"`
Expand Down Expand Up @@ -119,7 +119,7 @@ export async function generateMigration(dbType: DbType, name: string) {
process.chdir(process.env.ZKSYNC_HOME as string);
}

export async function setupForDal(dalPath: DalPath, dbUrl: string) {
export async function setupForDal(dalPath: DalPath, dbUrl: string, shouldCheck: boolean = false) {
process.chdir(dalPath);
const localDbUrl = 'postgres://postgres:notsecurepassword@localhost';
if (dbUrl.startsWith(localDbUrl)) {
Expand All @@ -132,7 +132,8 @@ export async function setupForDal(dalPath: DalPath, dbUrl: string) {
await utils.spawn(`cargo sqlx migrate run --database-url ${dbUrl}`);
const isLocalSetup = process.env.ZKSYNC_LOCAL_SETUP;

if (dbUrl.startsWith(localDbUrl) && !isLocalSetup) {
shouldCheck = shouldCheck && dbUrl.startsWith(localDbUrl) && !isLocalSetup;
if (shouldCheck) {
// Dont't do this preparation for local (docker) setup - as it requires full cargo compilation.
await utils.spawn(
`cargo sqlx prepare --check --database-url ${dbUrl} -- --tests || cargo sqlx prepare --database-url ${dbUrl} -- --tests`
Expand All @@ -145,7 +146,7 @@ export async function setupForDal(dalPath: DalPath, dbUrl: string) {
export async function setup(opts: DbOpts) {
let dals = getDals(opts);
for (const [dalPath, dbUrl] of dals.entries()) {
await setupForDal(dalPath, dbUrl);
await setupForDal(dalPath, dbUrl, true);
}
}

Expand Down

0 comments on commit 245d719

Please sign in to comment.