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.2.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.2.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_sync_package-0.2.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.2.0.tar.gz
Algorithm Hash digest
SHA256 6dbb36f859fadf334a717ffe9e31623aeea455681d779ff57e94221b73cdf317
MD5 de075fc9a2002552569766745207f79d
BLAKE2b-256 7270229b7cbd55f47051f73b108146d1f529a98e10519970dd06ffec196ea942

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_sync_package-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e4314f4d0d2110f6c6eb1dff549c2e72bac67bdfb379b9ce60289e3a1331e46a
MD5 6543666110b7ea65d8c7e3b7f0595164
BLAKE2b-256 deb3ca8c875194924c49a559a7c6d9c24cd714dc09d2b5898d8ca050a1677c46

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