feat(cli): add db subcommand group for database management#615
Merged
lewisjared merged 6 commits intomainfrom Apr 10, 2026
Merged
feat(cli): add db subcommand group for database management#615lewisjared merged 6 commits intomainfrom
lewisjared merged 6 commits intomainfrom
Conversation
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 Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.
Contributor
There was a problem hiding this comment.
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 dbcommand 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
CLIContextwith adatabase_unmigratedlazy 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.
- 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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Add a new
ref dbCLI 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 dateref db heads-- Show the latest migration revision(s)ref db history-- Show migration history (with--last Noption)ref db backup-- Create a manual SQLite backupref db sql-- Execute arbitrary SQL queriesref db tables-- List all tables with column countsAlso adds a
database_unmigratedlazy property toCLIContextso db commands can inspect migration state without triggering migrations, while keeping lifecycle cleanup centralized (no manualtry/finallyneeded).Checklist
Please confirm that this pull request has done the following:
changelog/