Repository: allegro/ralph

Issue:
Table 'ralph_ng.transitions_transition' doesn't exist
when I follow the document to setup a develop environment, I met the error" default: django.db.utils.ProgrammingError: (1146, "Table 'ralph_ng.transitions_transition' doesn't exist") ". I think it is because there are no such tables when newly install ralph3 develop environment but ralph3 try to migrate them(from ralph2). I am on mac and have download the default box manually which will  be used in vagrant up.


Discussion:
default: Installation of Ralph and its dependencies succeeded.
    default: Starting configuration of the database.
    default: /home/vagrant/venv/lib/python3.5/site-packages/django/contrib/contenttypes/models.py:159: RemovedInDjango19Warning: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
    default:   class ContentType(models.Model):
    default: /home/vagrant/venv/lib/python3.5/site-packages/django/contrib/admin/models.py:28: RemovedInDjango19Warning: Model class django.contrib.admin.models.LogEntry doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
    default:   class LogEntry(models.Model):
    default: /home/vagrant/venv/lib/python3.5/site-packages/reversion/admin.py:16: RemovedInDjango19Warning: django.contrib.contenttypes.generic is deprecated and will be removed in Django 1.9. Its contents have been moved to the fields, forms, and admin submodules of django.contrib.contenttypes.
    default:   from django.contrib.contenttypes.generic import GenericInlineModelAdmin, GenericRelation
    default: /home/vagrant/ralph/src/ralph/admin/widgets.py:15: RemovedInDjango19Warning: The utilities in django.db.models.loading are deprecated in favor of the new application loading system.
    default:   from django.db.models.loading import get_model
    default: /home/vagrant/venv/lib/python3.5/site-packages/taggit/models.py:96: RemovedInDjango19Warning: Model class taggit.models.Tag doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
    default:   class Tag(TagBase):
    default: /home/vagrant/venv/lib/python3.5/site-packages/taggit/models.py:201: RemovedInDjango19Warning: Model class taggit.models.TaggedItem doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
    default:   class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
    default: /home/vagrant/venv/lib/python3.5/site-packages/django/contrib/auth/models.py:41: RemovedInDjango19Warning: Model class django.contrib.auth.models.Permission doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
    default:   class Permission(models.Model):
    default: /home/vagrant/venv/lib/python3.5/site-packages/django/contrib/auth/models.py:98: RemovedInDjango19Warning: Model class django.contrib.auth.models.Group doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
    default:   class Group(models.Model):
    default: /home/vagrant/venv/lib/python3.5/site-packages/django/contrib/auth/models.py:436: RemovedInDjango19Warning: Model class django.contrib.auth.models.User doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
    default:   class User(AbstractUser):
    default: [04:26:22] WARNING Statsd not installed or configured - metrics will NOT be collected
    default: /home/vagrant/venv/lib/python3.5/site-packages/django/forms/fields.py:1228: RemovedInDjango19Warning: IPAddressField has been deprecated. Use GenericIPAddressField instead.
    default:   RemovedInDjango19Warning)
    default: [04:26:23] DEBUG   Setting up handler for ConfigurationClass change of BaseObject
    default: [04:26:23] DEBUG   Setting up handler for ConfigurationModule change of BaseObject
    default: [04:26:23] DEBUG   Setting up handler for Ethernet change of BaseObject
    default: [04:26:23] DEBUG   Setting up handler for IPAddress change of BaseObject
    default: Traceback (most recent call last):
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    default:     return self.cursor.execute(sql, params)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    default:     return self.cursor.execute(query, args)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 220, in execute
    default:     self.errorhandler(self, exc, value)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    default:     raise errorvalue
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 209, in execute
    default:     r = self._query(query)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 371, in _query
    default:     rowcount = self._do_query(q)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 335, in _do_query
    default:     db.query(q)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/connections.py", line 280, in query
    default:     _mysql.connection.query(self, query)
    default: _mysql_exceptions.ProgrammingError: (1146, "Table 'ralph_ng.transitions_transition' doesn't exist")
    default: 
    default: The above exception was the direct cause of the following exception:
    default: 
    default: Traceback (most recent call last):
    default:   File "/home/vagrant/venv/bin/dev_ralph", line 11, in <module>
    default:     load_entry_point('ralph', 'console_scripts', 'dev_ralph')()
    default:   File "/home/vagrant/ralph/src/ralph/__main__.py", line 20, in dev
    default:     main('ralph.settings.dev')
    default:   File "/home/vagrant/ralph/src/ralph/__main__.py", line 16, in main
    default:     execute_from_command_line(sys.argv)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    default:     utility.execute()
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 330, in execute
    default:     self.fetch_command(subcommand).run_from_argv(self.argv)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/core/management/base.py", line 390, in run_from_argv
    default:     self.execute(*args, **cmd_options)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/core/management/base.py", line 440, in execute
    default:     self.check()
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/core/management/base.py", line 478, in check
    default:     include_deployment_checks=include_deployment_checks,
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/core/checks/registry.py", line 72, in run_checks
    default:     new_errors = check(app_configs=app_configs)
    default:   File "/home/vagrant/ralph/src/ralph/lib/transitions/apps.py", line 18, in check_transition_settings
    default:     errors.extend(check_transition_templates(transition_templates))
    default:   File "/home/vagrant/ralph/src/ralph/lib/transitions/checks.py", line 52, in check_transition_templates
    default:     for transition in transitions_with_custom_templates:
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/models/query.py", line 162, in __iter__
    default:     self._fetch_all()
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/models/query.py", line 965, in _fetch_all
    default:     self._result_cache = list(self.iterator())
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/models/query.py", line 238, in iterator
    default:     results = compiler.execute_sql()
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 837, in execute_sql
    default:     cursor.execute(sql, params)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    default:     return super(CursorDebugWrapper, self).execute(sql, params)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    default:     return self.cursor.execute(sql, params)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/utils.py", line 97, in __exit__
    default:     six.reraise(dj_exc_type, dj_exc_value, traceback)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/utils/six.py", line 658, in reraise
    default:     raise value.with_traceback(tb)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    default:     return self.cursor.execute(sql, params)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    default:     return self.cursor.execute(query, args)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 220, in execute
    default:     self.errorhandler(self, exc, value)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    default:     raise errorvalue
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 209, in execute
    default:     r = self._query(query)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 371, in _query
    default:     rowcount = self._do_query(q)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/cursors.py", line 335, in _do_query
    default:     db.query(q)
    default:   File "/home/vagrant/venv/lib/python3.5/site-packages/MySQLdb/connections.py", line 280, in query
    default:     _mysql.connection.query(self, query)
    default: django.db.utils.ProgrammingError: (1146, "Table 'ralph_ng.transitions_transition' doesn't exist")
