|
| 1 | +import logging |
1 | 2 | import subprocess |
| 3 | +from pathlib import Path |
2 | 4 |
|
| 5 | +from alembic import command |
| 6 | +from alembic.config import Config |
3 | 7 |
|
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) |
16 | 9 |
|
17 | 10 |
|
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(): |
20 | 12 | 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...") |
36 | 14 |
|
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)) |
53 | 18 |
|
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") |
58 | 20 |
|
| 21 | + logging.info("Alembic: LLMstudio Tracker Migrations upgrade successful.") |
59 | 22 | 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