← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/lp-signing:charm-migrate-db into lp-signing:master

 

Colin Watson has proposed merging ~cjwatson/lp-signing:charm-migrate-db into lp-signing:master with ~cjwatson/lp-signing:encrypted-private-keys as a prerequisite.

Commit message:
Run migrations in charm

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/lp-signing/+git/lp-signing/+merge/382431

In the long term we might want to do separate manual migrations instead; but the database structure is pretty simple, so this is good enough for now.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/lp-signing:charm-migrate-db into lp-signing:master.
diff --git a/charm/lp-signing/reactive/lp-signing.py b/charm/lp-signing/reactive/lp-signing.py
index 8dbed78..3e46c23 100644
--- a/charm/lp-signing/reactive/lp-signing.py
+++ b/charm/lp-signing/reactive/lp-signing.py
@@ -47,7 +47,24 @@ def configure_rsync():
             raise RuntimeError('Failed to restart rsync')
 
 
-@when('ols.configured', 'db.master.available')
+@when('ols.configured', 'db-admin.master.available')
+@when_not('service.migrated')
+def migrate(pgsql):
+    if pgsql.master is None:
+        return
+
+    hookenv.log('Running migrations.')
+    success, output = postgres.run_migration(pgsql, action=False)
+    if success:
+        hookenv.log('Migrations run successfully.')
+        set_state('service.migrated')
+    else:
+        hookenv.log('Failed to run migrations:')
+        for line in output.splitlines():
+            hookenv.log(line)
+
+
+@when('ols.configured', 'db.master.available', 'service.migrated')
 @when_not('service.configured')
 def configure(pgsql):
     if pgsql.master is None:
@@ -84,4 +101,5 @@ def check_is_running():
 def build_label_changed():
     remove_state('ols.service.installed')
     remove_state('ols.configured')
+    remove_state('service.migrated')
     remove_state('service.configured')