API - check app config before dropping database

This commit is contained in:
Sam 2022-02-13 14:31:59 +01:00
parent d1aa220e1d
commit eea3ab5ae7
2 changed files with 15 additions and 13 deletions

View File

@ -1,5 +1,6 @@
import logging import logging
import os import os
import shutil
from importlib import import_module, reload from importlib import import_module, reload
from typing import Any from typing import Any
@ -128,4 +129,17 @@ def create_app() -> Flask:
else: else:
return render_template('index.html') return render_template('index.html')
@app.cli.command('drop-db')
def drop_db() -> None:
"""Empty database and delete uploaded files for dev environments."""
if app_settings == 'fittrackee.config.ProductionConfig':
print('This is a production server, aborting!')
return
db.engine.execute("DROP TABLE IF EXISTS alembic_version;")
db.drop_all()
db.session.commit()
print('Database dropped.')
shutil.rmtree(app.config['UPLOAD_FOLDER'], ignore_errors=True)
print('Uploaded files deleted.')
return app return app

View File

@ -1,7 +1,6 @@
# source for StandaloneApplication class: # source for StandaloneApplication class:
# http://docs.gunicorn.org/en/stable/custom.html # http://docs.gunicorn.org/en/stable/custom.html
import os import os
import shutil
from typing import Dict, Optional from typing import Dict, Optional
import click import click
@ -9,7 +8,7 @@ import gunicorn.app.base
from flask import Flask from flask import Flask
from flask_migrate import upgrade from flask_migrate import upgrade
from fittrackee import create_app, db from fittrackee import create_app
from fittrackee.users.exceptions import UserNotFoundException from fittrackee.users.exceptions import UserNotFoundException
from fittrackee.users.utils import set_admin_rights from fittrackee.users.utils import set_admin_rights
@ -46,17 +45,6 @@ def upgrade_db() -> None:
upgrade(directory=BASEDIR + '/migrations') upgrade(directory=BASEDIR + '/migrations')
@app.cli.command('drop-db')
def drop_db() -> None:
"""Empty database and delete uploaded files for dev environments."""
db.engine.execute("DROP TABLE IF EXISTS alembic_version;")
db.drop_all()
db.session.commit()
print('Database dropped.')
shutil.rmtree(app.config['UPLOAD_FOLDER'], ignore_errors=True)
print('Uploaded files deleted.')
@app.cli.command('set-admin') @app.cli.command('set-admin')
@click.argument('username') @click.argument('username')
def set_admin(username: str) -> None: def set_admin(username: str) -> None: