lundi 19 décembre 2016

Django test runner causing db operational errors

I have inherited a website built with Python 3.4, Django 1.10 with a MySQL database that has been built with no tests in place except for some old, unused legacy tests. I have since started writing tests but experience an error when trying to run manage.py test (this works for about 5 minutes on removing the old db before the exception is raised)

Creating test database for alias 'default'... Got an error creating
the test database: (1007, "Can't create database 'test_rmpublic';
database exists") Type 'yes' if you would like to try deleting the
test database 'test_rmpublic', or 'no' to cancel: yes Destroying old
test database for alias 'default'... Traceback (most recent call
last):   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/mysql/base.py",
line 110, in execute
    return self.cursor.execute(query, args)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
220, in execute
    self.errorhandler(self, exc, value)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/connections.py",
line 36, in defaulterrorhandler
    raise errorvalue   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
209, in execute
    r = self._query(query)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
371, in _query
    rowcount = self._do_query(q)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
335, in _do_query
    db.query(q)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/connections.py",
line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1072, "Key column 'establishment_id' doesn't exist in table")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   File "manage.py", line 10, in
<module>
    execute_from_command_line(sys.argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 367, in execute_from_command_line
    utility.execute()   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/test.py",
line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 294, in run_from_argv
    self.execute(*args, **cmd_options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 345, in execute
    output = self.handle(*args, **options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/test.py",
line 72, in handle
    failures = test_runner.run_tests(test_labels)   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
549, in run_tests
    old_config = self.setup_databases()   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
499, in setup_databases
    self.parallel, **kwargs   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
743, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/base/creation.py",
line 70, in create_test_db
    run_syncdb=True,   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 130, in call_command
    return command.execute(*args, **defaults)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 345, in execute
    output = self.handle(*args, **options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/migrate.py",
line 204, in handle
    fake_initial=fake_initial,   File "/projectpath/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)   File
"/projectpath/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)   File
"/projectpath/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 244, in apply_migration
    state = migration.apply(state, schema_editor)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/base/schema.py",
line 92, in __exit__
    self.execute(sql)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/base/schema.py",
line 112, in execute
    cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/utils.py", line
94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)   File "/projectpath/lib/python3.4/site-packages/django/utils/six.py", line
685, in reraise
    raise value.with_traceback(tb)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/mysql/base.py",
line 110, in execute
    return self.cursor.execute(query, args)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
220, in execute
    self.errorhandler(self, exc, value)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/connections.py",
line 36, in defaulterrorhandler
    raise errorvalue   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
209, in execute
    r = self._query(query)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
371, in _query
    rowcount = self._do_query(q)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
335, in _do_query
    db.query(q)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/connections.py",
line 280, in query
    _mysql.connection.query(self, query) django.db.utils.OperationalError: (1072, "Key column
'establishment_id' doesn't exist in table")

I looked through all the migration files and couldn't see any reference to establishment_id. Thinking that it might be a problem with the old test_db, I used the hacky but accepted answer described in this question to use sqlite3 as a test db instead and got a new error:

Traceback (most recent call last):   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py",
line 337, in execute
    return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such table:
rmcustomer_establishmentradius

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   File "manage.py", line 10, in
<module>
    execute_from_command_line(sys.argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 367, in execute_from_command_line
    utility.execute()   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/test.py",
line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 294, in run_from_argv
    self.execute(*args, **cmd_options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 345, in execute
    output = self.handle(*args, **options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/test.py",
line 72, in handle
    failures = test_runner.run_tests(test_labels)   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
549, in run_tests
    old_config = self.setup_databases()   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
499, in setup_databases
    self.parallel, **kwargs   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
743, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/base/creation.py",
line 78, in create_test_db
    self.connection._test_serialized_contents = self.serialize_db_to_string()   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/base/creation.py",
line 122, in serialize_db_to_string
    serializers.serialize("json", get_objects(), indent=None, stream=out)   File
"/projectpath/lib/python3.4/site-packages/django/core/serializers/__init__.py",
line 129, in serialize
    s.serialize(queryset, **options)   File "/projectpath/lib/python3.4/site-packages/django/core/serializers/base.py",
line 79, in serialize
    for count, obj in enumerate(queryset, start=1):   File "/projectpath/lib/python3.4/site-packages/django/db/backends/base/creation.py",
line 118, in get_objects
    for obj in queryset.iterator():   File "/projectpath/lib/python3.4/site-packages/django/db/models/query.py",
line 54, in __iter__
    results = compiler.execute_sql()   File "/projectpath/lib/python3.4/site-packages/django/db/models/sql/compiler.py",
line 835, in execute_sql
    cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/utils.py", line
94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)   File "/projectpath/lib/python3.4/site-packages/django/utils/six.py", line
685, in reraise
    raise value.with_traceback(tb)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py",
line 337, in execute
    return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table:
rmcustomer_establishmentradius

Apologies for the bloated stack traces. Any help would be great.

Aucun commentaire:

Enregistrer un commentaire