def downgrade_heads(revisions): """ Run migrations to available HEAD """ from faq_migrations.source.alembic_wrapper import AlembicMigrations am = AlembicMigrations() am.downgrade(revisions.split(','))
def downgrade(amount): """ Run migrations to available HEAD """ from faq_migrations.source.alembic_wrapper import AlembicMigrations am = AlembicMigrations() am.downgrade(amount)
def last_revision(): """ Show last created migration from files. This may be not yet applied migration """ from faq_migrations.source.alembic_wrapper import AlembicMigrations print(AlembicMigrations().__get_last_revision__())
def create(name): """ Create new migration with fingerprint of current git branch name :param name: migration name """ from faq_migrations.source.alembic_wrapper import AlembicMigrations AlembicMigrations().create(name)
def __init__(self, db_name): self.db_name = db_name self.db = Database(db_name=db_name) self.db.create() self.alembic = AlembicMigrations(Database(db_name=db_name).url) self.repo = git.Repo('') self.set_active_branch() self.initial_revision = self.repo.index.version print(f'current {db_name} revision is {self.initial_revision}')
def heads(abnormal_termination): """ Show current heads """ from faq_migrations.source.alembic_wrapper import AlembicMigrations al = AlembicMigrations() migration_heads = list(al.heads) print('------------------------------------------') for head in migration_heads: print(head.longdoc) print('Branch: {}'.format(al.branch_name(head))) print('------------------------------------------') # Exit with status code 1 if exists more then one migration. # We need it for CI if abnormal_termination and (len(migration_heads) > 1): raise SystemExit( f'Current migrations have {len(migration_heads)} heads. Fix it!' )
def migrate(): """ Run migrations to available HEAD """ from faq_migrations.source.alembic_wrapper import AlembicMigrations am = AlembicMigrations() if am.migrate() is False: print('\nYou must merge branches first\n') am.merge() am.migrate()
def history(limit=20, upper=True, verbose=False): """ Show migration history :param limit: limit of output migrations :param upper: if True will be show new migrations (like DESC by datetime) :param verbose: show log entry of migration """ from faq_migrations.source.alembic_wrapper import AlembicMigrations am = AlembicMigrations() revisions = am.history(limit, upper) for revision in revisions: if verbose: script = am.get_revision(revision.revision) print('{}({}): {}'.format( revision, am.branch_name(revision), script.log_entry )) continue print('{} ({})'.format( revision, am.branch_name(revision)) )
def init(): """ Creates new directory for migrations, generates alembic.ini and mako-files """ from faq_migrations.source.alembic_wrapper import AlembicMigrations AlembicMigrations().init()
def merge(): """ Start merging heads if there are more then one """ from faq_migrations.source.alembic_wrapper import AlembicMigrations AlembicMigrations().merge()
def current(): """ Show current migration revision from database """ from faq_migrations.source.alembic_wrapper import AlembicMigrations print(AlembicMigrations().current(True))
def upgrade_migrations(): """ Show not yet applied migrations """ from faq_migrations.source.alembic_wrapper import AlembicMigrations print(AlembicMigrations().upgrade_revisions(AlembicMigrations().current()))