Skip to main content

Intelligent database synchronization tool for Django projects

Project description

"""

Django DB Sync

Django DB Sync is a powerful, intelligent database synchronization tool designed specifically for Django projects. It automatically detects and resolves schema differences between your Django models and database tables, eliminating the need for manual migrations in many scenarios.

Why Django DB Sync?

Unlike Django's built-in migrations system, Django DB Sync works by analyzing the current state of your database and comparing it directly with your Django models. This approach is particularly valuable when:

  • Working with legacy databases that weren't created with Django
  • Dealing with databases that have been manually modified
  • Syncing schemas across different environments
  • Cleaning up orphaned tables and unused columns
  • Requiring granular control over database schema changes

Key Features

  • Multi-Database Support: Works seamlessly with MySQL, PostgreSQL, SQLite, and Oracle
  • Intelligent Schema Detection: Automatically compares Django models with actual database schema
  • Safety First: Built-in dry-run mode and backup creation before making changes
  • Comprehensive Reporting: Detailed HTML reports and colored terminal output
  • Orphaned Table Management: Identifies and manages tables without corresponding Django models
  • Smart Field Mapping: Intelligent mapping between Django field types and database column types
  • Constraint Handling: Proper management of foreign keys, indexes, and other constraints
  • Beautiful Interface: Colored terminal output with progress indicators and status updates

Installation

pip install django-dbsync

Add to your Django settings:

INSTALLED_APPS = [
    # ... other apps
    'django_dbsync',
]

# Optional: Configure django-dbsync
DJANGO_DBSYNC = {
    'DEFAULT_DATABASE': 'default',
    'AUTO_CREATE_TABLES': True,
    'AUTO_ADD_COLUMNS': True,
    'AUTO_DROP_COLUMNS': False,
    'EXCLUDE_APPS': ['admin', 'contenttypes', 'sessions'],
    'COLORED_OUTPUT': True,
    'SHOW_ORPHANED_TABLES': True,
}

Usage

Basic Sync

# Sync default database
python manage.py dbsync

# Sync specific database
python manage.py dbsync --database=secondary

# Dry run (show changes without applying)
python manage.py dbsync --dry-run

# Auto-approve all changes (dangerous!)
python manage.py dbsync --auto-approve

# Drop orphaned tables (dangerous!)
python manage.py dbsync --drop-orphaned 

Advanced Options

# Exclude specific apps
python manage.py dbsync --exclude-apps admin auth contenttypes

# Include only specific apps
python manage.py dbsync --include-apps myapp otherapp

# Create backup before sync
python manage.py dbsync --backup

# Generate detailed report
python manage.py dbsync --report json
python manage.py dbsync --report html
python manage.py dbsync --report both

# Drop orphaned tables (dry run)
python manage.py dbsync --drop-orphaned --dry-run

Database Check

# Check database schema
python manage.py dbcheck

# Check specific database
python manage.py dbcheck --database=secondary

# Show specific table details
python manage.py dbcheck --table=my_table

# Compare with Django models
python manage.py dbcheck --compare-models

Configuration

Database Settings

Support for multiple databases:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main_db',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
    },
    'analytics': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'analytics_db',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'localhost',
    }
}

# Sync configuration per database
DJANGO_DBSYNC = {
    'CUSTOM_DATABASES': {
        'analytics': {
            'AUTO_DROP_COLUMNS': True,
            'EXCLUDE_APPS': ['admin'],
        }
    }
}

Complete Settings Reference

DJANGO_DBSYNC = {
    # Database configuration
    'DEFAULT_DATABASE': 'default',
    'CUSTOM_DATABASES': None,
    
    # Sync behavior
    'AUTO_CREATE_TABLES': True,
    'AUTO_ADD_COLUMNS': True,
    'AUTO_DROP_COLUMNS': False,
    'AUTO_RENAME_TABLES': False,
    'BACKUP_BEFORE_SYNC': True,
    
    # Output settings
    'COLORED_OUTPUT': True,
    'VERBOSE_LOGGING': True,
    'SHOW_PROGRESS': True,
    
    # Safety settings
    'EXCLUDE_APPS': ['sessions', 'admin', 'contenttypes'],
    'EXCLUDE_TABLES': [],
    'DRY_RUN_MODE': False,
    
    # Report settings
    'GENERATE_HTML_REPORT': False,
    'REPORT_OUTPUT_DIR': 'dbsync_reports/',
    'SHOW_ORPHANED_TABLES': True,
}

Supported Field Types

All Django field types are supported across MySQL, PostgreSQL, and SQLite:

  • AutoField, BigAutoField
  • CharField, TextField, EmailField, URLField, SlugField
  • IntegerField, BigIntegerField, SmallIntegerField
  • PositiveIntegerField, PositiveSmallIntegerField
  • FloatField, DecimalField
  • BooleanField
  • DateField, DateTimeField, TimeField
  • UUIDField, JSONField
  • ForeignKey, OneToOneField

Example Output

Django Database Sync v1.0.2
==================================================
Starting synchronization...

✅ myapp.User
   - Added column 'phone' to 'users'
   - Modified column 'email' in 'users'

⚠️  myapp.Order
   - Table 'orders_old' renamed to 'orders'
   - Extra column 'temp_field' in 'orders' (kept)

❌ myapp.Product
   - Failed to add column 'description'

⚠️  Orphaned Tables (2 found):
🗃️  old_backup_table - 1,247 rows, 2.45 MB
🗃️  temp_migration - 0 rows, 0.01 MB

Synchronization completed!

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

Support

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

django_dbsync-1.0.2.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

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

django_dbsync-1.0.2-py3-none-any.whl (31.9 kB view details)

Uploaded Python 3

File details

Details for the file django_dbsync-1.0.2.tar.gz.

File metadata

  • Download URL: django_dbsync-1.0.2.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for django_dbsync-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c30e63ffb5080b5f867cfdadcb2c18e406937ab9b9b1eaf258ad10e031f2862d
MD5 b8970a14f4d1b0efaf4d543bec176f9f
BLAKE2b-256 9f1d06c9d13ab08cf37288edba69649d36e77479ed793d4d06c262e32935edb4

See more details on using hashes here.

File details

Details for the file django_dbsync-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_dbsync-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 31.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for django_dbsync-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cf3511f724384e263d151e20de40ed6fcea247a759ac9cd333f4f7c56c1a7533
MD5 ebda3a8df68fb83bd4b790d7fe9df59d
BLAKE2b-256 350cc083e007265d624710d42602d3df6d263370ce23851eca8b230e05016f26

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