﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23749	Add an example of using database routers in migrations	Alfred Perlstein	Tim Graham <timograham@…>	"It seems that the migration system in 1.7 does not properly query the dbrouter setup when a migrations.RunPython method is invoked.

Running the following command:
{{{
python manage.py migrate mycompany --noinput --traceback --database=logs --verbosity=2
}}}

Never seems to call the router for the following migration, so as you can see I've added code to parse sys.argv as an experiment which seems to sort-of work, but really looks like the wrong way to do this.

{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from applianceUI.mycompany.dbrouter import LogRouter
import sys

def bkd_default_ports(apps, schema_editor):
    db = None
    for arg in sys.argv:
        if '=' not in arg:
            continue
        print ""arg: "",  arg
        k, v = arg.split('=', 2)
        print ""k: '%s', v: '%s'  "" % (k,v)

        if k == ""--database"":
            print arg
            db = v
            break

    print ""db is %s"" % db
    router = LogRouter()
    Blockd = apps.get_model(""mycompany"", ""Blockd"")

    if router.allow_migrate(db, Blockd) is False:
        return

    try:
        bkd = Blockd.objects.order_by(""-id"")[0]
    except IndexError:
        bkd = Blockd.objects.create()
    if bkd.bkd_ports is """":
        bkd.bkd_ports = ""80, 8080, 3128""
        bkd.save()


class Migration(migrations.Migration):

    dependencies = [
        ('mycompany', '0005_auto_direction_and_indexes'),
    ]

    operations = [
        migrations.RunPython(bkd_default_ports),
    ]
}}}

The arguments apps and schema_editor didn't give me enough data to make my decision.  How can i properly derive that the database is ""logs""?

"	Cleanup/optimization	closed	Documentation	1.7	Normal	fixed	migrations dbrouter	me@…	Ready for checkin	1	0	0	0	1	0
