Skip to content

Commit 75dbace

Browse files
committed
refactor: small refactor in db checking code
1 parent 47fe7e7 commit 75dbace

File tree

1 file changed

+5
-10
lines changed
  • tagstudio/src/core/library/alchemy

1 file changed

+5
-10
lines changed

tagstudio/src/core/library/alchemy/db.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import structlog
44
from sqlalchemy import Dialect, Engine, String, TypeDecorator, create_engine, text
5-
from sqlalchemy.orm import DeclarativeBase, sessionmaker
5+
from sqlalchemy.orm import DeclarativeBase
66

77
logger = structlog.getLogger(__name__)
88

@@ -38,15 +38,10 @@ def make_tables(engine: Engine) -> None:
3838
# create tag and delete it to bump the autoincrement sequence
3939
# TODO - find a better way
4040
# is this the better way?
41-
Session = sessionmaker(bind=engine) # noqa: N806
42-
session = Session()
43-
result = session.execute(text("SELECT SEQ FROM sqlite_sequence WHERE name='tags'"))
44-
autoincrement_val = result.fetchone()
45-
if autoincrement_val:
46-
# fetchone returns a tuple even when there is only one value
47-
autoincrement_val = autoincrement_val[0]
48-
if not autoincrement_val or autoincrement_val < 1000:
49-
with engine.connect() as conn:
41+
with engine.connect() as conn:
42+
result = conn.execute(text("SELECT SEQ FROM sqlite_sequence WHERE name='tags'"))
43+
autoincrement_val = result.scalar()
44+
if not autoincrement_val or autoincrement_val < 1000:
5045
conn.execute(text("INSERT INTO tags (id, name, color) VALUES (999, 'temp', 1)"))
5146
conn.execute(text("DELETE FROM tags WHERE id = 999"))
5247
conn.commit()

0 commit comments

Comments
 (0)