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.4.tar.gz (111.5 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.4-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alembic_dump-0.1.4.tar.gz
  • Upload date:
  • Size: 111.5 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.4.tar.gz
Algorithm Hash digest
SHA256 b2c142156099a0b466a799f88dee710228db0c85b8005292ea6a4759ddac799f
MD5 f8db13984489324d18a42bbe5fdd533e
BLAKE2b-256 e3f06aca727b35862dd038bb426d870166d2c82b250dc18a72eeebec8088ba1c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alembic_dump-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 13.0 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2d4f6a4443df35baffdfcb1c4803d0053f06bd762ae672a8274eb1b5f3c8eb72
MD5 6ae0364a095ed4d41af45e4474472e2d
BLAKE2b-256 1d5e0b312552855546ca9baf24f3da5587c6addeb1acf2c5a40024d2a62d9601

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