sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

sqlx prepare should not clean sqlx

Open cycraig opened this issue 1 year ago • 6 comments

Bug Description

The minimal_project_recompile step of cargo sqlx prepare --workspace always runs cargo clean -p sqlx, because it depends on sqlx-macros (assuming the macros feature is enabled).

cargo sqlx prepare --database-url "postgres://postgres:password@localhost/example" --workspace
    Cleaning [=============================] 1/1: sqlx, 0 files/folders cleaned 

Surprisingly, cargo clean -p sqlx can take several minutes in a large project, and I don't think it is necessary since sqlx itself does not contain any query macros.

Edit: it seems like it was always the plan to exclude sqlx from being cleaned: https://github.com/launchbadge/sqlx/pull/1802#issuecomment-1099801198

Suggestions

  • Hardcode an exclusion filter for sqlx in out_of_workspace_dependents.
  • Accept a list of exclusions from an environment variable, like SQLX_PREPARE_NO_CLEAN="sqlx crate1 crate2"?
  • Something else?

Minimal Reproduction

Reproducible in any workspace where the macros feature is enabled for sqlx::query!, and cargo sqlx prepare --workspace is run.

E.g. https://github.com/cycraig/sqlx_prepare_bug.

Info

  • SQLx version: 0.7.1
  • SQLx features enabled: ["chrono", "ipnetwork", "json", "macros", "migrate", "postgres", "runtime-tokio-rustls", "uuid"] (only macros seems relevant here)
  • Database server and version: Postgres 14
  • Operating system: MacOS
  • rustc --version: 1.72.0

cycraig avatar Sep 27 '23 19:09 cycraig

Edit: it seems like it was always the plan to exclude sqlx from being cleaned: https://github.com/launchbadge/sqlx/pull/1802#issuecomment-1099801198

It looks like this issue is now of interest to me. I can take a look, but yes excluding sqlx from the clean should be fine

CosmicHorrorDev avatar Dec 20 '23 16:12 CosmicHorrorDev

Just noting that for our relatively large workspace, cargo sqlx prepare has gone from being a fairly quick operation to one that takes nearly 4 minutes to complete:

❯ time make db-sqlx-offline-data
Checking cargo sqlx-cli version
cargo sqlx-cli (0.7.3) is the same version as sqlx-cli (0.7.3)
Generating sqlx offline data
     Removed 13 files, 334.6KiB total
    Checking sqlx v0.7.3
   <snip>
    Finished dev [unoptimized + debuginfo] target(s) in 14.81s
query data written to .sqlx in the workspace root; please check this into version control

________________________________________________________
Executed in  230.85 secs    fish           external
   usr time  248.45 secs  397.00 micros  248.45 secs
   sys time   43.08 secs  196.00 micros   43.08 secs

The make command just runs:

	cargo sqlx prepare \
		--workspace --database-url "$$DATABASE_URL" \
		-- --all-features --all-targets

mplanchard avatar Mar 19 '24 23:03 mplanchard

Do you have a fairly large target dir (as in 10's of GBs)?

CosmicHorrorDev avatar Mar 19 '24 23:03 CosmicHorrorDev

Yep, it's quite large:

❯ du -sh target
83G     target

mplanchard avatar Mar 19 '24 23:03 mplanchard

Cleaning that will likely drastically reduce the run time as a temporary workaround

I've been meaning to dig into cargo clean -p more to see what all is slow there, but I've had very limited free time lately

CosmicHorrorDev avatar Mar 19 '24 23:03 CosmicHorrorDev

Cleaning that will likely drastically reduce the run time as a temporary workaround

Oh yeah, nice! Did an rm -rf target, recompiled, and now it's much faster indeed:

❯ du -sh target
2.2G    target

❯ time make db-sqlx-offline-data
Checking cargo sqlx-cli version
cargo sqlx-cli (0.7.3) is the same version as sqlx-cli (0.7.3)
Generating sqlx offline data
     Removed 0 files
    Checking sqlx v0.7.3
    <snip>
    Finished dev [unoptimized + debuginfo] target(s) in 6.85s
query data written to .sqlx in the workspace root; please check this into version control
________________________________________________________
Executed in   12.95 secs    fish           external
   usr time   20.38 secs    0.00 millis   20.38 secs
   sys time    8.51 secs    1.01 millis    8.51 secs

Thanks for the tip!

mplanchard avatar Mar 19 '24 23:03 mplanchard