Skip to main content

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 string
  • DATABASE_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metapg_cli-0.0.1.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metapg_cli-0.0.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file metapg_cli-0.0.1.tar.gz.

File metadata

  • Download URL: metapg_cli-0.0.1.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for metapg_cli-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e835eae48e17308f9d95d53753063499bb1cc9838046d96d17934e3a1b254a6e
MD5 c5d842353550b5413c94ce458ba549a7
BLAKE2b-256 4acd951db47b2beef3e0994eaa9f5c9c5771aa157a705073bf5af08197da1e91

See more details on using hashes here.

File details

Details for the file metapg_cli-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: metapg_cli-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for metapg_cli-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b503170e115d41849843364627a0bba0ce1c1b1e36dc5f88b6383b48541bba6e
MD5 62fe68ec7eede06aa50ac977b5e4b981
BLAKE2b-256 0383627009bb8bbca035f4c27fccf1256f678fe16857318634fd2aabc4e9fea5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page