reflex db cli: catch exception in makemigrations (#1362)
This commit is contained in:
parent
ca11b82432
commit
3cbee575fe
@ -8,6 +8,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import typer
|
import typer
|
||||||
|
from alembic.util.exc import CommandError
|
||||||
|
|
||||||
from reflex import constants, model
|
from reflex import constants, model
|
||||||
from reflex.config import get_config
|
from reflex.config import get_config
|
||||||
@ -275,6 +276,7 @@ def db_init():
|
|||||||
"scripts and [bold]reflex db migrate[/bold] to apply migrations "
|
"scripts and [bold]reflex db migrate[/bold] to apply migrations "
|
||||||
"to a new or existing database.",
|
"to a new or existing database.",
|
||||||
)
|
)
|
||||||
|
return
|
||||||
prerequisites.get_app()
|
prerequisites.get_app()
|
||||||
model.Model.alembic_init()
|
model.Model.alembic_init()
|
||||||
model.Model.migrate(autogenerate=True)
|
model.Model.migrate(autogenerate=True)
|
||||||
@ -282,7 +284,7 @@ def db_init():
|
|||||||
|
|
||||||
@db_cli.command()
|
@db_cli.command()
|
||||||
def migrate():
|
def migrate():
|
||||||
"""Create or update database schema based on app models or existing migration scripts."""
|
"""Create or update database schema from migration scripts."""
|
||||||
prerequisites.get_app()
|
prerequisites.get_app()
|
||||||
if not prerequisites.check_db_initialized():
|
if not prerequisites.check_db_initialized():
|
||||||
return
|
return
|
||||||
@ -301,7 +303,14 @@ def makemigrations(
|
|||||||
if not prerequisites.check_db_initialized():
|
if not prerequisites.check_db_initialized():
|
||||||
return
|
return
|
||||||
with model.Model.get_db_engine().connect() as connection:
|
with model.Model.get_db_engine().connect() as connection:
|
||||||
|
try:
|
||||||
model.Model.alembic_autogenerate(connection=connection, message=message)
|
model.Model.alembic_autogenerate(connection=connection, message=message)
|
||||||
|
except CommandError as command_error:
|
||||||
|
if "Target database is not up to date." not in str(command_error):
|
||||||
|
raise
|
||||||
|
console.print(
|
||||||
|
f"[red]{command_error} Run [bold]reflex db migrate[/bold] to update database."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
cli.add_typer(db_cli, name="db", help="Subcommands for managing the database schema")
|
cli.add_typer(db_cli, name="db", help="Subcommands for managing the database schema")
|
||||||
|
Loading…
Reference in New Issue
Block a user