[Django] #22583: Allow per-database migrations in multidb configurations

30 views
Skip to first unread message

Django

unread,
May 6, 2014, 7:00:02 AM5/6/14
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
--------------------------------------+------------------------
Reporter: akaariai | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
Currently a database router can be used to skip operations on given model.
However, this approach doesn't work at all for RunPython or RunSQL
commands - especially RunSQL has no notion of which models the SQL
touches. For this reason there should be some way to specify which
databases the migration should apply to. The simplest way seems to be
addition of using kwarg to RunPython and RunSQL commands. For example:
{{{
operations = [
RunSQL("create table foobar(id serial primary key);",
using='default'),
RunSQL("create table foobar(id integer primary key auto_increment)
engine myisam;",
using=('mysql_alias1', 'mysql_alias2'))
]
}}}

However the problem here is that this assumes the migrations know the used
database aliases. This doesn't work for 3rd party app migrations. For
these cases "for_vendor='mysql'/'postgresql'/..." argument would work
better.

Still, there might be need to allow skipping whole apps for migations -
say you have a 3rd party app you don't want to migrate on database other -
my understanding is that one can only skip all model operations on given
database, but there is no way to force skip of RunSQL and RunPython
commands, too. If it would be possible to define that the app in whole
shouldn't be migrated on database other, then there would be no problem
when running manage.py migrate.

--
Ticket URL: <https://code.djangoproject.com/ticket/22583>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 12, 2014, 8:40:55 AM5/12/14
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
-----------------------------+------------------------------------
Reporter: akaariai | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by timo):

* type: Bug => New feature
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/22583#comment:1>

Django

unread,
Aug 25, 2014, 2:34:16 PM8/25/14
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
-----------------------------+------------------------------------
Reporter: akaariai | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------

Comment (by andrewgodwin):

So, it's worth pointing out that RunPython can just switch on the value of
`schema_editor.connection` if it wants to make decisions about the current
database, so I'm not sure if I want to add extra logic to RunSQL?
Especially a run-per-backend thing...

--
Ticket URL: <https://code.djangoproject.com/ticket/22583#comment:2>

Django

unread,
Sep 5, 2014, 2:25:17 PM9/5/14
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
-----------------------------+------------------------------------
Reporter: akaariai | Owner: nobody
Type: New feature | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by andrewgodwin):

* status: new => closed
* resolution: => wontfix


Comment:

I'm going to WONTFIX this and suggest that people use RunPython instead to
do any custom logic; they can either call RunSQL from inside that or, more
easily, just use schema_editor.execute() if they wish to run raw SQL.

--
Ticket URL: <https://code.djangoproject.com/ticket/22583#comment:3>

Django

unread,
Oct 28, 2014, 1:19:14 PM10/28/14
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
-----------------------------+------------------------------------
Reporter: akaariai | Owner: nobody
Type: New feature | Status: closed
Component: Migrations | Version: master

Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------

Comment (by lorden):

I had this problem too and RunPython was not reliable enough. I created a
package to run raw SQL on a specific connection similar to the example
given in the ticket description: [https://pypi.python.org/pypi/django-
migrations-plus django-migrations-plus]. Contributions are appreciated!

--
Ticket URL: <https://code.djangoproject.com/ticket/22583#comment:4>

Django

unread,
Jan 8, 2015, 4:57:44 AM1/8/15
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
-----------------------------+------------------------------------
Reporter: akaariai | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by loic):

* status: closed => new
* has_patch: 0 => 1
* resolution: wontfix =>


Comment:

Reopening after discussing this on multiple occasions on IRC.

`RunPython` and `RunSQL` should query the database router like every other
operations and hints can be used to assist the router in taking a
decision. This is consistent with how db routing is performed in the other
parts of the system (i.e. `db_for_read` and `db_for_write`):

{{{
class MyRouter(object):
def allow_migrate(self, db, model, **hints):
if 'target_db' in hints:
return db == hints['target_db']
return True

RunPython(forwards, hints={'target_db': 'default'})
}}}

This also allows completely opting out of migrations for any given db
with:

{{{
class MyRouter(object):
def allow_migrate(self, db, model, **hints):
return db != 'excluded_db'
}}}

PR https://github.com/django/django/pull/3849

--
Ticket URL: <https://code.djangoproject.com/ticket/22583#comment:5>

Django

unread,
Jan 8, 2015, 6:45:56 AM1/8/15
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
-----------------------------+------------------------------------
Reporter: akaariai | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by loic):

* cc: loic (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/22583#comment:6>

Django

unread,
Jan 8, 2015, 8:16:35 AM1/8/15
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
-----------------------------+---------------------------------------------
Reporter: akaariai | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------------
Changes (by MarkusH):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/22583#comment:7>

Django

unread,
Jan 9, 2015, 1:22:28 PM1/9/15
to django-...@googlegroups.com
#22583: Allow per-database migrations in multidb configurations
-----------------------------+---------------------------------------------
Reporter: akaariai | Owner: nobody
Type: New feature | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------------
Changes (by Loic Bistuer <loic.bistuer@…>):

* status: new => closed

* resolution: => fixed


Comment:

In [changeset:"8f4877c89d0f28e289399fbb46357623a49e7eb0"]:
{{{
#!CommitTicketReference repository=""
revision="8f4877c89d0f28e289399fbb46357623a49e7eb0"
Fixed #22583 -- Allowed RunPython and RunSQL to provide hints to the db
router.

Thanks Markus Holtermann and Tim Graham for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22583#comment:8>

Reply all
Reply to author
Forward
0 new messages