Sunday, March 22, 2015

Data migration script populating db table - testing

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.27
Comment: GPGTools - https://gpgtools.org

iQIcBAEBCgAGBQJVDouXAAoJEE/+9O67CIlEcXgQAL8U4dm/GV2OZtz4gSdvKK6q
sfjQ4EPdqwaXKhM5OhEFk9/LZEEaqGAP+R9wjOph9jk3KqPhpu7iyaQNNkTfMQPb
zRR/wEV0FoxP637MQkm5jTD26nvTMK0T1fbHGcqMEzAYmYLnblq4riRVnqxcupQt
2oaFgNc4r1lc18ONhBbjn0E2mBV4i4VHood72Ic3KF/2WRnNS49+91o49WPLsB7y
WFsKvHv8x8Q+R2ZtoOQon1o4ovVlh7E0I3ZPa64d0k3Xrj9RVX4zbkRGUJxpIVao
uPi6oRmKrXGZ4soaT3TurdTPebJwrJG2ziqQ4LT7c+tqEa4q6Kel74BhkWxt547w
FaGosbh+Ds5h6sN/kA8hP0orYhr+aEssDheFbLaiS0hIiKVrTEvMwLyoCyL+scD6
1C3RRyhwpaqHuZMOb07xt0uP1W0kWzBa9QONIi7pLRHRhHahbWoNsK87/JH/EpEG
GhKelgYwuy4kHbVG3id9dTP2GOzD/nr9bASYsFtKuKkgqGG2v4rbt4M5Uy9XbuB2
4QSg9nigB6J+h/tWH2zB6+zJzZZ/HrOx87pLTinJB7WawoP/Vbw11FMzcLJUXf8b
YD1cw8UXqB8jemNRdmcnQivrzckOgd/x7ta2ntrmO/3AQ3XNv9h1aiBaAmvw3DvX
tkxXxsvsItwy8PDaFCHh
=zqkx
-----END PGP SIGNATURE-----
Hi

I have created an data migration script for populating one of
app db tables with data from a JSON file. First I ran

python manage.py makemigrations --empty TPP_App



('TPP_App' is the name of my app) and then I added a custom
method called populate_db which will use bulk_create to add
multiple entries to the table, and then do a save() to write the
changes to the db. At the moment the custom method is empty
because I am still working on it, but the script looks like this right
now

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

def populate_db( apps, schema_editor ):
print 'Populating db from JSON file...'

class Migration(migrations.Migration):

dependencies = [
('TPP_App', '0003_auto_20150224_2024'),
]

operations = [
migrations.RunPython( populate_db ),
]

How do I test this script to make sure the custom method works
as expected, before I run it fully? Can I call this from the interpreter
and test it with the db API?

At the moment if I do

python manage.py migrate

it reports there are no migrations to apply.

Sandeep


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10767D81-937A-4F08-983A-B055C4AB3982%40sandeepmurthy.is.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment