Skip to content

fix: a media path over 500 chars fails on PostgreSQL, and nothing tested PostgreSQL at all - #293

Merged
GeiserX merged 1 commit into
mainfrom
ai/pg-first-class
Aug 15, 2026
Merged

GeiserX merged 1 commit into
mainfrom
ai/pg-first-class

Conversation

@GeiserX

@GeiserX GeiserX commented Aug 15, 2026

Copy link
Copy Markdown
Owner

The problem

PostgreSQL was never actually exercised. The test workflow had no database service, and every "postgres" test asserted on mock calls — mock_session.execute.assert_awaited_once() — so the SQL those paths generate was never compiled, let alone run. One mock helper is used ~145 times against a handful of real-SQLite tests. That is a check that cannot go red.

Meanwhile there were two schema authorities: SQLite built by create_all, PostgreSQL built only by Alembic. They drifted, and nothing could notice.

Measured, not assumed: 54 differences on SQLite, 50 on PostgreSQL.

One is a live bug rather than cosmetics:

INSERT INTO media (id, file_path) VALUES ('probe1', repeat('a', 600));
-- Alembic-built database: ERROR: value too long for type character varying(500)
-- ORM-built database:     INSERT 0 1

media.file_path is VARCHAR(500) on a real PostgreSQL install but Text in the models — the comment says "changed to Text for long paths" but no migration ever widened it. A media path over 500 characters hard-fails on PostgreSQL and works on SQLite.

Two more the audit had missed: the reactions table has no foreign key to messages in the migrated schema on either backend (so reaction rows are orphanable), and on SQLite migration 005's rebuild dropped its other foreign key too, leaving that table with none. PostgreSQL also carries two unused nextval() sequences from a BIGSERIAL the models explicitly disclaim.

One audit claim turned out stale and is reported as such: the audit-log indexes it flagged as missing are now declared — #288 added them. There is no index drift anywhere, on either backend.

The fix

Migration 021 aligns the migrated schema with the models on both backends: nullability on 35 columns (backfill, then tighten), the declared defaults, the file_path widening, the missing reaction foreign key, eight SQLite integer widths, and the empty rollback table migration 005 leaves behind on every fresh install.

Every step reads the live schema first and does nothing where the shape already matches — so an install provisioned by create_all, which is what most existing users have, rebuilds nothing. SQLite rebuilds go through batch_alter_table per table, only where that table still differs.

CI now runs a real PostgreSQL service, and a new fixture yields a real adapter on a real engine, parameterised over both backends, skipping cleanly when no server is reachable so a laptop without Docker still passes. The workflow fails if the PostgreSQL leg skips — a silently skipped backend is exactly how this drift survived.

A schema-parity test builds both schemas both ways on both backends and fails on any difference in tables, columns, types, nullability, defaults, indexes, unique constraints, primary keys or foreign keys.

Also fixes a pre-existing crash loop found while working in the entrypoint: its SQLite path chain omitted DATABASE_DIR, so such an install was inspected at one path and migrated at another, and Alembic then re-ran the initial migration against the real database.

Two deletions, stated loudly

  • _messages_media_backup is dropped only when empty; a copy holding a real pre-normalization row survives untouched (tested).
  • Orphan reaction rows are deleted, but only on a database about to gain fk_reaction_message, because PostgreSQL refuses the constraint otherwise. A create_all-built database already declares that FK, so nothing is deleted there.

Verification

  • Eight real upgrade shapes, each "old" database built by git archive origin/main's own models and migration chain — never the current tree — seeded with real rows and digested row-by-row, column-by-column, before and after. Fresh SQLite, fresh PostgreSQL, existing create_all-provisioned SQLite (the common real-user case; the shipped stamping ladder was extracted verbatim from entrypoint.sh and run, not reimplemented), existing PostgreSQL at the previous head, both backends from older revisions, and a re-run. Every row byte-identical where it should be.
  • The reviewer diffed deeper than the committed test and without any allow-list — raw sqlite_master DDL text, and pg_attribute/pg_constraint/pg_indexes/pg_sequences directly. Zero differences after normalising cosmetic ordering.
  • The gate was proven able to go red: four hand-injected drifts, all caught, including one that fails on the PostgreSQL leg only — proving that leg is genuinely independent rather than decorative.
  • Full suite with a real PostgreSQL configured: 2889 passed, 171 subtests, 0 failed, 0 skipped, and zero hits for the PostgreSQL skip reason. ruff clean.

Known and deliberate

create_all stays as a fallback for the viewer image, which ships no alembic/ and has no entrypoint, so it cannot migrate. It is limited to SQLite because on PostgreSQL it would race a concurrently migrating container. This is now two provably-equal paths rather than two drifting ones — the parity test fails if they diverge again. Making it literally one path means shipping Alembic into the viewer image, which is a bigger blast radius than this change warranted and is tracked separately.

No version bump: still 7.33.6. The single 8.0.0 bump comes at the end of the programme.

…ends for real

