allura
Revision | 31cc45a051263f0351a4767bbda5ff469dda34a5 (tree) |
---|---|
Time | 2010-05-05 03:03:51 |
Author | Rick Copeland <rcopeland@geek...> |
Commiter | Rick Copeland |
[#330] - add flyway options, as well as flyway initialization on project DB create
@@ -22,11 +22,13 @@ class MigrateCommand(command.Command): | ||
22 | 22 | parser.add_option('--log-level', dest='log_level', default='INFO') |
23 | 23 | parser.add_option('--reset', dest='reset', action='store_true', default=False) |
24 | 24 | parser.add_option('-d', '--dry-run', dest='dry_run', action='store_true', default=False) |
25 | + parser.add_option('-s', '--status', dest='status_only', action='store_true', default=False) | |
26 | + parser.add_option('--force', dest='force', action='store_true', default=False) | |
25 | 27 | |
26 | 28 | def command(self): |
27 | 29 | self._setup_logging() |
28 | 30 | self._load_migrations() |
29 | - from .runner import run_migration, reset_migration | |
31 | + from .runner import run_migration, reset_migration, show_status, set_status | |
30 | 32 | parsed_connection_url = parse_uri(self.options.connection_url) |
31 | 33 | if not parsed_connection_url['path']: |
32 | 34 | parsed_connection_url['path'] += '/' |
@@ -45,7 +47,11 @@ class MigrateCommand(command.Command): | ||
45 | 47 | parsed_connection_url['port']) |
46 | 48 | for ds in datastores: |
47 | 49 | self.log.info('Migrate DB: %s', ds.database) |
48 | - if self.options.reset: | |
50 | + if self.options.status_only: | |
51 | + show_status(ds) | |
52 | + elif self.options.force: | |
53 | + set_status(ds, self._target_versions()) | |
54 | + elif self.options.reset: | |
49 | 55 | reset_migration(ds, dry_run=self.options.dry_run) |
50 | 56 | else: |
51 | 57 | run_migration(ds, self._target_versions(), dry_run=self.options.dry_run) |
@@ -32,6 +32,31 @@ def run_migration(datastore, target_versions, dry_run): | ||
32 | 32 | step.apply(info.versions) |
33 | 33 | info.m.save() |
34 | 34 | |
35 | +def show_status(datastore): | |
36 | + # Get the migration status of the db | |
37 | + session = MigrationInfo.__mongometa__.session | |
38 | + session.bind = datastore | |
39 | + info = MigrationInfo.m.get() | |
40 | + if info is None: | |
41 | + info = MigrationInfo.make({}) | |
42 | + for k,v in info.versions.iteritems(): | |
43 | + log.info('%s=%s', k, v) | |
44 | + | |
45 | +def set_status(datastore, target_versions): | |
46 | + # Get the migration status of the db | |
47 | + session = MigrationInfo.__mongometa__.session | |
48 | + session.bind = datastore | |
49 | + info = MigrationInfo.m.get() | |
50 | + if info is None: | |
51 | + info = MigrationInfo.make({}) | |
52 | + latest_versions = Migration.latest_versions() | |
53 | + for k,v in target_versions.iteritems(): | |
54 | + cur = info.versions.get(k, -1) | |
55 | + islatest = ' (LATEST)' if v == latest_versions[k] else '' | |
56 | + log.info('FORCE %s=%s%s (current=%s)', k, v, islatest, cur) | |
57 | + info.versions.update(target_versions) | |
58 | + info.m.save() | |
59 | + | |
35 | 60 | def plan_migration(session, ormsession, info, target): |
36 | 61 | '''Return the optimal list of graph.MigrationSteps to run in order to |
37 | 62 | satisfy the target requirements''' |
@@ -147,6 +147,7 @@ class Neighborhood(MappedClass): | ||
147 | 147 | + 'You can edit this description in the admin page'), |
148 | 148 | database=database, |
149 | 149 | is_root=True) |
150 | + p.configure_flyway_initial() | |
150 | 151 | with h.push_config(c, project=p, user=user): |
151 | 152 | # Install default named roles (#78) |
152 | 153 | role_owner = auth.ProjectRole(name='Owner') |
@@ -466,6 +467,16 @@ class Project(MappedClass): | ||
466 | 467 | project_users = uniq([r.user for r in self.roles if not r.user.username.startswith('*')]) |
467 | 468 | return project_users |
468 | 469 | |
470 | + def configure_flyway_initial(self): | |
471 | + from flyway.model import MigrationInfo | |
472 | + from flyway.migrate import Migration | |
473 | + with h.push_config(c, project=self): | |
474 | + mi = project_doc_session.get(MigrationInfo) | |
475 | + if mi is None: | |
476 | + mi = MigrationInfo.make({}) | |
477 | + mi.versions.update(Migration.latest_versions()) | |
478 | + project_doc_session.save(mi) | |
479 | + | |
469 | 480 | class AppConfig(MappedClass): |
470 | 481 | class __mongometa__: |
471 | 482 | session = project_orm_session |