Skip to content

Commit

Permalink
Refactor code for marking a migration as completed or not
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc committed Apr 18, 2016
1 parent b5095fe commit 40936f1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions dbmigrator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,25 @@ def compare_schema(db_connection_string, callback, *args, **kwargs):
def run_migration(cursor, version, migration_name, migration):
print('Running migration {} {}'.format(version, migration_name))
migration.up(cursor)
cursor.execute('INSERT INTO schema_migrations VALUES (%s)', (version,))
mark_migration(cursor, version, True)
cursor.connection.commit()


def rollback_migration(cursor, version, migration_name, migration):
print('Rolling back migration {} {}'.format(version, migration_name))
migration.down(cursor)
cursor.execute('DELETE FROM schema_migrations WHERE version = %s',
(version,))
mark_migration(cursor, version, False)
cursor.connection.commit()


def timestamp():
now = datetime.datetime.utcnow()
return now.strftime('%Y%m%d%H%M%S')


def mark_migration(cursor, version, completed):
if completed:
cursor.execute('INSERT INTO schema_migrations VALUES (%s)', (version,))
else:
cursor.execute('DELETE FROM schema_migrations WHERE version = %s',
(version,))

0 comments on commit 40936f1

Please sign in to comment.