In Flask-migrate ValueError: Invalid interpolation syntax in connection string at position 15

I use flask migrateto create and migrate a database in a flask with -sqlalchemy flask.

Everything worked fine until I changed the password of the database user that contains "@", after which it stops working, I updated my code based on writing the connection string when the password contains special characters

It works for the application, but not for flask-migration, its error display during migration

ie on python manage.py db migrate

ValueError: invalid interpolation syntax in u'mysql://user:p%40ssword@localhost/testdb' at position 15

Here is the password p@sswordand it is escaped urlquote(see the link above the question).

Full stack of errors:

Traceback (most recent call last):
  File "manage.py", line 20, in <module>
    manager.run()
  File "/usr/local/lib/python2.7/dist-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/usr/local/lib/python2.7/dist-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/usr/local/lib/python2.7/dist-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/flask_migrate/__init__.py", line 177, in migrate
    version_path=version_path, rev_id=rev_id)
  File "/usr/local/lib/python2.7/dist-packages/alembic/command.py", line 117, in revision
    script_directory.run_env()
  File "/usr/local/lib/python2.7/dist-packages/alembic/script/base.py", line 407, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/usr/local/lib/python2.7/dist-packages/alembic/util/pyfiles.py", line 93, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python2.7/dist-packages/alembic/util/compat.py", line 79, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "migrations/env.py", line 22, in <module>
    current_app.config.get('SQLALCHEMY_DATABASE_URI'))
  File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 218, in set_main_option
    self.set_section_option(self.config_ini_section, name, value)
  File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 245, in set_section_option
    self.file_config.set(section, name, value)
  File "/usr/lib/python2.7/ConfigParser.py", line 752, in set
    "position %d" % (value, tmp_value.find('%')))
ValueError: invalid interpolation syntax in u'mysql://user:p%40ssword@localhost/testdb' at position 15

Please, help

+1
source share
3

migrations/env.py , .

config.set_main_option('sqlalchemy.url',
                       current_app.config.get('SQLALCHEMY_DATABASE_URI'))

SQLALCHEMY_DATABASE_URI %, .

, migrations/env.py

db_url_escaped = current_app.config.get('SQLALCHEMY_DATABASE_URI').replace('%', '%%')
config.set_main_option('sqlalchemy.url', db_url_escaped)

. set_main_option:

, ConfigParser.set, pyformat (,% (some_value) s). , , , . %%. .

+4

.

"%" ( ) URI- db , urlencode .

( "%%" ), . - .

, , - "%" db. , . "alembic" github . , RawConfigParser .

-1

You can look at http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#mysql-unicode I had the same problem with my password and mysql connector. using the mysql + pymysql connector allowed me to connect in the application and in the migration scripts.

-1
source

Source: https://habr.com/ru/post/1687474/


All Articles