I found the issue in **ralph/src/ralph/lib/transitions/checks.py, line 48:** it try to check transitions app but the table hasn't been created yet when check the transitions app. I think it should check if Transition table is there when do that.

**The root cause: Transition module try to inquiry the table before it was created when migrate.**
transitions_with_custom_templates = Transition.objects.exclude(   template_name__in=excluded_templates  )_//-->Here Transition.objects will try to get the table, so raise the error._

_I think it needs a check before use it, as I have added it temporaryly:_
 transitions_with_custom_templates =[]
 if Transition.check(): _//-->here, check if the Transition Table has been created, I don't know the way to check it. At least the sqlobject has a function to check if a ORM table exists.

<img width="1032" alt="ralph" src="https://user-images.githubusercontent.com/21003791/35181825-200e55c4-fdc1-11e7-9591-a227bce65a49.png">
closed it by error operation, reopen it.
Confirmed. I also see `django.db.utils.ProgrammingError: (1146, "Table 'ralph_ng.transitions_transition' doesn't exist")` at the end of the output of `vagrant up`.
yes,  I have added a judgment of Transitions temporarily, after that , I also changed the dev.txt in which "https://github.com/django-debug-toolbar/django-debug-toolbar.git@0ed1538374d3dc41d51fc562b4e8be6ce8208552" can not be found
```
(venv) vagrant@ralph-dev:~/ralph$ git diff requirements/dev.txt
diff --git a/requirements/dev.txt b/requirements/dev.txt
index 5297104..48cdd0b 100644
--- a/requirements/dev.txt
+++ b/requirements/dev.txt
@@ -1,5 +1,5 @@
 -r test.txt
-git+https://github.com/django-debug-toolbar/django-debug-toolbar.git@0ed1538374d3dc41d51fc562b4e8be6ce8208552
+git+https://github.com/django-debug-toolbar/django-debug-toolbar.git
 Werkzeug
 pudb
 ipython
(venv) vagrant@ralph-dev:~/ralph$
```

