-
Notifications
You must be signed in to change notification settings - Fork 56
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
[replit] The lesser of two evils #167
Conversation
582dafb
to
aad6505
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Hah! I started to write this very changeset. Is there any more testing to do here @lhchavez, or do you want to rebase and we can get this in and cut a minor release? |
aad6505
to
98d4927
Compare
98d4927
to
8ddb318
Compare
Currently the replit library has a very gross quirk: it has a global in `replit.database.default_db.db`, and the mere action of importing this library causes side effects to run! (connects to the database, starts a thread to refresh the URL, and prints a warning to stdout, adding insult to injury). So we're trading that very gross quirk with a gross workaround to preserve backwards compatibility: the modules that somehow end up importing that module now have a `__getattr__` that _lazily_ calls the code that used to be invoked as a side-effect of importing the library. Maybe in the future we'll deploy a breaking version of the library where we're not beholden to this backwards-compatibility quirck.
8ddb318
to
704c92a
Compare
I made some changes:
|
Providing accessors, to hint that we are accessing mutable state
7cc70b6
to
f5a6d0e
Compare
Continuation from #167 (comment), The more I looked at I reverted the surface area to what it was before, save with one alteration. Internally, we only ever use the Another issue was that of Finally, a complaint I've seen around is that the Python interpreter will hang if the database has been connected to, since we This was the summary of trying to get some thoughts out of my head before the weekend, I still need to revisit the tests, add missing documentation, and probably rename |
f5a6d0e
to
50583ec
Compare
@airportyh Dismissed your initial review since I'm taking a different approach from Luis' initial implementation. The changes here attempt to maximize compatibility with the existing API without adding too much to the surface area of the library. There's an additional improvement which was not reflected in the original implementation, being that when the |
An issue with LazyDB is that the refresh_db timer would not get canceled if the user closes the database. Additionally, the db_url refresh logic relies on injection, whereas the Database should ideally be the thing requesting that information from the environment
50583ec
to
e4d5a02
Compare
@@ -4,7 +4,7 @@ | |||
|
|||
from flask import Blueprint, Flask, request | |||
|
|||
from .default_db import db | |||
from . import default_db |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it even possible to use a db other than the default_db?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! As an example, we use it in some CI tests internally. An example is as follows (I added a print
in the default DB initializer):
>>> import os
>>> from replit.database import Database
>>> db = Database(os.environ.get("REPLIT_DB_URL"))
>>> db.set('foo', 'bar')
>>> db.get('foo')
'bar'
>>> import replit
>>> replit.db
Initializing default db!
<Database(db_url=...)>
You can see in this example that the initializer for the default database is only triggered on certain property accessors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the cleanup!
Why
Currently the replit library has a very gross quirk: it has a global in
replit.database.default_db.db
, and the mere action of importing this library causes side effects to run! (connects to the database, starts a thread to refresh the URL, and prints a warning to stdout, adding insult to injury).What changed
So we're trading that very gross quirk with a gross workaround to preserve backwards compatibility: the modules that somehow end up importing that module now have a
__getattr__
that lazily calls the code that used to be invoked as a side-effect of importing the library. Maybe in the future we'll deploy a breaking version of the library where we're not beholden to this backwards-compatibility quirck.Test plan
Rollout