Skip to content

Commit f241d70

Browse files
authored
fix checking for migrations in granian (#5348)
1 parent 24cf764 commit f241d70

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

reflex/reflex.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ def _run(
201201
# Get the app module.
202202
app_task = prerequisites.compile_or_validate_app
203203
args = (frontend,)
204+
kwargs = {
205+
"check_if_schema_up_to_date": True,
206+
}
204207

205208
# Granian fails if the app is already imported.
206209
if should_use_granian():
@@ -209,16 +212,15 @@ def _run(
209212
compile_future = concurrent.futures.ProcessPoolExecutor(max_workers=1).submit(
210213
app_task,
211214
*args,
215+
**kwargs,
212216
)
213217
validation_result = compile_future.result()
214218
else:
215-
validation_result = app_task(*args)
219+
validation_result = app_task(*args, **kwargs)
220+
216221
if not validation_result:
217222
raise click.exceptions.Exit(1)
218223

219-
# Warn if schema is not up to date.
220-
prerequisites.check_schema_up_to_date()
221-
222224
# Get the frontend and backend commands, based on the environment.
223225
setup_frontend = frontend_cmd = backend_cmd = None
224226
if env == constants.Env.DEV:
@@ -529,9 +531,7 @@ def migrate():
529531
from reflex import model
530532
from reflex.utils import prerequisites
531533

532-
# TODO see if we can use `get_app()` instead (no compile). Would _skip_compile still be needed then?
533-
_skip_compile()
534-
prerequisites.get_compiled_app()
534+
prerequisites.get_app()
535535
if not prerequisites.check_db_initialized():
536536
return
537537
model.Model.migrate()

reflex/utils/prerequisites.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,14 @@ def get_app(reload: bool = False) -> ModuleType:
393393
return app
394394

395395

396-
def get_and_validate_app(reload: bool = False) -> AppInfo:
396+
def get_and_validate_app(
397+
reload: bool = False, check_if_schema_up_to_date: bool = False
398+
) -> AppInfo:
397399
"""Get the app instance based on the default config and validate it.
398400
399401
Args:
400402
reload: Re-import the app module from disk
403+
check_if_schema_up_to_date: If True, check if the schema is up to date.
401404
402405
Returns:
403406
The app instance and the app module.
@@ -412,32 +415,47 @@ def get_and_validate_app(reload: bool = False) -> AppInfo:
412415
if not isinstance(app, App):
413416
msg = "The app instance in the specified app_module_import in rxconfig must be an instance of rx.App."
414417
raise RuntimeError(msg)
418+
419+
if check_if_schema_up_to_date:
420+
check_schema_up_to_date()
421+
415422
return AppInfo(app=app, module=app_module)
416423

417424

418-
def validate_app(reload: bool = False) -> None:
425+
def validate_app(
426+
reload: bool = False, check_if_schema_up_to_date: bool = False
427+
) -> None:
419428
"""Validate the app instance based on the default config.
420429
421430
Args:
422431
reload: Re-import the app module from disk
432+
check_if_schema_up_to_date: If True, check if the schema is up to date.
423433
"""
424-
get_and_validate_app(reload=reload)
434+
get_and_validate_app(
435+
reload=reload, check_if_schema_up_to_date=check_if_schema_up_to_date
436+
)
425437

426438

427439
def get_compiled_app(
428-
reload: bool = False, export: bool = False, dry_run: bool = False
440+
reload: bool = False,
441+
export: bool = False,
442+
dry_run: bool = False,
443+
check_if_schema_up_to_date: bool = False,
429444
) -> ModuleType:
430445
"""Get the app module based on the default config after first compiling it.
431446
432447
Args:
433448
reload: Re-import the app module from disk
434449
export: Compile the app for export
435450
dry_run: If True, do not write the compiled app to disk.
451+
check_if_schema_up_to_date: If True, check if the schema is up to date.
436452
437453
Returns:
438454
The compiled app based on the default config.
439455
"""
440-
app, app_module = get_and_validate_app(reload=reload)
456+
app, app_module = get_and_validate_app(
457+
reload=reload, check_if_schema_up_to_date=check_if_schema_up_to_date
458+
)
441459
# For py3.9 compatibility when redis is used, we MUST add any decorator pages
442460
# before compiling the app in a thread to avoid event loop error (REF-2172).
443461
app._apply_decorated_pages()
@@ -446,16 +464,25 @@ def get_compiled_app(
446464

447465

448466
def compile_app(
449-
reload: bool = False, export: bool = False, dry_run: bool = False
467+
reload: bool = False,
468+
export: bool = False,
469+
dry_run: bool = False,
470+
check_if_schema_up_to_date: bool = False,
450471
) -> None:
451472
"""Compile the app module based on the default config.
452473
453474
Args:
454475
reload: Re-import the app module from disk
455476
export: Compile the app for export
456477
dry_run: If True, do not write the compiled app to disk.
478+
check_if_schema_up_to_date: If True, check if the schema is up to date.
457479
"""
458-
get_compiled_app(reload=reload, export=export, dry_run=dry_run)
480+
get_compiled_app(
481+
reload=reload,
482+
export=export,
483+
dry_run=dry_run,
484+
check_if_schema_up_to_date=check_if_schema_up_to_date,
485+
)
459486

460487

461488
def _can_colorize() -> bool:
@@ -500,20 +527,23 @@ def _can_colorize() -> bool:
500527
return file.isatty()
501528

502529

503-
def compile_or_validate_app(compile: bool = False) -> bool:
530+
def compile_or_validate_app(
531+
compile: bool = False, check_if_schema_up_to_date: bool = False
532+
) -> bool:
504533
"""Compile or validate the app module based on the default config.
505534
506535
Args:
507536
compile: Whether to compile the app.
537+
check_if_schema_up_to_date: If True, check if the schema is up to date.
508538
509539
Returns:
510540
If the app is compiled successfully.
511541
"""
512542
try:
513543
if compile:
514-
compile_app()
544+
compile_app(check_if_schema_up_to_date=check_if_schema_up_to_date)
515545
else:
516-
validate_app()
546+
validate_app(check_if_schema_up_to_date=check_if_schema_up_to_date)
517547
except Exception as e:
518548
if isinstance(e, click.exceptions.Exit):
519549
return False

0 commit comments

Comments
 (0)