Rich command-line interface for PostgreSQL migrations and pool management
Project description
metapg.cli
Rich command-line interface for PostgreSQL operations
metapg.cli provides a beautiful terminal interface for managing PostgreSQL databases, migrations, and connection pools with rich formatting and intuitive commands.
Installation
pip install metapg.cli
This will install the metapg command globally.
Quick Start
# Migration commands
metapg migration status --db main
metapg migration apply --db main
metapg migration rollback --db main --steps 1
metapg migration create add_users_table --db main
# Pool management commands
metapg pool init mydb --dsn postgresql://localhost/mydb
metapg pool status mydb
metapg pool close mydb
# Backwards compatible (migration commands at root)
metapg status --db main
metapg apply --db main
Features
- 🎨 Rich Interface - Beautiful tables, colors, and formatting
- 📊 Migration Management - Status, apply, rollback, and create migrations
- 🔗 Pool Management - Initialize, monitor, and close connection pools
- ⚡ Fast Operations - Efficient database operations with progress indicators
- 🛡️ Safe Defaults - Confirmation prompts for destructive operations
- 🎛️ Multi-Database - Support for multiple named databases
Commands
Migration Commands
# Show migration status
metapg migration status [--db DATABASE] [--migrations-dir DIR]
# Apply pending migrations
metapg migration apply [--db DATABASE] [--target MIGRATION] [--force]
# Rollback migrations
metapg migration rollback [--db DATABASE] [--steps N]
# Create new migration
metapg migration create MIGRATION_NAME [--db DATABASE]
Pool Commands
# Initialize connection pool
metapg pool init [DATABASE] [--dsn CONNECTION_STRING] [--min-size N] [--max-size N]
# Show pool status
metapg pool status [DATABASE]
# Close connection pool
metapg pool close [DATABASE] [--force]
Global Options
--help- Show help message--db DATABASE- Specify database name (default: "default")--force- Skip confirmation prompts
Configuration
Environment Variables
DATABASE_URL- Default database connection stringDATABASE_URL_{NAME}- Connection string for named database
Migration Directory Structure
migrations/
├── default/ # Default database migrations
│ ├── 001_initial.sql
│ └── 002_add_users.sql
└── analytics/ # Named database migrations
└── 001_events.sql
Migration File Format
-- Create users table
-- Created: 2024-01-01
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- ROLLBACK (optional)
DROP TABLE users;
Examples
Basic Migration Workflow
# Check current status
metapg migration status
# Create new migration
metapg migration create add_posts_table
# Edit the generated SQL file, then apply
metapg migration apply
# Check status again
metapg migration status
Multi-Database Setup
# Set up multiple databases
export DATABASE_URL="postgresql://localhost/main_db"
export DATABASE_URL_ANALYTICS="postgresql://localhost/analytics_db"
# Manage each database separately
metapg migration status --db default
metapg migration status --db analytics
metapg migration apply --db analytics
Pool Management
# Initialize pools for different databases
metapg pool init main --dsn postgresql://localhost/main_db
metapg pool init analytics --dsn postgresql://localhost/analytics_db
# Monitor pool status
metapg pool status main
metapg pool status analytics
# Close when done
metapg pool close main
Rich Output
The CLI provides beautiful, colored output with:
- 📊 Status Tables - Clear migration status with timestamps
- ✅ Success Indicators - Green checkmarks for completed operations
- ⚠️ Warning Messages - Yellow alerts for pending operations
- ❌ Error Messages - Red indicators for failed operations
- 📈 Progress Bars - Visual progress for long operations
Integration
Programmatic Usage
import metapg.cli
# Access the CLI app directly
app = metapg.cli.app
# Use in scripts or other applications
if __name__ == "__main__":
app()
CI/CD Integration
# GitHub Actions example
- name: Run migrations
run: |
metapg migration apply --force --db production
metapg migration status --db production
License
MIT License - see LICENSE file for details.
Part of metapg
This package is part of the metapg metapackage for PostgreSQL operations.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file metapg_cli-0.0.2.tar.gz.
File metadata
- Download URL: metapg_cli-0.0.2.tar.gz
- Upload date:
- Size: 30.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.13.2 Darwin/23.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73bb4dd0681616e0dc2c53c42c3520ab6bc984aa43a887f9b3c906bc8321e517
|
|
| MD5 |
63611668b5b02a6c31388db7a763bd36
|
|
| BLAKE2b-256 |
336ba9a84670ed17316222e2570f94cdb04ac39bbbfcbbfc7fdb9718bd1423ce
|
File details
Details for the file metapg_cli-0.0.2-py3-none-any.whl.
File metadata
- Download URL: metapg_cli-0.0.2-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.13.2 Darwin/23.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e178a65a73cdd168f64c1b990e4192eebba1706134cd16a66eacb283dee6a7e1
|
|
| MD5 |
853a57edf7709afabfdd3c8a64993284
|
|
| BLAKE2b-256 |
3c6ceeff48f75c5cc748afdd72a5401c234a340e66d65a6a2d75f8ab66df3e18
|