Skip to content

Deduplicate DatabaseJanitor codebase after async introduction#1366

Open
tboy1337 wants to merge 3 commits into
dbfixtures:mainfrom
tboy1337:de-dupe
Open

Deduplicate DatabaseJanitor codebase after async introduction#1366
tboy1337 wants to merge 3 commits into
dbfixtures:mainfrom
tboy1337:de-dupe

Conversation

@tboy1337

@tboy1337 tboy1337 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Follow-up refactor after #1295 introduced AsyncDatabaseJanitor alongside the existing sync DatabaseJanitor.

This PR consolidates shared janitor logic into a new BaseDatabaseJanitor base class:

  • Shared __init__ with version parsing and configuration assignment
  • Shared is_template() implementation
  • Shared _build_create_database_sql() for CREATE DATABASE DDL construction
  • Class-level type annotations preserved on the base class

DatabaseJanitor and AsyncDatabaseJanitor now inherit from BaseDatabaseJanitor, removing the _JanitorConfig dataclass, _JanitorConfigTarget protocol, and related helper functions (_init_janitor_config, _apply_janitor_config, module-level _build_create_database_sql).

Sync/async-specific methods (init, drop, cursor, load, and connection helpers) remain in their respective subclasses, since they require separate sync and async implementations.

This builds on the intent of #1364 and closes the remaining small deduplication gap by hoisting is_template() into the base class and keeping explicit type annotations.

Changes

  • pytest_postgresql/janitor.py — introduce BaseDatabaseJanitor; slim down sync/async subclasses
  • newsfragments/+dedupe-janitor.misc.rst — changelog entry

No public API changes: DatabaseJanitor and AsyncDatabaseJanitor constructors and behavior are unchanged.

Summary by CodeRabbit

  • Refactor
    • Improved consistency between synchronous and asynchronous database janitor operations.
    • Centralised shared database configuration and database creation logic for more predictable behaviour.
    • Streamlined template handling and database SQL generation across both modes.
  • Documentation
    • Added a release note documenting deduplication following the introduction of asynchronous support.

- Introduced a common base class, BaseDatabaseJanitor, to reduce code duplication.
- Removed the _JanitorConfig dataclass and related functions, simplifying the initialization process.
- Updated DatabaseJanitor and AsyncDatabaseJanitor to directly assign configuration attributes.
- Enhanced SQL query building methods for better readability and maintainability.
- Improved overall structure and clarity of the janitor classes.
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b9e1441f-93f9-402c-8a77-2513064d3021

📥 Commits

Reviewing files that changed from the base of the PR and between dfb3bf2 and ed34e6d.

📒 Files selected for processing (1)
  • pytest_postgresql/janitor.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • pytest_postgresql/janitor.py

📝 Walkthrough

Walkthrough

The refactor adds BaseDatabaseJanitor to centralise janitor configuration, version parsing, template detection, and CREATE DATABASE SQL generation. Synchronous and asynchronous janitors inherit this shared behaviour and use the instance SQL builder.

Changes

DatabaseJanitor deduplication

Layer / File(s) Summary
Shared janitor configuration and SQL builder
pytest_postgresql/janitor.py
Adds BaseDatabaseJanitor, storing configuration on the instance and providing shared template detection and CREATE DATABASE SQL construction.
Synchronous and asynchronous integration
pytest_postgresql/janitor.py, newsfragments/*
Updates both janitor implementations to use the shared SQL builder and template predicate, and records the deduplication in a newsfragment.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main refactor: deduplicating DatabaseJanitor and AsyncDatabaseJanitor after the async introduction.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Newsfragment Check ✅ Passed PASS: newsfragments/+dedupe-janitor.misc.rst is present, and pyproject.toml lists 'misc' as a valid towncrier fragment type.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…classes

- Updated `BaseDatabaseJanitor`, `DatabaseJanitor`, and `AsyncDatabaseJanitor` to use the `is_template()` method for checking template status instead of the `as_template` attribute.
- Improved code clarity and maintainability by standardizing the method used for template checks across the janitor classes.
@tboy1337

Copy link
Copy Markdown
Contributor Author

@fizyk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant