Support sqlite database urls relative to project path.
See original GitHub issueWhen developing it makes sense to have the database file in the local repository directory, to have a dev.env
which is valid for all developers it needs to be relative…
I have been doing hacks in settings.py to get around it for a few years now but I would like a discussion about how to maybe integrate such a feature into django-environ.
This basically just assumes that if the the file is at the root of the system it’s instead relative but I’m looking for a way to get rid of these in every project…
for db in DATABASES:
if DATABASES[db]['ENGINE'] == 'django.db.backends.sqlite3':
if os.path.split(DATABASES[db]['NAME'])[0] == "/":
DATABASES[db]['NAME'] = root(DATABASES[db]['NAME'][1:])
I am thinking that this is kind of sane because no one proably stores the database in the root of the file system but making it optional by having a sqlite_root
argument to the db-url function maybe works?
root = environ.Path(__file__) - 2
...
DATABASES = {
'default': env.db(sqlite_root=root()),
}
I think that both relative AND absolute paths should work.
One option which I think might work safely for triggering a relative path instead of absolute is to do it when host is .
or some other reserved name which isn’t really a hostname. It should error out if sqlite_root
was not provided to the url parse function.
DATABASE_URL=sqlite://./db.sqlite3
Please share your opinions on this…
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top GitHub Comments
Hi @tomasf The file url syntax for dbs allows your to put you db everywhere in your filesystem. Relative or absolute too. See the tips for sqlite in Readme. If I correctly understand what do you mean, you need to have
dev.env
with relative path for db… so you can usesqlite:///db.sqlite
for a db in the same dir of .env file andsqlite://../db.sqlite
for relative one andsqlite:////home/user/db.sqlite
for absolute one. Is it right for you?I’d like django-environ remain simple as long as possible. I’d not complicate its codebase without an urgent and obvious need. To all those who want to work with relative paths, I would recommend some kind of approach like this: