Skip to main content

A Django package for database synchronization

Project description

Django Sync

Django Sync is a powerful Django utility that enables seamless data synchronization between two databases. It is designed to be simple to configure and flexible to use, supporting both manual and automatic synchronization.

Features

  • Directional Sync: Synchronize data from a default database to a slave database, or vice-versa.
  • Manual Sync Command: A sync_data management command with directional control.
  • Automatic Sync: Automatically syncs data on model save and delete operations.
  • Flexible Configuration: Easily configure which models and fields to include or exclude from synchronization.

Installation

  1. Install the package from PyPI:

    pip install django-sync-package
    
  2. Add 'sync' to your INSTALLED_APPS in settings.py:

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

Configuration

  1. Databases: In your settings.py, define your default (master) database and a slave database connection.

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'db_master',
            # ... other settings
        },
        'slave': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'db_slave',
            # ... other settings
        }
    }
    
  2. Sync Configuration: Add a SYNC_CONFIG dictionary to your settings.py to specify which models to sync.

    SYNC_CONFIG = {
        'MODELS': [
            'auth.User',
            'yourapp.YourModel',
        ],
        'EXCLUDE': {
            'auth.User': ['password', 'last_login', 'date_joined'],
        }
    }
    

Usage

Manual Synchronization

The sync_data management command allows you to perform a uni-directional sync. You can control the direction with the --direction flag.

  • Push data from default to slave:

    python manage.py sync_data --direction=master-to-slave
    
  • Pull data from slave to default:

    python manage.py sync_data --direction=slave-to-master
    

Example: Syncing Two Instances

If you are running two separate instances of a project (e.g., on different servers), you can keep them in sync by configuring each instance to recognize the other's database and running a two-step sync process:

  1. Pull changes from the remote instance: python manage.py sync_data --direction=slave-to-master
  2. Push local changes to the remote instance: python manage.py sync_data --direction=master-to-slave

Automatic Synchronization

By default, Django Sync will automatically trigger a synchronization whenever a model included in SYNC_CONFIG is saved or deleted.

  • When an object is created, updated, or deleted in the default database, the change is automatically propagated to the slave database.
  • This provides a simple way to maintain a real-time replica of your data.

Running Tests

To run the built-in tests for the sync app:

python manage.py test sync

Contributing

Contributions are welcome! If you have a feature request, bug report, or a pull request, please feel free to open an issue or submit a PR on the project's GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

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_sync_package-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

django_sync_package-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file django_sync_package-0.1.0.tar.gz.

File metadata

  • Download URL: django_sync_package-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for django_sync_package-0.1.0.tar.gz
Algorithm Hash digest
SHA256 264d8824a983fb3ab1e7b68a2a1c34497e9ce25753a5c35e3ed17610a0590505
MD5 68db7aca21692a7adffe096f1db58205
BLAKE2b-256 04181a26746bdf3dff334173fc56d790279224329a6bacd25911017b571b94e9

See more details on using hashes here.

File details

Details for the file django_sync_package-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sync_package-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c8c0fbc2647afea6545cf3ec4249547aa7ba7cc7268dfef22655e2e3fb51b3f
MD5 33c2b77f9c769001f0bfad9a96efd2a1
BLAKE2b-256 218f75108ed9ea3d5725916ff151d346aba8cf4e055fe7d5cdcfb552babf883c

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