-
Notifications
You must be signed in to change notification settings - Fork 5
feat(hashing): add PostgresHashCacher with psycopg3 backend (ITL-520) #223
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
Merged
eywalker
merged 13 commits into
main
from
eywalker/itl-520-filehasher-add-postgresql-backend-for-the-hash-cacher
Jul 11, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d4c0e16
docs(specs): add ITL-520 PostgresHashCacher design spec
kurodo3[bot] 0050aa0
docs(plans): add ITL-520 PostgresHashCacher implementation plan
kurodo3[bot] fd8cb13
chore(deps): add testcontainers[postgres] to dev deps (ITL-520)
kurodo3[bot] 7fd5112
feat(hashing): add PostgresHashCacher with thread-local psycopg3 conn…
kurodo3[bot] b7fecb8
feat(hashing): export PostgresHashCacher from orcapod.hashing (ITL-520)
kurodo3[bot] cae234a
feat(contexts): add conninfo param to enable_file_hash_caching() for …
kurodo3[bot] 25bc874
docs(hashing): add Postgres backend section to file-hash-caching docs…
kurodo3[bot] c5a4d74
test(hashing): add pragma: no cover to unreachable ImportError fallba…
kurodo3[bot] 5637a66
fix(hashing): address PR review comments and add schema versioning (I…
kurodo3[bot] 43f986e
test(hashing): add coverage for migrate_hash_cache, get_function_comp…
kurodo3[bot] 6391ef4
fix(hashing): address Copilot PR review round 2
kurodo3[bot] 2ef095e
fix(hashing): address Copilot PR review round 3
kurodo3[bot] b5b25d0
test(hashing): clean up unused imports and variables (Copilot review …
kurodo3[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| """SQLite hash cache schema migration script. | ||
|
|
||
| Upgrades a ``file_hash_cache`` SQLite database from schema V0 (no | ||
| ``cached_at`` column) to schema V1 (``cached_at`` column added). | ||
|
|
||
| Usage:: | ||
|
|
||
| python -m orcapod.hashing.migrate_hash_cache /path/to/cache.db | ||
|
|
||
| Or from Python code:: | ||
|
|
||
| from orcapod.hashing.migrate_hash_cache import migrate_sqlite_hash_cache | ||
| migrate_sqlite_hash_cache("/path/to/cache.db") | ||
|
|
||
| What the migration does: | ||
|
|
||
| * Adds a ``cached_at INTEGER NOT NULL DEFAULT 0`` column to the | ||
| ``file_hash_cache`` table (existing rows receive ``cached_at = 0``). | ||
| * Sets ``PRAGMA user_version = 1`` to stamp the new schema version. | ||
|
|
||
| The migration is idempotent: running it on an already-migrated database | ||
| prints a message and exits without making changes. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import sqlite3 | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def migrate_sqlite_hash_cache(db_path: "Path | str") -> None: | ||
| """Upgrade a SQLite hash cache database from schema V0 to V1. | ||
|
|
||
| Adds the ``cached_at`` column (epoch seconds; defaults to ``0`` for | ||
| existing rows) and stamps ``PRAGMA user_version = 1``. | ||
|
|
||
| Args: | ||
| db_path: Path to the SQLite database file to migrate. | ||
|
|
||
| Raises: | ||
| FileNotFoundError: If ``db_path`` does not exist. | ||
| ValueError: If the database does not contain the expected | ||
| ``file_hash_cache`` table. | ||
| """ | ||
| db_path = Path(db_path) | ||
| if not db_path.exists(): | ||
| raise FileNotFoundError(f"Database not found: {db_path}") | ||
|
|
||
| with sqlite3.connect(db_path) as conn: | ||
| # Check whether the table exists at all. | ||
| table_exists = conn.execute( | ||
| "SELECT name FROM sqlite_master " | ||
| "WHERE type='table' AND name='file_hash_cache'" | ||
| ).fetchone() | ||
| if not table_exists: | ||
| raise ValueError( | ||
| f"No 'file_hash_cache' table found in {db_path}. " | ||
| "Is this an orcapod hash cache database?" | ||
| ) | ||
|
|
||
| version = conn.execute("PRAGMA user_version").fetchone()[0] | ||
| if version >= 1: | ||
| print( | ||
| f"{db_path}: already at schema V{version} — nothing to migrate." | ||
| ) | ||
| return | ||
|
|
||
| columns = { | ||
| row[1] for row in conn.execute("PRAGMA table_info(file_hash_cache)") | ||
| } | ||
| if "cached_at" in columns: | ||
| # Table already has cached_at (was created with current code before | ||
| # version stamping was introduced). Just stamp the version. | ||
| conn.execute("PRAGMA user_version = 1") | ||
| conn.commit() | ||
| print( | ||
| f"{db_path}: 'cached_at' column already present; " | ||
| "stamped schema version to V1." | ||
| ) | ||
| return | ||
|
|
||
| print(f"{db_path}: migrating from schema V0 → V1 …") | ||
| conn.execute( | ||
| "ALTER TABLE file_hash_cache " | ||
| "ADD COLUMN cached_at INTEGER NOT NULL DEFAULT 0" | ||
| ) | ||
| conn.execute("PRAGMA user_version = 1") | ||
| conn.commit() | ||
| print( | ||
| f"{db_path}: migration complete. " | ||
| "Existing rows have cached_at = 0 (epoch origin)." | ||
| ) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """Entry point for ``python -m orcapod.hashing.migrate_hash_cache``.""" | ||
| parser = argparse.ArgumentParser( | ||
| prog="python -m orcapod.hashing.migrate_hash_cache", | ||
| description=( | ||
| "Upgrade an orcapod SQLite hash cache database " | ||
| "from schema V0 to V1 (adds the cached_at column)." | ||
| ), | ||
| ) | ||
| parser.add_argument( | ||
| "db_path", | ||
| metavar="DB_PATH", | ||
| help="Path to the SQLite hash cache database file.", | ||
| ) | ||
| args = parser.parse_args() | ||
| migrate_sqlite_hash_cache(args.db_path) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit
6391ef43. SQLite'sALTER TABLE ADD COLUMNonly accepts literal constants as DEFAULT — function calls likestrftime('%s','now')are disallowed — so the migration had to useDEFAULT 0. The real fix is inSqliteHashCacher.put(), which now explicitly suppliescached_aton every INSERT:Both freshly created and migrated databases now produce real timestamps on new rows. Pre-migration rows keep
cached_at = 0(meaning 'timestamp unknown'). A new regression test (test_put_after_migration_sets_nonzero_cached_at) verifies this end-to-end.