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

SQLite apply_driver_hacks breaking URI usage #1045

Closed
henryiii opened this issue Mar 26, 2022 · 1 comment · Fixed by #1097
Closed

SQLite apply_driver_hacks breaking URI usage #1045

henryiii opened this issue Mar 26, 2022 · 1 comment · Fixed by #1097
Milestone

Comments

@henryiii
Copy link

I noticed that flask-sqlalchmy is injecting (the wrong) directory when run, it puts the file directory in instead of the current working directory, causing me to not get a valid database. This means, by the way, that assert file.exists() passes but then a nonexistent file is still opened. I finally figured that out, but in doing so, I decided I wanted to use a URI, as described here: https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#uri-connections - and the injection system completely ruins URI's, by adding a path before the file:.

app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///file:db.sqlite3?mode=ro&uri=true"

produces

/absolute/file/path/to/source/folder/file:db.sqlite3?mode=ro&uri=true"

Which does create and open a file with that name (file:db.sqlite3?mode=ro&uri=true). If it had been absolute, then it would have both paths (/absolute/file/path/to/source/folder/file:/absolute/file/path/to/binary/folder/db.sqlite3?mode=ro&uri=true), which at least won't open because it's a non-existent folder.

This should either only act after the the file: prefix when making this absolute, or just leave it alone when it's a file: and not make it absolute, leaving that to the user. Given that SLQLite happy opens files that don't exist, it is really irritating to mess with this path IMO, but maybe there's a good reason (which is why I don't know which of the two options is the right one to take).

This code is in apply_driver_hacks.

Environment:

  • Python version: 3.10
  • Flask-SQLAlchemy version: 2.5.1
  • SQLAlchemy version: 1.4.32
@davidism davidism added this to the 3.0 milestone Apr 4, 2022
@davidism
Copy link
Member

davidism commented Apr 4, 2022

I didn't realize file: was a thing. Makes sense to detect that as well and modify it.

Because the current working directory is not always obvious during deployment, Flask-SQLAlchemy makes relative SQLite paths relative to the project root (will change to the instance folder in 3.x). This way, the database ends up in a consistent place, either absolute to begin with or always relative to the project.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants