#23749: migrations.RunPython broken with dbrouters.
-------------------------------------+-------------------------------------
     Reporter:  alfredperlstein      |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Migrations           |                  Version:  1.7
     Severity:  Normal               |               Resolution:
     Keywords:  migrations dbrouter  |             Triage Stage:
    Has patch:  0                    |  Unreviewed
  Needs tests:  0                    |      Needs documentation:  0
Easy pickings:  0                    |  Patch needs improvement:  0
                                     |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by alfredperlstein):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Comment:

 Traceback I get is as follows:
 {{{
 + psql -U pgsql -d postgres -c 'create database historicallogs'
 CREATE DATABASE
 + python manage.py migrate mycompany --noinput --traceback --database=logs
 Operations to perform:
   Apply all migrations: mycompany
 Running migrations:
   Applying mycompany.0001_initial... OK
   Applying mycompany.0002_auto_20140904_1549... OK
   Applying mycompany.0003_auto_20141006_1134... OK
   Applying mycompany.0004_blockd_bkd_ports... OK
   Applying mycompany.0005_auto_direction_and_indexes... OK
   Applying mycompany.0006_auto_20141103_1539...Traceback (most recent call
 last):
   File "manage.py", line 42, in <module>
     execute_from_command_line(sys.argv)
   File "/usr/local/lib/python2.7/site-
 packages/django/core/management/__init__.py", line 385, in
 execute_from_command_line
     utility.execute()
   File "/usr/local/lib/python2.7/site-
 packages/django/core/management/__init__.py", line 377, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/usr/local/lib/python2.7/site-
 packages/django/core/management/base.py", line 288, in run_from_argv
     self.execute(*args, **options.__dict__)
   File "/usr/local/lib/python2.7/site-
 packages/django/core/management/base.py", line 338, in execute
     output = self.handle(*args, **options)
   File "/usr/local/lib/python2.7/site-
 packages/django/core/management/commands/migrate.py", line 160, in handle
     executor.migrate(targets, plan, fake=options.get("fake", False))
   File "/usr/local/lib/python2.7/site-
 packages/django/db/migrations/executor.py", line 63, in migrate
     self.apply_migration(migration, fake=fake)
   File "/usr/local/lib/python2.7/site-
 packages/django/db/migrations/executor.py", line 97, in apply_migration
     migration.apply(project_state, schema_editor)
   File "/usr/local/lib/python2.7/site-
 packages/django/db/migrations/migration.py", line 107, in apply
     operation.database_forwards(self.app_label, schema_editor,
 project_state, new_state)
   File "/usr/local/lib/python2.7/site-
 packages/django/db/migrations/operations/special.py", line 117, in
 database_forwards
     self.code(from_state.render(), schema_editor)
   File
 
"/usr/local/www/applianceUI/applianceUI/mycompany/migrations/0006_auto_20141103_1539.py",
 line 8, in bkd_default_ports
     bkd = Blockd.objects.config()
   File
 "/usr/local/www/applianceUI/applianceUI/freeadmin/models/__init__.py",
 line 59, in config
     obj = self.order_by("-id")[0]
   File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
 line 177, in __getitem__
     return list(qs)[0]
   File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
 line 141, in __iter__
     self._fetch_all()
   File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
 line 966, in _fetch_all
     self._result_cache = list(self.iterator())
   File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py",
 line 265, in iterator
     for row in compiler.results_iter():
   File "/usr/local/lib/python2.7/site-
 packages/django/db/models/sql/compiler.py", line 700, in results_iter
     for rows in self.execute_sql(MULTI):
   File "/usr/local/lib/python2.7/site-
 packages/django/db/models/sql/compiler.py", line 786, in execute_sql
     cursor.execute(sql, params)
   File "/usr/local/lib/python2.7/site-
 packages/django/db/backends/utils.py", line 65, in execute
     return self.cursor.execute(sql, params)
   File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line
 94, in __exit__
     six.reraise(dj_exc_type, dj_exc_value, traceback)
   File "/usr/local/lib/python2.7/site-
 packages/django/db/backends/utils.py", line 65, in execute
     return self.cursor.execute(sql, params)
   File "/usr/local/lib/python2.7/site-
 packages/django/db/backends/sqlite3/base.py", line 485, in execute
     return Database.Cursor.execute(self, query, params)
 django.db.utils.OperationalError: no such table: mycompany_blockd
 + su - pgsql -c '/usr/local/bin/pg_ctl stop -w -D /data/pgsql/data'
 waiting for server to shut down.... done
 server stopped
 + exit 1


 }}}

 My dbrouter looks like such:

 {{{
 # Router for DB logs

 postgres_models = ('log', 'logrollup', 'ipwhitelist', 'urlwhitelist')
 tmplog_models = ('blocklog',)

 class LogRouter(object):
     '''
     A router to control all database operations for historical logs.
     '''

     def db_for_read(self, model, **hints):
         '''
         Attempts to read historical logs go to logs db.
         '''
         if model._meta.model_name in postgres_models:
             return 'logs'
         if model._meta.model_name in tmplog_models:
             return 'tmplogs'
         return None

     def db_for_write(self, model, **hints):
         '''
         Attempts to read historical logs go to logs db.
         '''
         if model._meta.model_name in postgres_models:
             return 'logs'
         if model._meta.model_name in tmplog_models:
             return 'tmplogs'
         return None

     def allow_relation(self, obj1, obj2, **hints):
         # XXX: need cy to revisit this.
         # "apps" is not defined during initial migrations for some
         # reason and this throws a traceback.
         #if obj1._meta.app_label in apps or obj2._meta.app_label in apps:
         #    return True
         return None

     def allow_migrate(self, db, model):
         '''
         Make sure the historical logs only appear in the 'logs'
         database.
         '''
         if db == 'logs':
             return model._meta.model_name in postgres_models
         elif model._meta.model_name in postgres_models:
             return False
         if db == 'tmplogs':
             return model._meta.model_name in tmplog_models
         elif model._meta.model_name in tmplog_models:
             return False

         # otherwise do not allow migrations for non-logs/tmplogs
 migrations.
         if db == 'logs' or db == 'tmplogs':
             return False
         return None

 }}}

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/073.d8e8493fc8897055bcb0e156663da52c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to