-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: click prompt to delete ydb (#476)
- Loading branch information
1 parent
b9f10e2
commit 67f9316
Showing
4 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
import os | ||
|
||
from pony.orm import Database, BindingError, OperationalError, TransactionError | ||
|
||
from y._db.config import SQLITE_PATH | ||
|
||
|
||
def bind_db(db: Database, **connection_settings) -> None: | ||
try: | ||
db.bind(**connection_settings) | ||
except BindingError as e: | ||
if not str(e).startswith('Database object was already bound to'): | ||
raise e | ||
|
||
def generate_mapping(db: Database) -> None: | ||
try: | ||
db.generate_mapping(create_tables=True) | ||
except OperationalError as e: | ||
if "no such column: " in str(e): | ||
from y._db.exceptions import NewDatabaseSchemaError | ||
raise NewDatabaseSchemaError from e | ||
raise e | ||
except TransactionError as e: | ||
if str(e) != "@db_session-decorated create_tables() function with `ddl` option cannot be called inside of another db_session": | ||
raise e | ||
|
||
def delete_sqlite() -> None: | ||
os.remove(SQLITE_PATH) |
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,3 +1,30 @@ | ||
|
||
import click | ||
|
||
from y import ENVIRONMENT_VARIABLES as ENVS | ||
from y._db import SQLITE_PATH, delete_sqlite | ||
from y._db.config import connection_settings | ||
|
||
class CacheNotPopulatedError(Exception): | ||
pass | ||
|
||
|
||
class yDBError(Exception): | ||
provider = ENVS.DB_PROVIDER | ||
location = SQLITE_PATH if provider == "sqlite" else connection_settings | ||
|
||
class NewDatabaseSchemaError(yDBError): | ||
msg = f"\n\nA more performant ydb schema has been pooshed to prod, you must delete your ypricemagic database at {yDBError.location}" | ||
def __init__(self) -> None: | ||
if self.provider == "sqlite" and self._user_approves_sqlite_deletion(): | ||
delete_sqlite() | ||
super().__init__(self.msg) | ||
|
||
def _user_approves_sqlite_deletion(self) -> bool: | ||
return click.prompt( | ||
self.msg + "\n\nWould you like to delete your db now?", | ||
default="no", | ||
type=click.Choice(["yes", "no"], case_sensitive=False), | ||
confirmation_prompt=True, | ||
show_default=False, | ||
) == "yes" |
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,19 +1,10 @@ | ||
|
||
from pony.orm import BindingError, TransactionError | ||
|
||
from y._db import bind_db, generate_mapping | ||
from y._db.config import connection_settings | ||
from y._db.entities import db | ||
from y._db.utils.utils import get_chain, get_block | ||
|
||
|
||
try: | ||
db.bind(**connection_settings) | ||
db.generate_mapping(create_tables=True) | ||
except TransactionError as e: | ||
if str(e) != "@db_session-decorated create_tables() function with `ddl` option cannot be called inside of another db_session": | ||
raise e | ||
except BindingError as e: | ||
if not str(e).startswith('Database object was already bound to'): | ||
raise e | ||
bind_db(db, **connection_settings) | ||
generate_mapping(db) | ||
|
||
__all__ = 'get_chain', 'get_block' |