Skip to content

Commit 47fe7e7

Browse files
committed
fix: stop sqlite db from being updated while running tests
1 parent c33d020 commit 47fe7e7

File tree

1 file changed

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

1 file changed

+14
-5
lines changed

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

Lines changed: 14 additions & 5 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
5+
from sqlalchemy.orm import DeclarativeBase, sessionmaker
66

77
logger = structlog.getLogger(__name__)
88

@@ -37,10 +37,19 @@ def make_tables(engine: Engine) -> None:
3737
# tag IDs < 1000 are reserved
3838
# create tag and delete it to bump the autoincrement sequence
3939
# TODO - find a better way
40-
with engine.connect() as conn:
41-
conn.execute(text("INSERT INTO tags (id, name, color) VALUES (999, 'temp', 1)"))
42-
conn.execute(text("DELETE FROM tags WHERE id = 999"))
43-
conn.commit()
40+
# 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:
50+
conn.execute(text("INSERT INTO tags (id, name, color) VALUES (999, 'temp', 1)"))
51+
conn.execute(text("DELETE FROM tags WHERE id = 999"))
52+
conn.commit()
4453

4554

4655
def drop_tables(engine: Engine) -> None:

0 commit comments

Comments
 (0)