-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
52 lines (42 loc) · 1.7 KB
/
run.py
File metadata and controls
52 lines (42 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import click
from flask.cli import with_appcontext
from flask_migrate import upgrade
from app import create_app, db
from seeding import populate_initial_data as populate_db_function
config_name = os.getenv('FLASK_CONFIG') or 'default'
app = create_app(config_name)
@app.cli.command("seed-db")
@with_appcontext
def seed_db_command():
"""Seeds the database with initial data from JSON."""
click.echo("Seeding the database with initial data...")
try:
populate_db_function()
click.echo(click.style("Initial data added successfully.", fg="green"))
except Exception as e:
db.session.rollback()
click.echo(click.style(f"Error while seeding data: {e}", fg="red"), err=True)
# Новая, автоматизированная команда
@app.cli.command("init-app")
@with_appcontext
def init_app_command():
"""Initializes the application: creates/updates DB schema and seeds it."""
click.echo("Initializing the application...")
click.echo("Applying database migrations...")
try:
upgrade()
click.echo(click.style("Migrations applied successfully.", fg="green"))
except Exception as e:
click.echo(click.style(f"Error applying migrations: {e}", fg="red"), err=True)
return
click.echo("Seeding the database with initial data...")
try:
populate_db_function()
click.echo(click.style("Initial data seeded successfully.", fg="green"))
except Exception as e:
db.session.rollback()
click.echo(click.style(f"Error while seeding data: {e}", fg="red"), err=True)
click.echo(click.style("Application initialized successfully!", fg="cyan"))
if __name__ == '__main__':
app.run(host='0.0.0.0')