Data synchronization between devel / live databases in Django

With the new Django functionality with several db in the development version, I tried to work on creating a management team that allows me to synchronize data from a live site up to the developer's machine for advanced testing. (Having actual data, in particular user input, allows me to test a wider range of inputs.)

Now I have a β€œcore” work team. It can synchronize β€œsimple” model data, but the problem I am facing is that it ignores ManyToMany fields , which I see for no reason. Anyone have any ideas on how to fix this or is it better to want to deal with it? Should I first export this first request to the device and then re-import it?

from django.core.management.base import LabelCommand
from django.db.utils import IntegrityError
from django.db import models
from django.conf import settings

LIVE_DATABASE_KEY = 'live'

class Command(LabelCommand):
    help = ("Synchronizes the data between the local machine and the live server")
    args = "APP_NAME"
    label = 'application name'

    requires_model_validation = False
    can_import_settings = True

    def handle_label(self, label, **options):

        # Make sure we're running the command on a developer machine and that we've got the right settings
        db_settings = getattr(settings, 'DATABASES', {})
        if not LIVE_DATABASE_KEY in db_settings:
            print 'Could not find "%s" in database settings.' % LIVE_DATABASE_KEY
            return

        if db_settings.get('default') == db_settings.get(LIVE_DATABASE_KEY):
            print 'Data cannot synchronize with self.  This command must be run on a non-production server.'
            return

        # Fetch all models for the given app
        try:
            app = models.get_app(label)
            app_models = models.get_models(app)
        except:
            print "The app '%s' could not be found or models could not be loaded for it." % label

        for model in app_models:
            print 'Syncing %s.%s ...' % (model._meta.app_label, model._meta.object_name)

            # Query each model from the live site
            qs = model.objects.all().using(LIVE_DATABASE_KEY)

            # ...and save it to the local database
            for record in qs:
                try:
                    record.save(using='default')
                except IntegrityError:
                    # Skip as the record probably already exists
                    pass
+5
source share
2 answers

Extending the Django Dumpscript command should help a lot.

+2
source

This doesn't exactly answer your question, but why not just do db dump and db restore?

0

All Articles