Skip to content

Commit c3a7829

Browse files
committed
fix: automatic models
1 parent 0a0ab4b commit c3a7829

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

alembic/env.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import os
33
import sys
44
from logging.config import fileConfig
5+
import importlib
6+
from pathlib import Path
57

68
# Add the project root directory to the Python path
79
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
@@ -12,10 +14,18 @@
1214

1315
from alembic import context
1416
from api.core.config import settings
15-
16-
# Import your models here
1717
from api.core.database import Base
1818

19+
# Automatically import all models
20+
src_path = Path(__file__).parent.parent / "api" / "src"
21+
for path in src_path.rglob("*.py"):
22+
if path.name != "__init__.py":
23+
module_path = str(path.relative_to(Path(__file__).parent.parent)).replace(os.sep, ".")[:-3]
24+
try:
25+
importlib.import_module(module_path)
26+
except Exception as e:
27+
print(f"Failed to import {module_path}: {e}")
28+
1929
# this is the Alembic Config object
2030
config = context.config
2131

0 commit comments

Comments
 (0)