-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1aeeb18
[replit] The lesser of two evils
lhchavez a34c321
Marking internal properties as private
blast-hardcheese e628478
Reintroduce refresh_db noop to avoid errors on upgrade
blast-hardcheese 5be6253
Reflow LazyDB back down into default_db module
blast-hardcheese e4d5a02
Removing stale main.sh
blast-hardcheese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,58 @@ | ||
"""A module containing the default database.""" | ||
from os import environ, path | ||
import threading | ||
from typing import Optional | ||
|
||
import os | ||
import os.path | ||
from typing import Any, Optional | ||
|
||
from .database import Database | ||
|
||
|
||
def get_db_url() -> str: | ||
def get_db_url() -> Optional[str]: | ||
"""Fetches the most up-to-date db url from the Repl environment.""" | ||
# todo look into the security warning ignored below | ||
tmpdir = "/tmp/replitdb" # noqa: S108 | ||
if path.exists(tmpdir): | ||
if os.path.exists(tmpdir): | ||
with open(tmpdir, "r") as file: | ||
db_url = file.read() | ||
else: | ||
db_url = environ.get("REPLIT_DB_URL") | ||
return file.read() | ||
|
||
return db_url | ||
return os.environ.get("REPLIT_DB_URL") | ||
|
||
|
||
def refresh_db() -> None: | ||
"""Refresh the DB URL every hour.""" | ||
global db | ||
"""Deprecated: refresh_db is now the responsibility of the Database instance.""" | ||
pass | ||
|
||
|
||
def _unbind() -> None: | ||
global _db | ||
_db = None | ||
|
||
|
||
def _get_db() -> Optional[Database]: | ||
global _db | ||
if _db is not None: | ||
return _db | ||
|
||
db_url = get_db_url() | ||
db.update_db_url(db_url) | ||
threading.Timer(3600, refresh_db).start() | ||
|
||
if db_url: | ||
_db = Database(db_url, get_db_url=get_db_url, unbind=_unbind) | ||
else: | ||
# The user will see errors if they try to use the database. | ||
print("Warning: error initializing database. Replit DB is not configured.") | ||
_db = None | ||
return _db | ||
|
||
|
||
_db: Optional[Database] = None | ||
|
||
db: Optional[Database] | ||
db_url = get_db_url() | ||
if db_url: | ||
db = Database(db_url) | ||
else: | ||
# The user will see errors if they try to use the database. | ||
print("Warning: error initializing database. Replit DB is not configured.") | ||
db = None | ||
|
||
if db: | ||
refresh_db() | ||
# Previous versions of this library would just have side-effects and always set | ||
# up a database unconditionally. That is very undesirable, so instead of doing | ||
# that, we are using this egregious hack to get the database / database URL | ||
# lazily. | ||
def __getattr__(name: str) -> Any: | ||
if name == "db": | ||
return _get_db() | ||
if name == "db_url": | ||
return get_db_url() | ||
raise AttributeError(name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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):You can see in this example that the initializer for the default database is only triggered on certain property accessors.