~corey.bryant/glance/2014.1.rc1

« back to all changes in this revision

Viewing changes to debian/patches/ensure_versioned_db_models.patch

  • Committer: Chuck Short
  • Date: 2014-01-07 18:44:29 UTC
  • Revision ID: zulcss@ubuntu.com-20140107184429-7ncoyudf9x9uxy0j
* debian/control: Add python-psutil as a build dependency.
* debian/patches/skip-tests.patch: Rediffed.
* debian/patches/debian/patches/ensure_versioned_db_models.patch:
  Removed crufty patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Author: Adam Gandelman <admag@canonical.com>
2
 
Date: 05-24-2012
3
 
Subject: Ensure version control and valid database models 
4
 
 
5
 
Since Folsom-1, glance no longer auto-creates database models by default.
6
 
Ubuntu patches disabled this in Essex / 12.04 along with some other precautions
7
 
such as refusing to start without consistent database schema, and ensuring
8
 
database is version controlled before any migrations are run.  To maintain
9
 
consistent behavior with Essex on Ubuntu, this patch adds check of database
10
 
models at service startup and requires databases to be version controlled
11
 
before running any migrations (requiring 'glance-manage version_control 0' before
12
 
initializing a new datbase via 'glance-manage db_sync')
13
 
 
14
 
Index: glance/glance/db/sqlalchemy/api.py
15
 
===================================================================
16
 
--- glance.orig/glance/db/sqlalchemy/api.py     2012-06-15 11:46:04.380813622 -0700
17
 
+++ glance/glance/db/sqlalchemy/api.py  2012-06-15 12:32:27.932711363 -0700
18
 
@@ -23,6 +23,7 @@
19
 
 """
20
 
 
21
 
 import logging
22
 
+import os
23
 
 import time
24
 
 
25
 
 import sqlalchemy
26
 
@@ -89,6 +90,32 @@
27
 
                 raise
28
 
 
29
 
 
30
 
+def ensure_db_models():
31
 
+    """
32
 
+    Ensure connection to database and sqlalchemy models are consistent.
33
 
+    Connection errors would have been caught in models.register_models(),
34
 
+    which also auto-generations all tables outside of migration version
35
 
+    control. To avoid auto-creation, we can catch connection errors as well
36
 
+    as schema inconsitencies between sqlalchemy models and db.  If they exist,
37
 
+    throw an error and inform user to migrate the database manually.
38
 
+    """
39
 
+    session = get_session()
40
 
+    try:
41
 
+        # NOTE(adam_g); list of models (tables) as of 03/07/2012, defined
42
 
+        # in glance.registry.db.models.
43
 
+        test_image = session.query(models.Image).first()
44
 
+        test_image_property = session.query(models.ImageProperty).first()
45
 
+        test_image_member = session.query(models.ImageMember).first()
46
 
+    except Exception, e:
47
 
+        logger.error(e)
48
 
+        msg = (_('Could not ensure database connection and consistency. '\
49
 
+                 'Ensure database configuration and permissions are correct '\
50
 
+                 'and database has been migrated since last upgrade by '\
51
 
+                 'running \'glance-manage db_sync\''))
52
 
+        logger.error(msg)
53
 
+        raise e
54
 
+
55
 
+
56
 
 def configure_db():
57
 
     """
58
 
     Establish the database, create an engine if needed, and
59
 
@@ -122,7 +149,7 @@
60
 
         if CONF.debug:
61
 
             sa_logger.setLevel(logging.DEBUG)
62
 
 
63
 
-        if CONF.db_auto_create:
64
 
+        if 'AUTO_REGISTER_DB_MODELS' in os.environ or CONF.db_auto_create:
65
 
             logger.info('auto-creating glance registry DB')
66
 
             models.register_models(_ENGINE)
67
 
             try:
68
 
@@ -132,6 +159,7 @@
69
 
                 pass
70
 
         else:
71
 
             logger.info('not auto-creating glance registry DB')
72
 
+            ensure_db_models()
73
 
 
74
 
 
75
 
 def check_mutate_authorization(context, image_ref):
76
 
Index: glance/glance/db/sqlalchemy/migration.py
77
 
===================================================================
78
 
--- glance.orig/glance/db/sqlalchemy/migration.py       2012-06-15 11:46:04.360813621 -0700
79
 
+++ glance/glance/db/sqlalchemy/migration.py    2012-06-15 12:33:46.680708471 -0700
80
 
@@ -121,6 +121,18 @@
81
 
                      "migration control") % locals())
82
 
             raise exception.DatabaseMigrationError(msg)
83
 
 
84
 
+    # Attempt to get migration_repo version.  Should succeed
85
 
+    # if db was created under version control.
86
 
+    try:
87
 
+        db_version()
88
 
+    except exception.DatabaseMigrationError:
89
 
+        # If it was not created under VC, do nothing until user puts it
90
 
+        # under VC and sets appropriate version.
91
 
+        msg = (_("Database '%(sql_connection)s' does not appear to be under "
92
 
+                 "version control.  Refusing to migrate.  You can enable  "
93
 
+                 "version control for by using 'glance-manage version_control "
94
 
+                 " [VERSION #]") % locals())
95
 
+        raise exception.DatabaseMigrationError(msg)
96
 
     upgrade(version=version)
97
 
 
98