Skip to content

Commit cae9e05

Browse files
committed
fix checking for migrations in granian
1 parent c849cd8 commit cae9e05

2 files changed

Lines changed: 47 additions & 17 deletions

File tree

reflex/reflex.py

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

206209
# Granian fails if the app is already imported.
207210
if should_use_granian():
@@ -210,16 +213,15 @@ def _run(
210213
compile_future = concurrent.futures.ProcessPoolExecutor(max_workers=1).submit(
211214
app_task,
212215
*args,
216+
**kwargs,
213217
)
214218
validation_result = compile_future.result()
215219
else:
216-
validation_result = app_task(*args)
220+
validation_result = app_task(*args, **kwargs)
221+
217222
if not validation_result:
218223
raise click.exceptions.Exit(1)
219224

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

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

reflex/utils/prerequisites.py

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

397397

398-
def get_and_validate_app(reload: bool = False) -> AppInfo:
398+
def get_and_validate_app(
399+
reload: bool = False, check_if_schema_up_to_date: bool = False
400+
) -> AppInfo:
399401
"""Get the app instance based on the default config and validate it.
400402
401403
Args:
402404
reload: Re-import the app module from disk
405+
check_if_schema_up_to_date: If True, check if the schema is up to date.
403406
404407
Returns:
405408
The app instance and the app module.
@@ -415,32 +418,47 @@ def get_and_validate_app(reload: bool = False) -> AppInfo:
415418
raise RuntimeError(
416419
"The app instance in the specified app_module_import in rxconfig must be an instance of rx.App."
417420
)
421+
422+
if check_if_schema_up_to_date:
423+
check_schema_up_to_date()
424+
418425
return AppInfo(app=app, module=app_module)
419426

420427

421-
def validate_app(reload: bool = False) -> None:
428+
def validate_app(
429+
reload: bool = False, check_if_schema_up_to_date: bool = False
430+
) -> None:
422431
"""Validate the app instance based on the default config.
423432
424433
Args:
425434
reload: Re-import the app module from disk
435+
check_if_schema_up_to_date: If True, check if the schema is up to date.
426436
"""
427-
get_and_validate_app(reload=reload)
437+
get_and_validate_app(
438+
reload=reload, check_if_schema_up_to_date=check_if_schema_up_to_date
439+
)
428440

429441

430442
def get_compiled_app(
431-
reload: bool = False, export: bool = False, dry_run: bool = False
443+
reload: bool = False,
444+
export: bool = False,
445+
dry_run: bool = False,
446+
check_if_schema_up_to_date: bool = False,
432447
) -> ModuleType:
433448
"""Get the app module based on the default config after first compiling it.
434449
435450
Args:
436451
reload: Re-import the app module from disk
437452
export: Compile the app for export
438453
dry_run: If True, do not write the compiled app to disk.
454+
check_if_schema_up_to_date: If True, check if the schema is up to date.
439455
440456
Returns:
441457
The compiled app based on the default config.
442458
"""
443-
app, app_module = get_and_validate_app(reload=reload)
459+
app, app_module = get_and_validate_app(
460+
reload=reload, check_if_schema_up_to_date=check_if_schema_up_to_date
461+
)
444462
# For py3.9 compatibility when redis is used, we MUST add any decorator pages
445463
# before compiling the app in a thread to avoid event loop error (REF-2172).
446464
app._apply_decorated_pages()
@@ -449,16 +467,25 @@ def get_compiled_app(
449467

450468

451469
def compile_app(
452-
reload: bool = False, export: bool = False, dry_run: bool = False
470+
reload: bool = False,
471+
export: bool = False,
472+
dry_run: bool = False,
473+
check_if_schema_up_to_date: bool = False,
453474
) -> None:
454475
"""Compile the app module based on the default config.
455476
456477
Args:
457478
reload: Re-import the app module from disk
458479
export: Compile the app for export
459480
dry_run: If True, do not write the compiled app to disk.
481+
check_if_schema_up_to_date: If True, check if the schema is up to date.
460482
"""
461-
get_compiled_app(reload=reload, export=export, dry_run=dry_run)
483+
get_compiled_app(
484+
reload=reload,
485+
export=export,
486+
dry_run=dry_run,
487+
check_if_schema_up_to_date=check_if_schema_up_to_date,
488+
)
462489

463490

464491
def _can_colorize() -> bool:
@@ -503,20 +530,23 @@ def _can_colorize() -> bool:
503530
return file.isatty()
504531

505532

506-
def compile_or_validate_app(compile: bool = False) -> bool:
533+
def compile_or_validate_app(
534+
compile: bool = False, check_if_schema_up_to_date: bool = False
535+
) -> bool:
507536
"""Compile or validate the app module based on the default config.
508537
509538
Args:
510539
compile: Whether to compile the app.
540+
check_if_schema_up_to_date: If True, check if the schema is up to date.
511541
512542
Returns:
513543
If the app is compiled successfully.
514544
"""
515545
try:
516546
if compile:
517-
compile_app()
547+
compile_app(check_if_schema_up_to_date=check_if_schema_up_to_date)
518548
else:
519-
validate_app()
549+
validate_app(check_if_schema_up_to_date=check_if_schema_up_to_date)
520550
except Exception as e:
521551
if isinstance(e, click.exceptions.Exit):
522552
return False

0 commit comments

Comments
 (0)