2

migrations/env.py:

f = open("/etc/config.json", "r")
json_config = json.load(f)
config = context.config
section = config.config_ini_section
print(f"password: {json_config['DB_PASSWORD']}, host: {json_config['DB_HOST']}, db: {json_config['DB_DATABASE']}")
config.set_section_option(section, "DB_USERNAME", json_config["DB_USERNAME"])
config.set_section_option(section, "DB_PASSWORD", urllib.parse.quote_plus(json_config["DB_PASSWORD"]))
config.set_section_option(section, "DB_HOST", json_config["DB_HOST"])
config.set_section_option(section, "DB_DATABASE", json_config["DB_DATABASE"])

alembic.ini:

sqlalchemy.url = postgresql://%(DB_USERNAME)s:%(DB_PASSWORD)s@%(DB_HOST)s/%(DB_DATABASE)s

And my password string has @ and $ in it. Running pipenv run alembic revision --autogenerate -m "Initial migration" bumps into the following error:

ValueError: invalid interpolation syntax in 'P%40%24%24w0rd' at position 1
Kok How Teh
  • 3,298
  • 6
  • 47
  • 85
  • Now that you have posted your password, you should change it (not that you should have ever used that one in the first place). You can make your new one not contain those special characters. iezei9ca4phobaQuohba is a nice password. – jjanes Sep 29 '22 at 14:14

1 Answers1

3

See Alembic.ini passwords also need percent signs doubled where the solution seems to be:

urllib.parse.quote_plus("P@$$w0rd").replace("%", "%%")

In order to escape the encoded %(as %%) so that when Alembic uses configparser to parse the file they are properly handled.

Adrian Klaver
  • 15,886
  • 2
  • 17
  • 28