def db_reset( load: str = None, dir: str = BACKUP_PATH, structure: bool = True, data: bool = False ): """Reset database, with options to load fresh data.""" if not app.debug: prompt = ( "This deletes all data and resets the structure on %s.\nDo you want to continue?" % app.db.engine ) if not click.confirm(prompt): click.echo("I did nothing.") return from flexmeasures.data.scripts.data_gen import reset_db current_version = migrate.current() reset_db(app.db) migrate.stamp(current_version) if load: if not data and not structure: click.echo("Neither --data nor --structure given ... loading nothing.") return from flexmeasures.data.scripts.data_gen import load_tables load_tables(app.db, load, structure, data, dir)
def free_the_zoo(zoo_url): confirm = str(input('Are you sure you want to clear and re-migrate the database? (y/N): ')).strip() if confirm == 'y': init_zoo_db(zoo_url) if flask_migrate.current() is not None: flask_migrate.downgrade(tag='base') flask_migrate.upgrade() migrate_models()
def reset(): """Reset database data and re-create tables from data model.""" if not app.debug: prompt = ( "This deletes all data and re-creates the tables on %s.\nDo you want to continue?" % app.db.engine ) if not click.confirm(prompt): click.echo("I did nothing.") return from flexmeasures.data.scripts.data_gen import reset_db current_version = migrate.current() reset_db(app.db) migrate.stamp(current_version)
def upgrade_db(first_attempt=True): try: upgrade() except Exception: print('Error: Could not run migrations') if first_attempt and current() == None: # The migrations failed and the database has no record of past migrations. # # This is likely due to the developer having a version of the database from before Flask-Migrate # was introduced to the project. # # We try to automatically resolve this by telling alembic (the migration tool Flask-Migrate is based on) # that this database equates to the first revision, and then try the migration again. print( 'Retrying migration after stamping database with baseline revision' ) stamp(revision='baseline') upgrade_db(False) else: logging.error(traceback.format_exc()) raise
def database(action, revision, message): with webui.app.app_context(): # creates folder /migrations with necessary files for migrations if action == "init": init_migration() # creates migration with current state of models elif action == "migrate": comment = str(message) if message else "" migrate(message=comment) # upgrades current state to latest version of database or to defined revision elif action == "upgrade": if revision: upgrade(revision=revision) else: upgrade() # downgrades current state to previous version of database or to defined revision elif action == "downgrade": if revision: downgrade(revision=revision) else: downgrade() # shows current migration elif action == "current": return current() # shows history of migrations until current migration elif action == "history": return history() # creates migration manually with comment = message elif action == "create-migration": comment = str(message) if message else "" return manual_migrate(message=comment)
def db_current(c, dir=DEFAULT_MIGRATION_DIR): """ Show current migration revision. """ with app.app_context(): migrate.current(directory=dir)
def current(args): """Get current database schema revision.""" flask_migrate.current(DIRECTORY)
def mcurrent(directory=None, verbose=False, head_only=False): current(directory=None, verbose=False, head_only=False)
from app import app, db from flask_migrate import stamp, current with app.app_context(): db.create_all() print('Database tables:') for table in db.metadata.sorted_tables: print('- {}'.format(table)) stamp() print('Alembic current revision set to:') current()