1
Author: Adam Gandelman <admag@canonical.com>
3
Subject: Ensure version control and valid database models
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')
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
30
+def ensure_db_models():
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.
39
+ session = get_session()
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:
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\''))
58
Establish the database, create an engine if needed, and
61
sa_logger.setLevel(logging.DEBUG)
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)
71
logger.info('not auto-creating glance registry DB')
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
81
"migration control") % locals())
82
raise exception.DatabaseMigrationError(msg)
84
+ # Attempt to get migration_repo version. Should succeed
85
+ # if db was created under version control.
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)