Skip to content

Commit

Permalink
feat: click prompt to delete ydb (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Jan 7, 2024
1 parent b9f10e2 commit 67f9316
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
29 changes: 29 additions & 0 deletions y/_db/__init__.py
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)
7 changes: 5 additions & 2 deletions y/_db/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import errno
from os import mkdir, path

SQLITE_DIR = f"{path.expanduser( '~' )}/.ypricemagic"
SQLITE_PATH = f"{SQLITE_DIR}/ypricemagic.sqlite"

if ENVS.DB_PROVIDER == "sqlite":
try:
mkdir(f"{path.expanduser( '~' )}/.ypricemagic")
mkdir(SQLITE_DIR)
except OSError as e:
if e.errno != errno.EEXIST:
raise

connection_settings = {
'provider': str(ENVS.DB_PROVIDER),
'filename': f"{path.expanduser( '~' )}/.ypricemagic/ypricemagic.sqlite",
'filename': SQLITE_PATH,
'create_db': True,
}
else:
Expand Down
27 changes: 27 additions & 0 deletions y/_db/exceptions.py
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"
15 changes: 3 additions & 12 deletions y/_db/utils/__init__.py
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'

0 comments on commit 67f9316

Please sign in to comment.