PostgreSQL was never actually exercised. The test workflow had no database
service, and the "postgres" tests asserted on mock calls, so the SQL they
generate was never compiled, let alone run. Meanwhile SQLite was built by
create_all and PostgreSQL only by Alembic, so the two schemas drifted with
nothing able to notice.

Measured: 54 differences on SQLite, 50 on PostgreSQL. One of them is a live
bug rather than cosmetics — media.file_path is VARCHAR(500) on a real
PostgreSQL install but Text in the models, so a media path over 500 characters
hard-fails there and works on SQLite. Two more: the reactions table has no
foreign key to messages in the migrated schema on either backend, and
PostgreSQL carries two unused sequences from a BIGSERIAL that the models
explicitly disclaim.

Migration 021 aligns the migrated schema with the models on both backends:
nullability on 35 columns, the declared defaults, the file_path widening, the
missing reaction foreign key, eight SQLite integer widths, and the empty
rollback table migration 005 leaves behind. Every step reads the live schema
first and does nothing where the shape already matches, so an install
provisioned by create_all rebuilds nothing.

CI now runs a real PostgreSQL service, and a new fixture yields a real adapter
on a real engine parameterised over both backends. The workflow fails if the
PostgreSQL leg skips, because a silently skipped backend is how this drift
survived. A schema-parity test builds both schemas and fails on any difference.

Also fixes a pre-existing crash loop found while working in the entrypoint: its
SQLite path chain omitted DATABASE_DIR, so such an install was inspected at one
path and migrated at another, and Alembic then re-ran the initial migration
against the real database.
@coderabbitai

coderabbitai Bot commented Aug 15, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@GeiserX, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 10 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: dd224ce0-04d5-432d-8f51-682759410ca9

📥 Commits

Reviewing files that changed from the base of the PR and between 05102c5 and 100541b.

⛔ Files ignored due to path filters (1)
  • alembic/versions/20260815_021_align_schema_with_orm.py is excluded by !alembic/versions/**
📒 Files selected for processing (7)
  • .github/workflows/tests.yml
  • scripts/entrypoint.sh
  • src/db/base.py
  • src/db/models.py
  • tests/conftest.py
  • tests/test_db_adapter_real_engine.py
  • tests/test_schema_parity.py

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.

@github-actions

Copy link
Copy Markdown

🐳 Dev images published!

  • drumsergio/telegram-archive:dev
  • drumsergio/telegram-archive-viewer:dev

The dev/test instance will pick up these changes automatically (Portainer GitOps).

To test locally:

docker pull drumsergio/telegram-archive:dev
docker pull drumsergio/telegram-archive-viewer:dev

@codecov

codecov Bot commented Aug 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.82%. Comparing base (05102c5) to head (100541b).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #293      +/-   ##
==========================================
+ Coverage   93.79%   93.82%   +0.02%     
==========================================
  Files          27       27              
  Lines        8965     8965              
==========================================
+ Hits         8409     8411       +2     
+ Misses        556      554       -2     
Files with missing lines Coverage Δ
src/db/base.py 99.31% <ø> (ø)
src/db/models.py 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GeiserX
GeiserX merged commit 7f4aaf0 into main Aug 15, 2026
11 checks passed
@GeiserX
GeiserX deleted the ai/pg-first-class branch August 15, 2026 16:25
GeiserX added a commit that referenced this pull request Oct 2, 2026
…ends for real (#293)

PostgreSQL was never actually exercised. The test workflow had no database
service, and the "postgres" tests asserted on mock calls, so the SQL they
generate was never compiled, let alone run. Meanwhile SQLite was built by
create_all and PostgreSQL only by Alembic, so the two schemas drifted with
nothing able to notice.

Measured: 54 differences on SQLite, 50 on PostgreSQL. One of them is a live
bug rather than cosmetics — media.file_path is VARCHAR(500) on a real
PostgreSQL install but Text in the models, so a media path over 500 characters
hard-fails there and works on SQLite. Two more: the reactions table has no
foreign key to messages in the migrated schema on either backend, and
PostgreSQL carries two unused sequences from a BIGSERIAL that the models
explicitly disclaim.

Migration 021 aligns the migrated schema with the models on both backends:
nullability on 35 columns, the declared defaults, the file_path widening, the
missing reaction foreign key, eight SQLite integer widths, and the empty
rollback table migration 005 leaves behind. Every step reads the live schema
first and does nothing where the shape already matches, so an install
provisioned by create_all rebuilds nothing.

CI now runs a real PostgreSQL service, and a new fixture yields a real adapter
on a real engine parameterised over both backends. The workflow fails if the
PostgreSQL leg skips, because a silently skipped backend is how this drift
survived. A schema-parity test builds both schemas and fails on any difference.

Also fixes a pre-existing crash loop found while working in the entrypoint: its
SQLite path chain omitted DATABASE_DIR, so such an install was inspected at one
path and migrated at another, and Alembic then re-ran the initial migration
against the real database.
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