Esempio n. 1
0
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)
Esempio n. 2
0
def load(name: str, dir: str = BACKUP_PATH, structure: bool = True, data: bool = False):
    """Load backed-up contents (see `db-ops save`), run `reset` first."""
    if name:
        from flexmeasures.data.scripts.data_gen import load_tables

        load_tables(app.db, name, structure=structure, data=data, backup_path=dir)
    else:
        click.echo("You must specify the name of the backup: --name <unique name>")
Esempio n. 3
0
def db_load(
    name: str, dir: str = BACKUP_PATH, structure: bool = True, data: bool = False
):
    """Load structure and/or data for the database from a backup file."""
    if name:
        from flexmeasures.data.scripts.data_gen import load_tables

        load_tables(app.db, name, structure=structure, data=data, backup_path=dir)
    else:
        click.echo("You must specify the name of the backup: --name <unique name>")
Esempio n. 4
0
def restore_data_response():

    delete_structure = True
    delete_data = True
    restore_structure = True
    restore_data = True

    try:
        backup_name = request.args.get("backup", request.json["backup"])
    except KeyError:
        return no_backup()

    # Make sure backup folder and files exist before restoring
    backup_path = app.config.get("FLEXMEASURES_DB_BACKUP_PATH")
    if (not Path("%s/%s" % (backup_path, backup_name)).exists()
            or not Path("%s/%s" % (backup_path, backup_name)).is_dir()):
        return unrecognized_backup()
    affected_classes = get_affected_classes(structure=restore_structure,
                                            data=restore_data)
    for c in affected_classes:
        file_path = "%s/%s/%s.obj" % (backup_path, backup_name,
                                      c.__tablename__)
        if not Path(file_path).exists():
            return unrecognized_backup(
                "Can't load table, because filename %s does not exist." %
                c.__tablename__)

    # Reset in play mode only (this endpoint should not have been registered otherwise)
    assert app.config.get("FLEXMEASURES_MODE", "") == "play"
    if delete_data:
        depopulate_forecasts(db)
        depopulate_data(db)
    if delete_structure:
        depopulate_structure(db)

    # Load backup
    load_tables(
        db,
        backup_name,
        structure=restore_structure,
        data=restore_data,
        backup_path=backup_path,
    )

    return request_processed("Database restored to %s." % backup_name)