|
a/pyforge/flyway/runner.py |
|
b/pyforge/flyway/runner.py |
1 |
import logging
|
1 |
import logging
|
|
|
2 |
|
|
|
3 |
from ming.orm import ORMSession
|
2 |
|
4 |
|
3 |
from .model import MigrationInfo
|
5 |
from .model import MigrationInfo
|
4 |
from .migrate import Migration
|
6 |
from .migrate import Migration
|
5 |
from . import graph
|
7 |
from . import graph
|
6 |
|
8 |
|
|
... |
|
... |
10 |
'''Attempt to migrate the database to a specific set of required
|
12 |
'''Attempt to migrate the database to a specific set of required
|
11 |
modules & versions.'''
|
13 |
modules & versions.'''
|
12 |
# Get the migration status of the db
|
14 |
# Get the migration status of the db
|
13 |
session = MigrationInfo.__mongometa__.session
|
15 |
session = MigrationInfo.__mongometa__.session
|
14 |
session.bind = datastore
|
16 |
session.bind = datastore
|
|
|
17 |
ormsession = ORMSession(session)
|
15 |
info = MigrationInfo.m.get()
|
18 |
info = MigrationInfo.m.get()
|
16 |
if info is None:
|
19 |
if info is None:
|
17 |
info = MigrationInfo.make({})
|
20 |
info = MigrationInfo.make({})
|
18 |
latest_versions = Migration.latest_versions()
|
21 |
latest_versions = Migration.latest_versions()
|
19 |
for k,v in target_versions.iteritems():
|
22 |
for k,v in target_versions.iteritems():
|
20 |
cur = info.versions.get(k, -1)
|
23 |
cur = info.versions.get(k, -1)
|
21 |
islatest = ' (LATEST)' if v == latest_versions[k] else ''
|
24 |
islatest = ' (LATEST)' if v == latest_versions[k] else ''
|
22 |
log.info('Target %s=%s%s (current=%s)', k, v, islatest, cur)
|
25 |
log.info('Target %s=%s%s (current=%s)', k, v, islatest, cur)
|
23 |
# Create a migration plan
|
26 |
# Create a migration plan
|
24 |
plan = list(plan_migration(session, info, target_versions))
|
27 |
plan = list(plan_migration(session, ormsession, info, target_versions))
|
25 |
# Execute (or print) the plan
|
28 |
# Execute (or print) the plan
|
26 |
for step in plan:
|
29 |
for step in plan:
|
27 |
log.info('Migrate %r', step)
|
30 |
log.info('Migrate %r', step)
|
28 |
if dry_run: continue
|
31 |
if dry_run: continue
|
29 |
step.apply(info.versions)
|
32 |
step.apply(info.versions)
|
30 |
info.m.save()
|
33 |
info.m.save()
|
31 |
|
34 |
|
32 |
def plan_migration(session, info, target):
|
35 |
def plan_migration(session, ormsession, info, target):
|
33 |
'''Return the optimal list of graph.MigrationSteps to run in order to
|
36 |
'''Return the optimal list of graph.MigrationSteps to run in order to
|
34 |
satisfy the target requirements'''
|
37 |
satisfy the target requirements'''
|
35 |
migrations = dict((k, v(session))
|
38 |
migrations = dict((k, v(session, ormsession))
|
36 |
for k,v in Migration.migrations_registry.iteritems())
|
39 |
for k,v in Migration.migrations_registry.iteritems())
|
37 |
g = graph.MigrationGraph(migrations)
|
40 |
g = graph.MigrationGraph(migrations)
|
38 |
return g.shortest_path(info.versions, target)
|
41 |
return g.shortest_path(info.versions, target)
|
39 |
|
42 |
|
40 |
def reset_migration(datastore, dry_run):
|
43 |
def reset_migration(datastore, dry_run):
|