Add Postgres support with SQLite backward compatibility #77

Merged
forgejo_admin merged 1 commit from 76-postgres-code-changes into main 2026-03-06 17:42:58 +00:00
Contributor

Summary

Adds Postgres (CNPG) support to pal-e-docs while maintaining full SQLite backward compatibility for local development. When PALDOCS_DATABASE_URL is set, the app uses Postgres; otherwise it falls back to PALDOCS_DATABASE_PATH (SQLite). This is PR 1 of 2 -- code changes only, no infrastructure swap.

Changes

  • src/pal_e_docs/config.py: Added database_url: str | None = None field (maps to PALDOCS_DATABASE_URL env var)
  • src/pal_e_docs/database.py: Rewrote engine creation to prefer Postgres URL, fallback to SQLite; SQLite pragmas only apply when dialect is sqlite
  • alembic/env.py: Updated to use database_url when available for migrations
  • alembic/versions/54714bc57fbe_initial_schema.py: Replaced sa.text('(CURRENT_TIMESTAMP)') with sa.func.now()
  • alembic/versions/a1b2c3d4e5f6_add_repos_table.py: Same CURRENT_TIMESTAMP fix
  • alembic/versions/b2c3d4e5f6a7_add_users_table.py: Same fix + replaced sa.text('1') with sa.true_() for boolean default
  • alembic/versions/c3d4e5f6a7b8_add_project_is_public_and_page_note_fk.py: Replaced sa.text('1') with sa.true_()
  • pyproject.toml: Added psycopg2-binary>=2.9 dependency
  • scripts/migrate_sqlite_to_postgres.py: New data migration script (SQLite dump -> Postgres load + sequence reset)

Test Plan

  • Tests pass locally (290 passed, SQLite path exercised)
  • No regressions -- all existing behavior preserved
  • Manual verification: deploy with PALDOCS_DATABASE_URL pointed at CNPG cluster
  • Deploy sequence: merge this PR -> app still uses SQLite (no behavior change). Later: apply Terraform, run alembic upgrade head against Postgres, execute migration script, then merge PR 2.

Review Checklist

  • Passed automated review-fix loop
  • No secrets committed
  • No unnecessary file changes
  • Commit messages are descriptive
  • ruff check and ruff format pass
  • plan-2026-02-26-tf-modularize-postgres -- the plan this implements (Phase 3)
  • Forgejo issue: #76
## Summary Adds Postgres (CNPG) support to pal-e-docs while maintaining full SQLite backward compatibility for local development. When `PALDOCS_DATABASE_URL` is set, the app uses Postgres; otherwise it falls back to `PALDOCS_DATABASE_PATH` (SQLite). This is PR 1 of 2 -- code changes only, no infrastructure swap. ## Changes - `src/pal_e_docs/config.py`: Added `database_url: str | None = None` field (maps to `PALDOCS_DATABASE_URL` env var) - `src/pal_e_docs/database.py`: Rewrote engine creation to prefer Postgres URL, fallback to SQLite; SQLite pragmas only apply when dialect is sqlite - `alembic/env.py`: Updated to use `database_url` when available for migrations - `alembic/versions/54714bc57fbe_initial_schema.py`: Replaced `sa.text('(CURRENT_TIMESTAMP)')` with `sa.func.now()` - `alembic/versions/a1b2c3d4e5f6_add_repos_table.py`: Same CURRENT_TIMESTAMP fix - `alembic/versions/b2c3d4e5f6a7_add_users_table.py`: Same fix + replaced `sa.text('1')` with `sa.true_()` for boolean default - `alembic/versions/c3d4e5f6a7b8_add_project_is_public_and_page_note_fk.py`: Replaced `sa.text('1')` with `sa.true_()` - `pyproject.toml`: Added `psycopg2-binary>=2.9` dependency - `scripts/migrate_sqlite_to_postgres.py`: New data migration script (SQLite dump -> Postgres load + sequence reset) ## Test Plan - [x] Tests pass locally (290 passed, SQLite path exercised) - [x] No regressions -- all existing behavior preserved - [ ] Manual verification: deploy with PALDOCS_DATABASE_URL pointed at CNPG cluster - Deploy sequence: merge this PR -> app still uses SQLite (no behavior change). Later: apply Terraform, run alembic upgrade head against Postgres, execute migration script, then merge PR 2. ## Review Checklist - [x] Passed automated review-fix loop - [x] No secrets committed - [x] No unnecessary file changes - [x] Commit messages are descriptive - [x] ruff check and ruff format pass ## Related Notes - `plan-2026-02-26-tf-modularize-postgres` -- the plan this implements (Phase 3) - Forgejo issue: #76
Add Postgres support with SQLite backward compatibility
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
23a1c20c31
- Add database_url field to Settings (PALDOCS_DATABASE_URL env var)
- Update database.py to use Postgres when database_url is set, fallback to SQLite
- Update alembic/env.py to respect database_url for migrations
- Replace SQLite-specific sa.text('(CURRENT_TIMESTAMP)') with sa.func.now()
- Replace SQLite-specific sa.text('1') with sa.true_()
- Add psycopg2-binary dependency
- Add data migration script (scripts/migrate_sqlite_to_postgres.py)

Closes #76 (PR 1 of 2)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
forgejo_admin deleted branch 76-postgres-code-changes 2026-03-06 17:42:58 +00:00
Commenting is not possible because the repository is archived.
No description provided.