Skip to content

Commit

Permalink
Refactor and handling database migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
pauliyobo committed Sep 30, 2024
1 parent 3890ddc commit 801cd6d
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 142 deletions.
2 changes: 1 addition & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = driver://user:pass@localhost/dbname
sqlalchemy.url = sqlite:///.appdata/database/database2.sqlite


[post_write_hooks]
Expand Down
7 changes: 3 additions & 4 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from alembic.autogenerate import rewriter
from alembic.operations import ops

from bookworm.database import Base, get_db_url
from bookworm.annotation.annotation_models import *
from bookworm.database import *



Expand Down Expand Up @@ -54,7 +53,7 @@ def run_migrations_offline() -> None:
script output.
"""
url = get_db_url()
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
Expand All @@ -73,7 +72,7 @@ def run_migrations_online() -> None:
and associate a connection with the context.
"""
connectable = create_engine(get_db_url())
connectable = create_engine(config.get_main_option("sqlalchemy.url"))

with connectable.connect() as connection:
context.configure(
Expand Down
30 changes: 30 additions & 0 deletions alembic/versions/85028f013e6d_zero_migration_revision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Zero Migration revision
Revision ID: 85028f013e6d
Revises: 28099038d8d6
Create Date: 2024-09-30 14:48:49.349114
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import bookworm

# revision identifiers, used by Alembic.
revision: str = '85028f013e6d'
down_revision: Union[str, None] = '28099038d8d6'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion bookworm/annotation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import wx

from bookworm import config, speech
from bookworm.database.models import Note, Quote
from bookworm.logger import logger
from bookworm.resources import sounds
from bookworm.service import BookwormService
Expand All @@ -20,7 +21,6 @@
AnnotationSettingsPanel,
AnnotationsMenuIds,
)
from .annotation_models import Note, Quote
from .annotator import Bookmarker, NoteTaker, Quoter

log = logger.getChild(__name__)
Expand Down
121 changes: 0 additions & 121 deletions bookworm/annotation/annotation_models.py

This file was deleted.

4 changes: 1 addition & 3 deletions bookworm/annotation/annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import sqlalchemy as sa

from bookworm import config
from bookworm.database.models import Book
from bookworm.database.models import Book, Bookmark, Note, Quote
from bookworm.logger import logger

from .annotation_models import Bookmark, Note, Quote

log = logger.getChild(__name__)
# The bakery caches query objects to avoid recompiling them into strings in every call

Expand Down
36 changes: 25 additions & 11 deletions bookworm/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,38 @@

from alembic import command
from alembic.config import Config
from sqlalchemy import create_engine
from alembic.migration import MigrationContext
from sqlalchemy import create_engine, text
from sqlalchemy.orm import scoped_session, sessionmaker

from bookworm import app
from bookworm.logger import logger
from bookworm import paths
from bookworm.paths import db_path as get_db_path

from .models import (
Book,
Base,
DocumentPositionInfo,
GetOrCreateMixin,
PinnedDocument,
RecentDocument,
)
from .models import *

log = logger.getChild(__name__)

def get_db_url() -> str:
db_path = os.path.join(get_db_path(), "database.sqlite")
return f"sqlite:///{db_path}"

def init_database():
engine = create_engine(get_db_url())
def init_database(engine = None, url: str = None) -> bool:
if not url:
url = get_db_url()
if not engine:
engine = create_engine(get_db_url())
log.debug(f"Using url {url} ")
with engine.connect() as conn:
context = MigrationContext.configure(conn)
rev = context.get_current_revision()
# let's check for the book table
# Should it be too ambiguous, we'd have to revisit what tables should be checked to determine whether the DB is at the baseline point
cursor = conn.execute(text("SELECT name FROM sqlite_master WHERE type='table';"))
tables = [row[0] for row in cursor.fetchall()]
is_baseline = tables != None and "book" in tables and rev == None
log.info(f"Current revision is {rev}")
log.info("Running database migrations and setup")
cfg_file = ""
script_location = "alembic"
Expand All @@ -44,8 +51,15 @@ def init_database():

cfg = Config(Path(cfg_file, "alembic.ini"))
cfg.set_main_option('script_location', str(script_location))
cfg.set_main_option("sqlalchemy.url", url)
if rev == None:
if is_baseline:
log.info("No revision was found, but the database appears to be at the baseline required to begin tracking.")
log.info("Stamping alembic revision")
command.stamp(cfg, "28099038d8d6")
command.upgrade(cfg, "head")
Base.session = scoped_session(
sessionmaker(engine, autocommit=False, autoflush=False)
)
return True

Loading

0 comments on commit 801cd6d

Please sign in to comment.