Skip to content

feat(cli): add db subcommand group for database management#615

Merged
lewisjared merged 6 commits intomainfrom
feat/db-commands
Apr 10, 2026
Merged

feat(cli): add db subcommand group for database management#615
lewisjared merged 6 commits intomainfrom
feat/db-commands

Conversation

@lewisjared
Copy link
Copy Markdown
Contributor

@lewisjared lewisjared commented Apr 10, 2026

Description

Add a new ref db CLI subcommand group for inspecting and managing database state directly:

  • ref db migrate -- Run pending Alembic migrations (with backup)
  • ref db status -- Check if the schema is up to date
  • ref db heads -- Show the latest migration revision(s)
  • ref db history -- Show migration history (with --last N option)
  • ref db backup -- Create a manual SQLite backup
  • ref db sql -- Execute arbitrary SQL queries
  • ref db tables -- List all tables with column counts

Also adds a database_unmigrated lazy property to CLIContext so db commands can inspect migration state without triggering migrations, while keeping lifecycle cleanup centralized (no manual try/finally needed).

Checklist

Please confirm that this pull request has done the following:

  • Tests added
  • Documentation added (where applicable)
  • Changelog item added to changelog/

Add CLI commands for inspecting and managing database state:
migrate, status, heads, history, backup, sql, tables.

Uses a new `database_unmigrated` lazy property on CLIContext
to avoid running migrations in commands that inspect migration
state directly, while keeping lifecycle cleanup centralized.
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 10, 2026

Codecov Report

❌ Patch coverage is 98.63014% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ckages/climate-ref/src/climate_ref/cli/__init__.py 88.88% 0 Missing and 1 partial ⚠️
packages/climate-ref/src/climate_ref/cli/db.py 99.19% 0 Missing and 1 partial ⚠️
Flag Coverage Δ
core 93.39% <98.63%> (+0.11%) ⬆️
providers 91.85% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/climate-ref/src/climate_ref/database.py 95.74% <100.00%> (+0.22%) ⬆️
...ckages/climate-ref/src/climate_ref/cli/__init__.py 94.94% <88.88%> (-0.61%) ⬇️
packages/climate-ref/src/climate_ref/cli/db.py 99.19% <99.19%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Cover backup with no database file, sql against unmigrated schema,
and tables listing on an empty database.
Consolidate duplicated SQLite URL parsing and in-memory detection
into a single _get_sqlite_path helper in database.py. Handles all
in-memory URL forms: sqlite://, sqlite:///:memory:, sqlite://:memory:.

Fixes a latent bug in Database.migrate where the in-memory check
compared against the wrong path format.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new ref db CLI subcommand group to inspect/manage database schema state and perform common DB maintenance tasks, while extending CLI context to support DB access without triggering migrations.

Changes:

  • Introduces ref db command group (migrate, status, heads, history, backup, sql, tables) with unit tests.
  • Adds _get_sqlite_path() helper and wires it into URL validation and pre-migration backup logic.
  • Extends CLIContext with a database_unmigrated lazy property and ensures both DB handles are closed on CLI exit.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/climate-ref/src/climate_ref/cli/db.py New Typer subcommands for DB management (migrations/status/backup/sql/etc.).
packages/climate-ref/src/climate_ref/cli/init.py Registers db group, adds database_unmigrated, and updates read-only command registry.
packages/climate-ref/src/climate_ref/database.py Adds SQLite path extraction helper and uses it for validation and backups.
packages/climate-ref/tests/unit/cli/test_db.py New unit tests covering ref db subcommands.
packages/climate-ref/tests/unit/cli/test_root.py Updates expected CLI command groups to include db.
packages/climate-ref/tests/unit/test_database.py Adds unit tests for _get_sqlite_path().
changelog/615.feature.md Changelog entry for the new ref db command group.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/climate-ref/src/climate_ref/database.py Outdated
Comment thread packages/climate-ref/src/climate_ref/cli/db.py
Comment thread packages/climate-ref/src/climate_ref/cli/db.py
Comment thread packages/climate-ref/src/climate_ref/cli/db.py Outdated
- URL-decode path in _get_sqlite_path to handle percent-encoded characters
- Validate --last >= 1 in db history command
- Add --limit option to db sql command (default 100 rows)
- Add tests for new behavior
@lewisjared lewisjared merged commit 96c3ee4 into main Apr 10, 2026
26 checks passed
@lewisjared lewisjared deleted the feat/db-commands branch April 10, 2026 14:03
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.

2 participants