Skip to main content

A Python library to dump, load, and mask data between databases in Alembic-managed environments, with SSH tunnel support.

Project description

Alembic Dump

A Python library to dump, load, and mask data between databases (e.g., PostgreSQL, MySQL) in environments managed by Alembic, with SSH tunnel support.

This tool is designed to help developers synchronize database schemas using Alembic revisions and then transfer data, optionally applying masking rules to sensitive information. It's particularly useful for creating staging or development environments from production data, or for migrating data between different database instances while maintaining schema integrity.

Key Features

  • Schema Synchronization: Ensures target database schema matches the source database schema based on Alembic revisions before data transfer.
  • Data Dump & Load: Efficiently transfers data table by table, respecting foreign key constraints by processing tables in a topologically sorted order.
  • Data Masking: Supports various strategies (e.g., hashing, partial masking, using Faker) to anonymize sensitive data during the transfer. Masking rules can be configured per table and per column.
  • SSH Tunneling: Built-in support for connecting to databases via an SSH bastion host.
  • Configuration: Uses Pydantic for clear and validated configuration of database connections, SSH tunnels, and masking rules.
  • Chunking: Processes data in chunks to manage memory usage effectively, especially for large tables.
  • Flexible Table Selection: Allows specifying tables to include or exclude from the dump/load process.

Installation

You can install alembic-dump using pip:

pip install alembic-dump

baisc usage

from alembic_dump.config import AppSettings, DBConfig, MaskingConfig, MaskingRule
from alembic_dump.core import dump_and_load

# 1. Define Source and Target Database Configurations
source_db_config = DBConfig(
    driver="postgresql",
    host="source.db.example.com",
    port=5432,
    username="user",
    password="password", # In real use, manage secrets carefully (e.g., env vars)
    database="sourcedb"
)

target_db_config = DBConfig(
    driver="postgresql",
    host="target.db.example.com",
    port=5432,
    username="user",
    password="password",
    database="targetdb"
)

# 2. (Optional) Define Masking Rules
masking_config = MaskingConfig(
    rules={
        "users": {
            "email": MaskingRule(strategy="hash"),
            "full_name": MaskingRule(strategy="faker", faker_provider="name")
        },
        "sensitive_logs": {
            "ip_address": MaskingRule(strategy="null")
        }
    }
)

# 3. (Optional) Define SSH Tunnel Configuration if needed
# ssh_config = SSHConfig(...)

# 4. Create AppSettings
app_settings = AppSettings(
    source_db=source_db_config,
    target_db=target_db_config,
    # ssh_tunnel=ssh_config, # Uncomment if using SSH tunnel
    masking=masking_config,
    chunk_size=1000,
    tables_to_exclude=["some_large_irrelevant_table"],
    # tables_to_include=["users", "orders"] # Only include these if specified
)

# 5. Specify the path to your Alembic migrations directory
# This directory should contain your alembic.ini and version scripts.
alembic_migrations_directory = "/path/to/your/alembic_migrations" 
# For testing, you might use a dedicated test Alembic environment.

# 6. Run the dump and load process
try:
    dump_and_load(settings=app_settings, alembic_dir=alembic_migrations_directory)
    print("Data dump and load completed successfully!")
except Exception as e:
    print(f"An error occurred: {e}")

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

alembic_dump-0.1.2.tar.gz (110.9 kB view details)

Uploaded Source

Built Distribution

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

alembic_dump-0.1.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file alembic_dump-0.1.2.tar.gz.

File metadata

  • Download URL: alembic_dump-0.1.2.tar.gz
  • Upload date:
  • Size: 110.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for alembic_dump-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5ed444dfd781bed970b11c62aefddda5457120d778ae4ffb2d770af6566497d5
MD5 3ef138ef86b4c535654b1615c1ce1a04
BLAKE2b-256 ec8347781570e6a0f0a6cc5e38631c68ed345bb6f1c9653125dc228d5e4fa0ca

See more details on using hashes here.

File details

Details for the file alembic_dump-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: alembic_dump-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for alembic_dump-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5dd1627fa80f5eaa32e365e995b08a1e5c9dd5b5a079697fe24cab4ecf71d806
MD5 cb59eb367c7bbfed87c7fceb10358996
BLAKE2b-256 5837824b4723aeab894055537e2c15556c949a0e780ed39383fa5cc102ea8436

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