def db_downgrade(step): """ Downgrades a DB to a previous version as specified with the `step` :param step: number of downgrades to do :return: None """ to_use = [_.strip('.sql') for _ in migration_files()] # since it's a downgrade, a reverse of the migration is essential to_use.reverse() generate_migration_file() dbd_query = anosql.from_path(MIGRATION_FILE, 'psycopg2') try: count = 0 for _ in to_use: count += 1 if MySQLScheme.fetch_one(REVISION_EXISTS, **{"args": {'revision': _}}): MySQLScheme.commit(getattr(dbd_query, f"downgrade_{_}").sql) LOGGER.info(f"successful downgrade: {_}") if count == step: break except errors.ProgrammingError: print("no more downgrade left")
def calls(): """ The decision box of this application :return: None """ parser = parse() arguments = parser.parse_args() from sqlraw import keyword, by_index, format_by_index, display_sql, \ regex, migration_files, files_by_number module = which_module() if arguments.db_initialise: getattr(module, 'db_initialise')() if arguments.db_migrate: getattr(module, 'db_migrate')() if arguments.db_upgrade: getattr(module, 'db_upgrade')() if arguments.keyword: print(keyword(arguments.keyword)) if arguments.db_downgrade: getattr(module, 'db_downgrade')(arguments.db_downgrade) if arguments.by_index: print(by_index(arguments.by_index)) if arguments.format_by_index: print(format_by_index(arguments.format_by_index)) if arguments.status: print(getattr(module, 'status')()) if arguments.files: print(migration_files()) if arguments.by_number: print(files_by_number()) if arguments.regex: print(regex(arguments.regex)) if arguments.collection: for _ in arguments.collection: print(display_sql(_))
def db_upgrade(): """ Runs an upgrade on a DB using the generated `MIGRATION_FILE` :return: None """ generate_migration_file() dbu_query = anosql.from_path(MIGRATION_FILE, 'psycopg2') for time_step in [_.strip('.sql') for _ in migration_files()]: decide = MySQLScheme.fetch_one(REVISION_EXISTS, **{"args": {'revision': time_step}}) if not decide: MySQLScheme.commit(getattr(dbu_query, f"upgrade_{time_step}").sql) LOGGER.info(f"successful migration: {time_step}") else: LOGGER.info(f'migration already exists: {time_step}')
def status(): """ Shows the already run migrations :return: String """ response = [] to_use = [_.strip('.sql') for _ in migration_files()] LOGGER.info(f"migration files: {to_use}") try: for step in to_use: if MySQLScheme.fetch_one(REVISION_EXISTS, **{"args": {'revision': step}}): response.append(f"migrations done : {step}") else: response.append(f"migrations undone: {step}") return "\n".join(response) except errors.ProgrammingError: return "No existing migration table"