Does anyone have any prior art for using Pants to ...
# general
a
Does anyone have any prior art for using Pants to run Django tests that depend on a Postgres database?
b
Not 100% relevant, since we're using SQLAlchemy + alembic, not Django , but we: 1. start postgres outside pants manually (i.e. long-running persistent server instance) https://github.com/pantsbuild/pants/issues/16362 2. set up an isolated database for each test session (that is, each pytest process gets its own database) a.
session
-scoped fixture that creates and deletes the database b. doing this construction each time is slow, so we put the migrations into a PEX, hash it, and restore from a cached version
$hashed_name = "CACHED:$hash"
, if we can, via: i. to create the
$random_name
database for this test run, optimistically execute
CREATE DATABASE $random_name TEMPLATE $hashed_name
. If that succeeds, all done otherwise, continue... ii. create the empty database
CREATE DATABASE $random_name
iii. run the migrations in that database (now good for running tests ) iv. save the cache for next time:
CREATE DATABASE $hashed_name TEMPLATE $random_name
, ignoring "already exists" errors, which might come from this happening concurrently and another process winning. c. (NB. the cached DB is left lying around, only the
$random_name
one is cleaned up)
a
The cached DB approach is clever! I got a Django solution to everything else from @curved-television-6568, but I'll definitely look at the caching/restore approach once our project gets a bit older
🎉 1
c
Thanks for the cached DB idea.. that would be great to adopt for our project as well. (and if that were to become a pants feature, great! 😈 ) cc @glamorous-tiger-7918
👀 1
h
@ancient-vegetable-10556 do you still have access to the toolchain repo? pretty sure we did that
👀 1