-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add flag to clean up postgres databases. #204
base: main
Are you sure you want to change the base?
Conversation
f4aa625
to
dc966b3
Compare
For issues regarding pytest-asyncio (which I agree is not in a good state now), I find using nest_asyncio resolved some of the issues I had with different scoped fixtures. |
A potentially good callout, although it'd mean it would mandate use of it for downstream users, not just our tests. The current test failures seem to not be related at least...which makes sense, since i'm bypassing the new fixture for async code. They're also not a failure I'm experiencing locally... |
fffe2ff
to
459b8a8
Compare
909dc5c
to
fc01609
Compare
fc01609
to
50a9fdf
Compare
Pull Request Test Coverage Report for Build 9764591739Details
💛 - Coveralls |
Sigh, the moment I got passing CI, I've now realized that there's a pytest deficiency that seems not to allow me to dynamically create new fixtures with variable dependent fixtures. at least not ergonomically. What i assume must be happening is that this is now forcing unnecessary database creations due to that. I suppose I'll have to revise this further... |
I appreciate the effort! For now we are using something like the following to scan for pmr resources and drop them after the testing suite is complete: async def clean_databases(engine: AsyncEngine):
"""
Needed because `pytest-mock-resources` doesn't clean up after itself if you connect to an existing database.
See https://github.com/schireson/pytest-mock-resources/issues/202
"""
async with engine.connect() as conn:
query = "SELECT datname FROM pg_database WHERE datname LIKE 'pytest_mock_resource%';"
databases = await conn.scalars(text(query))
for database in databases:
try:
query = f"DROP DATABASE {database}"
await conn.execute(text(query))
except Exception as e:
logging.error(f"Could not drop database {database}: {e}")
raise e
query = "SELECT datname FROM pg_database WHERE datname LIKE 'pmr_template_pg_%';"
templates = await conn.scalars(text(query))
for template in templates:
try:
query = f"DROP DATABASE {template}"
await conn.execute(text(query))
except Exception as e:
logging.error(f"Could not drop template {template}: {e}")
@pytest_asyncio.fixture(scope="session")
async def alembic_engine(postgres_async_alembic: AsyncEngine, postgres_async_teardown: AsyncEngine):
yield postgres_async_alembic
await postgres_async_alembic.dispose()
await clean_databases(postgres_async_teardown) |
Well amusingly we recently ran into issues in CI related to this at $job, so it's now in our critical path 😆. I think the solution for vanilla test databases is a lot simpler, and will be my shorter term solution here. It's the template databases which are the complicated problem. |
Fixes #202.
cleanup_databases=False
argument. Default False to retain original behaviorcreate_postgres_fixture
callIt probably makes sense to follow this up with a
pmr --cleanup
or the like kind of CLI extension, that more directly just deletes allpmr_*
named databases. If there's a hard test failure, you enter a debugger and exit uncleanly, you ctrl+c enough, or various other means: it's still fairly straightforward to end up with left over databases.1 unresolved issue:
pytest-asyncio has all sorts of weird stuff going on in both prior versions that people claim works, and the new versions are just broken apparently. In either case, I wasn't able to find a way that seemed to work reliably in async code (at least, using pytest-asyncio). Only affecting template database cleanup.
So unless/until there's a different plugin, or they resolve their stuff and we start requiring a much more recent minimum version bound for it after they do, template database cleanup will mean that async code wont share templates (and thus will be slower). Could be worse, but isn't ideal.