Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with MariaDB #3

Closed
dismine opened this issue Dec 7, 2019 · 10 comments
Closed

Doesn't work with MariaDB #3

dismine opened this issue Dec 7, 2019 · 10 comments

Comments

@dismine
Copy link

dismine commented Dec 7, 2019

Hi.

I must thank you for this amazing project! But it seems doesn't work with MariaDB. I tried with several versions: 5.5, 10.1, 10.3 and 10.4. Always the same result.

 Traceback (most recent call last):
   File "manage.py", line 23, in <module>
     execute_from_command_line(sys.argv)
   File "/opt/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
     utility.execute()
   File "/opt/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/opt/env/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/opt/env/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
     output = self.handle(*args, **options)
   File "/code/apps/test/management/commands/wait_for_database.py", line 91, in handle
     wait_for_database(**options)
   File "/code/apps/test/management/commands/wait_for_database.py", line 31, in wait_for_database
     connection.cursor().execute('SELECT')
   File "/opt/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
   File "/opt/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/opt/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "/opt/env/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/opt/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 82, in _execute
     return self.cursor.execute(sql)
   File "/opt/env/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
     return self.cursor.execute(query, args)
   File "/opt/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 209, in execute
     res = self._query(query)
   File "/opt/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 315, in _query
     db.query(q)
   File "/opt/env/lib/python3.7/site-packages/MySQLdb/connections.py", line 231, in query
     _mysql.connection.query(self, query)
 django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1")

Note. I have moved your command inside my project to be able to fix it. So, don't look at the path. Code is absolutely the same.

It seems like connection.cursor().execute('SELECT') doesn't wotk for MariaDB. But connection.cursor().execute('SELECT 1') does. What do you think?

@bittner
Copy link
Member

bittner commented Dec 9, 2019

Thank you for your feedback, and thanks for using django-probes in the first place! 🥇

Would you want to try placing a pull request for the change?

I would suggest to start with extending the test setup in a way that it covers more databases. Currently, only Postgres is tested against, which is apparently not enough. 😟

@dismine
Copy link
Author

dismine commented Dec 9, 2019

Hi.

Would you want to try placing a pull request for the change?

How do you propose to add a database-specific code?

@bittner
Copy link
Member

bittner commented Dec 9, 2019

The idea of this package is to be database independent. Ideally, we only add tests that prove that currently the code only works for Postgres and fails for MariaDB. Then we adapt the code to make it work for both database engines. Same for other engines, in future.

  • To run tests against specific database engines we need to override the DATABASE settings using what pytest-django provides. This will allow us to write a simple test that you run once against Postgres and once again against MariaDB.
  • You will need to add the mysqlclient package to the unit test dependencies in tox.ini.

Do you think you can do this?

@dismine
Copy link
Author

dismine commented Dec 9, 2019

We could try, it would be interesting. But I ask about a different thing. How to run a raw SQL depend on backend?

@bittner
Copy link
Member

bittner commented Dec 10, 2019

How to run a raw SQL depend on backend?

You make this dependent on your Django settings. Set the appropriate backend in your settings, then the SQL query will run against that specific DBMS.

@dismine
Copy link
Author

dismine commented Dec 10, 2019

No, i know this.

I am talking about this piece of code:

connection.cursor().execute('SELECT')

How to say?

if "MariaDB":
    connection.cursor().execute('SELECT 1')
elif "Postgres":
    connection.cursor().execute('SELECT')
else:
    raise Exception("Not supported backend.) #  Just as an example

@dismine
Copy link
Author

dismine commented Dec 10, 2019

I just realized that I could try to read settings and compare.

>>> from django.conf import settings
>>> settings.DATABASES['default']['ENGINE']
'django.db.backends.sqlite3' or 'django.db.backends.postgresql_psycopg2'

But quick search showed another way:

>>> from django.db import connection
>>> connection.vendor
'postgresql' or 'sqlite'

@dismine
Copy link
Author

dismine commented Dec 10, 2019

On StackOverflow, I have found a list with all ping queries. We could try to use it for extending a list of supported backends.

@bittner
Copy link
Member

bittner commented Dec 11, 2019

Wow, this is very helpful, cool! 🥇

So, we can use the appropriate ping query for the chosen database engine. That is one reason more to add additional tests for the different supported database engines. 👍

Side note:

What is also not yet supported is multiple databases. I have created a separate ticket (#4) for that.

jor-rit added a commit to jor-rit/django-probes that referenced this issue Mar 4, 2020
Use 'SELECT 1', it's the more compatible. This works on most commonly
used databases, like postgreSQL, mysql, mariadb, mssql (and probably
others). But does _not_ work on, for example, oracle (and probably
others).

This is a, minimal impact, 'quick fix', better solution would be to go
through Django's database backend 'is_usable' method.

Fixes issue painless-software#3
bittner pushed a commit that referenced this issue Mar 4, 2020
Use 'SELECT 1', it's the more compatible. This works on most commonly
used databases, like postgreSQL, mysql, mariadb, mssql (and probably
others). But does _not_ work on, for example, oracle (and probably
others).

This is a, minimal impact, 'quick fix', better solution would be to go
through Django's database backend 'is_usable' method.

Fixes issue #3
@bittner
Copy link
Member

bittner commented Mar 9, 2020

This should work now with version 1.3.0 released last week. 🚀

Thanks for your valuable input! 🥇

@bittner bittner closed this as completed Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants