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

🛠️ Core Capabilities

  1. Table Management: Create, rename, and manage database tables
  2. Column Operations: Add, modify, and remove columns with proper type mapping
  3. Constraint Handling: Manage foreign keys, unique constraints, and indexes
  4. Data Preservation: Safely modify schemas while preserving existing data
  5. Backup Integration: Automatic backup creation before destructive operations
  6. Detailed Reporting: Comprehensive logs and HTML reports of all operations

🔧 Technical Highlights

  • Database Agnostic: Works with all major database backends supported by Django
  • Type-Safe Operations: Intelligent field type mapping and validation
  • Transaction Safety: All operations wrapped in database transactions
  • Extensible Architecture: Modular design for easy customization and extension
  • Production Ready: Thoroughly tested with comprehensive error handling

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

# Basic sync commands
python manage.py dbsync  # Sync default database
python manage.py dbsync --database=secondary  # Sync specific database
python manage.py dbsync --dry-run  # Show changes without applying
python manage.py dbsync --auto-approve  # Auto-approve all changes (dangerous!)
python manage.py dbsync --drop-orphaned  # Drop orphaned tables (dangerous!)

Advanced Options

# App management
python manage.py dbsync --exclude-apps admin auth contenttypes  # Exclude specific apps
python manage.py dbsync --include-apps myapp otherapp  # Include only specific apps

# Backup and reporting
python manage.py dbsync --backup  # Create backup before sync
python manage.py dbsync --report json  # Generate JSON report
python manage.py dbsync --report html  # Generate HTML report
python manage.py dbsync --report both  # Generate both JSON and HTML reports

# Safety checks
python manage.py dbsync --drop-orphaned --dry-run  # Check what would be dropped
python manage.py dbsync --suggest-manual-commands  # Show manual SQL commands
python manage.py dbsync --generate-orphaned-models  # Generate models for orphaned tables

Database Check

# Database checking commands
python manage.py dbcheck  # Check database schema
python manage.py dbcheck --database=secondary  # Check specific database
python manage.py dbcheck --table=my_table  # Show specific table details
python manage.py dbcheck --compare-models  # Compare with Django models
python manage.py dbcheck --check-case-mismatches  # Check for case mismatches
python manage.py dbcheck --check-name-conflicts  # Check for name conflicts
python manage.py dbcheck --verbose  # Show detailed information
python manage.py dbcheck --fix  # Attempt to fix issues automatically
python manage.py dbcheck --include-apps=app1,app2  # Check specific apps only
python manage.py dbcheck --include-tables=table1,table2  # Check specific tables only

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,
    'AUTO_FIX_TABLE_CASE': True,  # Automatically fix table name case mismatches
    '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
  • FileField, ImageField
  • ForeignKey, OneToOneField, ManyToManyField

Table Name Case Handling

Django-dbsync automatically detects and handles table name case mismatches between your Django models and the database. This is common when:

  • Your model has db_table = 'MemberLicense' but the database table is memberlicense
  • Database systems are case-insensitive but Django models use specific casing
  • Tables were created with different naming conventions

Automatic Case Fixing

By default, the tool will automatically fix case-only mismatches (when AUTO_FIX_TABLE_CASE = True):

# The tool will automatically rename 'memberlicense' to 'MemberLicense'
python manage.py dbsync

Manual Control

To disable automatic case fixing and get prompted for each rename:

DJANGO_DBSYNC = {
    'AUTO_FIX_TABLE_CASE': False,
}

Checking for Case Mismatches

To check for table name case mismatches without fixing them:

python manage.py dbcheck --check-case-mismatches

This will show you all mismatches found and provide guidance on how to fix them.

Manual SQL Commands for Table Renames

When table name conflicts are detected, you can get manual SQL commands to resolve them:

python manage.py dbsync --dry-run --suggest-manual-commands

This will show you the exact SQL commands needed to rename tables manually:

============================================================
🔧 MANUAL SQL COMMANDS FOR TABLE RENAMES
============================================================
The following SQL commands can be run manually to rename tables:

1. MySQL case-insensitive conflict resolution: 'publisher_detail2' → 'Publisher_detail2'
   SQL: RENAME TABLE `publisher_detail2` TO `Publisher_detail2`;

💡 Instructions:
   1. Connect to your database using your preferred SQL client
   2. Run the commands above one by one
   3. Run 'python manage.py dbsync' again to complete the sync
   4. Make sure to backup your database before running these commands!

This gives you full control over table renaming operations while ensuring data safety.

Note: Manual SQL commands are automatically displayed in dry-run mode, so you don't need the --suggest-manual-commands flag anymore.

Generating Models for Orphaned Tables

When orphaned tables are found, you can generate Django models for them:

python manage.py dbsync --dry-run --generate-orphaned-models

This creates a Python file with Django models for all orphaned tables:

# Django Models for Orphaned Tables
# Generated by django-dbsync on 2025-07-28 10:34:31
# 
# Instructions:
# 1. Copy the models you want to keep to your Django app's models.py
# 2. Remove the 'managed = False' line if you want Django to manage the table
# 3. Update the Meta class as needed
# 4. Run 'python manage.py makemigrations' and 'python manage.py migrate'

from django.db import models

# Table: publisher
# Rows: 0, Size: 0.02 MB
class Publisher(models.Model):
    """
    Auto-generated model for table 'publisher'
    Generated by django-dbsync
    """
    name = models.CharField(max_length=100, null=False, blank=False)
    website = models.CharField(max_length=200, null=False, blank=False)
    created_at = models.DateTimeField(null=False, blank=False)

    class Meta:
        db_table = 'publisher'
        managed = False  # Django won't manage this table

    def __str__(self):
        return f'Publisher(id={self.id})'

Benefits:

  • Easy retention: Copy models to keep orphaned tables
  • Auto-generated: No manual model writing needed
  • Safe: Uses managed = False by default
  • Complete: Includes all field types and constraints

Table Name Conflicts

Sometimes databases can have both lowercase and uppercase versions of the same table name (e.g., publisher and Publisher). This can cause issues with table renaming operations.

To check for table name conflicts:

python manage.py dbcheck --check-name-conflicts

This will identify any tables that have case conflicts and provide guidance on how to resolve them.

Example conflict scenario:

  • Database has both publisher and Publisher tables
  • Django model expects Publisher
  • The tool will detect this conflict and avoid the rename operation
  • You'll need to manually resolve the conflict before syncing

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.9.tar.gz (39.1 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.9-py3-none-any.whl (41.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_dbsync-1.0.9.tar.gz
  • Upload date:
  • Size: 39.1 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.9.tar.gz
Algorithm Hash digest
SHA256 a50e29c5f1219c19fe3f022db936956d76253db3fa6e72d7a1c8f692ec4478e3
MD5 b676a5da46b708dc2f0d012f0f8cb800
BLAKE2b-256 8cb996bc074d81a54c33516cb180967a86a808a17b1fca503c03aae7f8668ad6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_dbsync-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 41.5 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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d4b4f48f3519dc273592cd3f80461d2d454f8657bee48bbf69cefd13e17c7905
MD5 7999c3a2f0014bc9626c785c094594ac
BLAKE2b-256 73488a5a31ee220a1a6e5d39794ea61adda7d2c100d09d51d7af06e99f7b870b

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