If there is **an error about importError  when "from django.utils.deprecation import MiddlewareMixin"**, that's because django 1.8 and 1.9 don't have this module in utils.deprecation, I use django 2.0.1 instead.

But now, there is always an error:**AppRegistryNotReady: Apps aren't loaded yet.**.  I can not find out how to fix  it. @rcsheets 
Traceback (most recent call last):
  File "/home/vagrant/venv/bin/dev_ralph", line 11, in <module>
    load_entry_point('ralph', 'console_scripts', 'dev_ralph')()
  File "/home/vagrant/ralph/src/ralph/__main__.py", line 18, in dev
    main('ralph.settings.dev')
  File "/home/vagrant/ralph/src/ralph/__main__.py", line 14, in main
    execute_from_command_line(sys.argv)
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/apps/registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/home/vagrant/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/vagrant/ralph/src/ralph/admin/__init__.py", line 2, in <module>
    from ralph.admin.mixins import (
  File "/home/vagrant/ralph/src/ralph/admin/mixins.py", line 13, in <module>
    from django.contrib.contenttypes.admin import GenericTabularInline
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/contrib/contenttypes/admin.py", line 5, in <module>
    from django.contrib.contenttypes.fields import GenericForeignKey
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/contrib/contenttypes/fields.py", line 3, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 134, in <module>
    class ContentType(models.Model):
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/db/models/base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "/home/vagrant/venv/lib/python3.5/site-packages/django/apps/registry.py", line 127, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
@rcsheets Could you please help me with this issue:django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. 
It will be much easier to read the diff in your comment if you indent each line with four spaces.

    Then it will be formatted like this.

This will aid in reproducing your changes.
about the error "**ralph_ng.transitions_transition' doesn't exist**", I have fixed it temporarily.

It just just add a judgement  if Transition table exists or not before  this code:
`transitions_with_custom_templates = Transition.objects.exclude( template_name__in=excluded_templates )`

If there is a warning colored with red  in the output, please ignore it.(I had ever changed the git and cause a lot of issues in the continue setting up)
`default:   Could not find a tag or branch '0ed1538374d3dc41d51fc562b4e8be6ce8208552', assuming commit.`

after the setting up finished, using "vagrant ssh" to login to vm and run "make run" in ralph folder.
@ar4s any ideas?
@lowang-bh @rcsheets #3200 should fix this issue. Please checkout yours code to ``ng`` branch and try again.
@lowang-bh I added your changes to ``dev.txt`` with fixed version of ``django-debug-toolbar`` (#3221)
@ar4s, my checkout  is the ng branch which include #3200, actually, the exception raise at 
`        transitions_with_custom_templates = Transition.objects.exclude(
            template_name__in=excluded_templates
            )`when I use ipdb to debug the issue.
`Transition.objects` will query the db but up to now the Transitions table is not created yet.
I have try all kinds of exceptions, but it didn't catch it. 

Resolving patch:
diff --git a/src/ralph/lib/transitions/checks.py b/src/ralph/lib/transitions/checks.py
--- a/src/ralph/lib/transitions/checks.py
+++ b/src/ralph/lib/transitions/checks.py
@@ -1,9 +1,14 @@
+import logging
+
 from django.core.checks import Error
-from django.db.utils import OperationalError
+from django.db.utils import DatabaseError
 from django.template.base import TemplateDoesNotExist
 from django.template.loader import get_template
 
 
+logger = logging.getLogger(__name__)
+
+
 def check_transition_templates(transition_templates):
     # to prevent AppRegistryNotReady
     from ralph.lib.transitions.models import Transition
@@ -63,6 +68,6 @@ def check_transition_templates(transition_templates):
                 ),
                 id='transitions.E004'
             ))
-    except OperationalError:
-        pass
+    except DatabaseError as e:
+        logger.error(e)
     return errors


Test patch: