Skip to content

Commit 3885732

Browse files
committed
fix: alembic upgrade head path is now independent of working dir
1 parent a882b8c commit 3885732

File tree

1 file changed

+14
-51
lines changed
  • libs/tracker/llmstudio_tracker/db/migrations

1 file changed

+14
-51
lines changed
Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,24 @@
1+
import logging
12
import subprocess
3+
from pathlib import Path
24

5+
from alembic import command
6+
from alembic.config import Config
37

4-
def run_alembic_upgrade():
5-
try:
6-
if _alembic_is_up_to_date():
7-
print("Alembic: DB is already up-to-date.")
8-
return
9-
10-
print("Alembic: DB is not up-to-date. Running Alembic upgrade...")
11-
subprocess.run(["poetry", "run", "alembic", "upgrade", "head"], check=True)
12-
print("Alembic: Upgrade successful.")
13-
except subprocess.CalledProcessError as e:
14-
print(f"Alembic: Upgrade failed: {e}")
15-
raise
8+
logging.basicConfig(level=logging.INFO)
169

1710

18-
def _alembic_is_up_to_date() -> bool:
19-
"""Returns True if the DB is already at the latest revision."""
11+
def run_alembic_upgrade():
2012
try:
21-
current = (
22-
subprocess.check_output(
23-
["poetry", "run", "alembic", "current"], stderr=subprocess.DEVNULL
24-
)
25-
.decode()
26-
.strip()
27-
)
28-
29-
head = (
30-
subprocess.check_output(
31-
["poetry", "run", "alembic", "heads"], stderr=subprocess.DEVNULL
32-
)
33-
.decode()
34-
.strip()
35-
)
13+
logging.info("Running LLMstudio Tracker Migrations Alembic upgrade...")
3614

37-
current_rev_line = next(
38-
(
39-
line
40-
for line in current.splitlines()
41-
if line.strip() and not line.startswith("DB URL")
42-
),
43-
"",
44-
)
45-
head_rev_line = next(
46-
(
47-
line
48-
for line in head.splitlines()
49-
if line.strip() and not line.startswith("DB URL")
50-
),
51-
"",
52-
)
15+
alembic_cfg = Config()
16+
this_dir = Path(__file__).resolve().parent
17+
alembic_cfg.set_main_option("script_location", str(this_dir))
5318

54-
current_rev = current_rev_line.split(" ")[0]
55-
head_rev = head_rev_line.split(" ")[0]
56-
57-
return current_rev == head_rev
19+
command.upgrade(alembic_cfg, "head")
5820

21+
logging.info("Alembic: LLMstudio Tracker Migrations upgrade successful.")
5922
except subprocess.CalledProcessError as e:
60-
print("Alembic: Check if up-to-date failed. Upgrading head to be safe.")
61-
return False
23+
logging.error(f"Alembic: LLMstudio Tracker Migrations upgrade failed: {e}")
24+
raise

0 commit comments

Comments
 (0)