Skip to content

Commit b11eb3a

Browse files
committed
refactor(migration): improve error handling and type safety in migration module
- Replace generic error decorator with typed migration-specific error handling - Add proper type hints and validation for migration operations - Rename database dependency key from "database" to "db" for consistency - Remove redundant db_migration option from CLI - Improve error messages and logging for migration operations
1 parent e0586b0 commit b11eb3a

File tree

4 files changed

+268
-151
lines changed

4 files changed

+268
-151
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ dependencies = [
3535

3636
[project.optional-dependencies]
3737
"templating" = ["jinja2 == 3.0.1"]
38-
"database" = ["alembic >= 1.16.3"]
38+
"db" = ["alembic >= 1.16.3"]
3939

4040
[project.urls]
4141
Documentation = "https://robyn.tech/"
@@ -86,7 +86,7 @@ orjson = "^3.9.15"
8686

8787
[tool.poetry.extras]
8888
templating = ["jinja2"]
89-
database = ["alembic"]
89+
db = ["alembic"]
9090

9191
[tool.poetry.group.dev]
9292
optional = true

robyn/cli.py

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
import sys
55
import webbrowser
66
import argparse
7+
import importlib.util
78
from pathlib import Path
89
from typing import Optional
10+
from pip._internal.cli.main import main as pip_main
911

1012
from InquirerPy.base.control import Choice
1113
from InquirerPy.resolver import prompt
1214

1315
from robyn.env_populator import load_vars
1416
from robyn.robyn import get_version
17+
from robyn.migrate import configure_parser, execute_command
1518

1619
from .argument_parser import Config
1720
from .reloader import create_rust_file, setup_reloader
@@ -52,21 +55,10 @@ def create_robyn_app():
5255
"default": Choice("no-db", name="No DB"),
5356
"name": "project_type",
5457
},
55-
{
56-
"type": "list",
57-
"message": "Need Database Migration? (Y/N)",
58-
"choices": [
59-
Choice("Y", name="Y"),
60-
Choice("N", name="N"),
61-
],
62-
"default": Choice("N", name="N"),
63-
"name": "db_migration",
64-
},
6558
]
6659
result = prompt(questions=questions)
6760
project_dir_path = Path(str(result["directory"])).resolve()
6861
docker = result["docker"]
69-
db_migration = result["db_migration"]
7062
project_type = str(result["project_type"])
7163

7264
final_project_dir_path = (CURRENT_WORKING_DIR / project_dir_path).resolve()
@@ -83,34 +75,6 @@ def create_robyn_app():
8375
if docker == "N":
8476
os.remove(f"{final_project_dir_path}/Dockerfile")
8577

86-
# If database migration is needed, install alembic
87-
if db_migration == "Y":
88-
print("Installing alembic...")
89-
try:
90-
# Check if alembic is already installed
91-
import importlib.util
92-
alembic_spec = importlib.util.find_spec('alembic')
93-
94-
if alembic_spec is None:
95-
# Install alembic using pip API
96-
try:
97-
import pip
98-
print("Installing alembic using pip API...")
99-
from pip._internal.cli.main import main as pip_main
100-
pip_main(['install', 'alembic', '--quiet'])
101-
print("Successfully installed alembic.")
102-
except ImportError:
103-
# If pip API is not available, use subprocess
104-
print("Installing alembic using subprocess...")
105-
subprocess.run([sys.executable, "-m", "pip", "install", "alembic", "-q"], check=True,
106-
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
107-
print("Successfully installed alembic.")
108-
else:
109-
print("Alembic is already installed.")
110-
except (subprocess.CalledProcessError, ImportError) as e:
111-
print(f"Failed to install alembic: {str(e)}")
112-
print("Please install it manually using 'pip install alembic'.")
113-
11478
print(f"New Robyn project created in '{final_project_dir_path}' ")
11579

11680

@@ -143,7 +107,6 @@ def start_app_normally(config: Config):
143107

144108
def handle_db_command():
145109
"""Handle database migration commands."""
146-
import importlib.util
147110
alembic_spec = importlib.util.find_spec("alembic")
148111

149112
if alembic_spec is None:
@@ -153,7 +116,6 @@ def handle_db_command():
153116
if install_choice == 'y':
154117
try:
155118
try:
156-
from pip._internal.cli.main import main as pip_main
157119
print("Installing alembic...")
158120
pip_main(['install', 'alembic', '--quiet'])
159121
print("Successfully installed alembic.")
@@ -175,13 +137,6 @@ def handle_db_command():
175137
print("Please install alembic manually using 'pip install alembic' before using database commands.")
176138
sys.exit(1)
177139

178-
try:
179-
from robyn.migrate import configure_parser, execute_command
180-
except ImportError as e:
181-
print(f"ERROR: Failed to import migrate module: {str(e)}")
182-
print("This might be due to an incomplete installation or a version mismatch.")
183-
print("Try reinstalling Robyn or updating your dependencies.")
184-
sys.exit(1)
185140
parser = argparse.ArgumentParser(
186141
usage=argparse.SUPPRESS, # omit usage hint
187142
description='Robyn database migration commands.'

0 commit comments

Comments
 (0)