def commit_db_model(db_uri, db_repo, model_dotted_name, commit_msg):
    logger.info(
        "Committing new change on model : {}".format(model_dotted_name))

    # Test if the migrate repository exists
    if not os.path.isdir(db_repo):
        raise OSError(2, "Migrate repository doesn't exists ! ", db_repo)

    new_model = load_model(model_dotted_name)

    # Display detected changes to user
    print("------------------------------")
    print(mapi.compare_model_to_db(db_uri, db_repo, new_model))
    print("------------------------------")
    if not confirm(
            "Are these changes correct (ignored tables {} are listed here but will be ignored ) ?"
            .format(IGNORED_TABLES)):
        logger.info("Changes not confirmed aborting :(")
        return

    with TemporaryDirectory(prefix="OPVDBMigrate") as temp_dir:
        sys.path.append(temp_dir)  # Used to read generated python
        old_model_fpath = Path(temp_dir) / "oldmodel.py"

        # generating old model from database
        logger.info("Generated old model python script from database")
        old_model_source = mapi.create_model(db_uri, db_repo)
        logger.debug("Old model python : \n {}".format(old_model_source))
        old_model_source = correction_geography(old_model_source)
        save_to_file(old_model_source, old_model_fpath)

        # get latest version
        new_version = mapi.version(db_repo) + 1
        update_script_path = Path(
            db_repo) / "versions" / get_update_script_name(
                new_version, commit_msg)
        logger.debug("Lastest version in repository {} is {}".format(
            db_repo, new_version))

        # make_update_script_for_model, save it in repo lastest_version + 1 + commit msg
        oldmodel = load_model('oldmodel:meta')
        oldmodel = filter_tables(oldmodel, IGNORED_TABLES)
        update_script_source = mapi.make_update_script_for_model(
            db_uri, db_repo, oldmodel, new_model)
        print(update_script_source)
        input("Wait ... ")
        save_to_file(update_script_source, update_script_path)

        if confirm(
                "Did you check (and modify if necessary) the upgrade script ({}) ? !! Confirm will upgrade the database !!"
                .format(update_script_path)):
            logger.info("Upgrading the database")
            # upgrade
            mapi.upgrade(db_uri, db_repo)
            logger.info("Migration done and versionned :) ")
        else:
            logger.info("Removing the update script")
            os.unlink(update_script_path)
Example #2
0
File: db.py Project: fucc1/FPA_Core
def migrate():
    """ Run pending data migrations """
    url = current_app.config.get("SQLALCHEMY_DATABASE_URI")
    repo = os.path.join(os.path.dirname(__file__), "..", "findmigs")

    try:
        migrate_api.upgrade(url, repo)
    except DatabaseNotControlledError:
        # Assume it's a new database, and try the migration again
        migrate_api.version_control(url, repo)
        migrate_api.upgrade(url, repo)

    diff = migrate_api.compare_model_to_db(url, repo, db.metadata)
    if diff:
        print diff
        raise CommandException("The database doesn't match the current model")
Example #3
0
File: db.py Project: fucc1/FPA_Core
def migrate():
    """ Run pending data migrations """
    url = current_app.config.get('SQLALCHEMY_DATABASE_URI')
    repo = os.path.join(os.path.dirname(__file__), '..', 'findmigs')

    try:
        migrate_api.upgrade(url, repo)
    except DatabaseNotControlledError:
        # Assume it's a new database, and try the migration again
        migrate_api.version_control(url, repo)
        migrate_api.upgrade(url, repo)

    diff = migrate_api.compare_model_to_db(url, repo, db.metadata)
    if diff:
        print diff
        raise CommandException("The database doesn't match the current model")
Example #4
0
def migrate():
    url = config.get('openspending.db.url')
    repo = config.get('openspending.migrate_dir',
                      os.path.join(os.path.dirname(config['__file__']),
                                   'migration'))

    try:
        migrate_api.upgrade(url, repo)
    except DatabaseNotControlledError:
        # Assume it's a new database, and try the migration again
        migrate_api.version_control(url, repo)
        migrate_api.upgrade(url, repo)

    diff = migrate_api.compare_model_to_db(url, repo, db.metadata)
    if diff:
        # Oh dear! The database we migrated to doesn't match openspending.model
        print diff
        return 1

    return 0
Example #5
0
 def test_compare_model_to_db(self):
     diff = api.compare_model_to_db(self.url, self.repo, models.meta)
Example #6
0
 def test_compare_model_to_db(self):
     diff = api.compare_model_to_db(self.url, self.repo, models.meta)