Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Allow migrations to be run by pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
robyoung committed Feb 18, 2014
1 parent 48933a2 commit 3c086ee
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions run_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""
import imp
import os
import re
import sys
import pymongo
from os.path import join
import logging
from backdrop.core.database import Database
Expand Down Expand Up @@ -34,10 +34,16 @@ def get_database(config):
config.MONGO_HOSTS, config.MONGO_PORT, config.DATABASE_NAME)


def get_migrations():
def match_migration_path(path, pattern):
if not path.endswith('.py'):
return False
return pattern is None or pattern.search(path)


def get_migrations(pattern):
migrations_path = join(ROOT_PATH, 'migrations')
for migration_file in os.listdir(migrations_path):
if migration_file.endswith('.py'):
if match_migration_path(migration_file, pattern):
migration_path = join(migrations_path, migration_file)

yield imp.load_source('migration', migration_path)
Expand All @@ -48,6 +54,10 @@ def get_migrations():
config = load_config(os.getenv('GOVUK_ENV', 'development'))
database = get_database(config)

for migration in get_migrations():
migration_pattern = None
if len(sys.argv) > 1:
migration_pattern = re.compile(sys.argv[1])

for migration in get_migrations(migration_pattern):
log.info("Running migration %s" % migration)
migration.up(database)

0 comments on commit 3c086ee

Please sign in to comment.