Created
October 7, 2018 14:16
-
-
Save Eduard-gan/d5f34d3c5ea726ce90df2720d400028e to your computer and use it in GitHub Desktop.
Dump Django tables and restore em in data-migration form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from __future__ import unicode_literals | |
from django.db import migrations, models | |
# FUNCTION TO DUMP DATA | |
def backup_tables(apps, schema_editor): | |
from django.core import serializers | |
Campaign = apps.get_model('reminders', 'Campaign') | |
CampaignSnapshot = apps.get_model('reminders', 'CampaignSnapshot') | |
with open('reminders/fixtures/campaigns.json', 'w+') as file: | |
file.write(serializers.serialize('json', Campaign.objects.all())) | |
with open('reminders/fixtures/snapshots.json', 'w+') as file: | |
file.write(serializers.serialize('json', CampaignSnapshot.objects.all())) | |
class Migration(migrations.Migration): | |
dependencies = [ | |
('reminders', '0004_auto_20180914_1813'), | |
] | |
operations = [ | |
migrations.RunPython(backup_tables), # <<<<<< ----- USAGE OF THAT FUNCTION BEFORE ANY ACTIONS. Added RunPython in operations. | |
migrations.RemoveField( | |
model_name='campaign', | |
name='client', | |
), | |
#... here goes the rest of migration that is ommited due it was generated automatically |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### This migration created manyally as a "blank migration": | |
### ./manage.py makemigrations reminders --empty | |
# coding: utf-8 | |
from __future__ import unicode_literals | |
from django.db import migrations, models | |
# RESTORE FUNCTION THAT IMPLEMENTS CORRECT LOADING OF DATA INTO CHANGED SCHEMA | |
def restore_data(apps, schema_editor): | |
from datetime import datetime | |
from decimal import Decimal | |
import json | |
Client = apps.get_model('reminders', 'Client') | |
Campaign = apps.get_model('reminders', 'Campaign') | |
CampaignSnapshot = apps.get_model('reminders', 'CampaignSnapshot') | |
with open('reminders/fixtures/campaigns.json', 'r') as campaigns_file: | |
campaigns_data = json.loads(campaigns_file.read()) | |
for campaign in campaigns_data: | |
client = Client.objects.get(id=campaign['fields']['client']) | |
Campaign.objects.create(client=client, | |
direct_id=campaign['pk'], | |
name=campaign['fields']['name'], | |
archived=campaign['fields']['archived']) | |
with open('reminders/fixtures/snapshots.json', 'r') as snapshots_file: | |
snapshots_data = json.loads(snapshots_file.read()) | |
for snapshot in snapshots_data: | |
campaign = Campaign.objects.get(direct_id=snapshot['fields']['campaign']) | |
CampaignSnapshot.objects.create(campaign=campaign, | |
sum=Decimal(snapshot['fields']['sum']), | |
created=datetime.strptime(snapshot['fields']['created'], '%Y-%m-%d').date()) | |
class Migration(migrations.Migration): | |
dependencies = [ | |
('reminders', '0006_auto_20181005_1646'), | |
] | |
operations = [ | |
migrations.RunPython(restore_data) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment