Merge lp:~jaypipes/glance/bug726685 into lp:~glance-coresec/glance/cactus-trunk

Proposed by Jay Pipes
Status: Merged
Approved by: Rick Harris
Approved revision: 99
Merged at revision: 107
Proposed branch: lp:~jaypipes/glance/bug726685
Merge into: lp:~glance-coresec/glance/cactus-trunk
Diff against target: 319 lines (+218/-13)
9 files modified
glance/registry/db/api.py (+5/-5)
glance/registry/db/migrate_repo/versions/006_key_to_name.py (+93/-0)
glance/registry/db/migrate_repo/versions/006_mysql_downgrade.sql (+11/-0)
glance/registry/db/migrate_repo/versions/006_mysql_upgrade.sql (+11/-0)
glance/registry/db/migrate_repo/versions/006_sqlite_downgrade.sql (+46/-0)
glance/registry/db/migrate_repo/versions/006_sqlite_upgrade.sql (+46/-0)
glance/registry/db/models.py (+2/-4)
glance/registry/server.py (+1/-1)
tests/stubs.py (+3/-3)
To merge this branch: bzr merge lp:~jaypipes/glance/bug726685
Reviewer Review Type Date Requested Status
Rick Harris (community) Approve
Glance Core security contacts Pending
Review via email: mp+56252@code.launchpad.net

Commit message

Changes "key" column in image_properties to "name".

Description of the change

Changes "key" column in image_properties to "name".

As with all things migration, this was a pain in the ass.

MySQL doesn't support RENAME INDEX, so a custom SQL migration
file for it was needed.

Likewise, SQLite doesn't support either RENAME INDEX or
ALTER TABLE CHANGE COLUMN, so it also needed a custom migration
script.

To post a comment you must log in.
Revision history for this message
Rick Harris (rconradharris) wrote :

Impressive work with the migrations to make this work out. Really nice job.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'glance/registry/db/api.py'
2--- glance/registry/db/api.py 2011-04-06 15:17:34 +0000
3+++ glance/registry/db/api.py 2011-04-07 19:09:30 +0000
4@@ -251,14 +251,14 @@
5 """
6 orig_properties = {}
7 for prop_ref in image_ref.properties:
8- orig_properties[prop_ref.key] = prop_ref
9+ orig_properties[prop_ref.name] = prop_ref
10
11- for key, value in properties.iteritems():
12+ for name, value in properties.iteritems():
13 prop_values = {'image_id': image_ref.id,
14- 'key': key,
15+ 'name': name,
16 'value': value}
17- if key in orig_properties:
18- prop_ref = orig_properties[key]
19+ if name in orig_properties:
20+ prop_ref = orig_properties[name]
21 image_property_update(context, prop_ref, prop_values,
22 session=session)
23 else:
24
25=== added file 'glance/registry/db/migrate_repo/versions/006_key_to_name.py'
26--- glance/registry/db/migrate_repo/versions/006_key_to_name.py 1970-01-01 00:00:00 +0000
27+++ glance/registry/db/migrate_repo/versions/006_key_to_name.py 2011-04-07 19:09:30 +0000
28@@ -0,0 +1,93 @@
29+# vim: tabstop=4 shiftwidth=4 softtabstop=4
30+
31+# Copyright 2011 OpenStack LLC.
32+# All Rights Reserved.
33+#
34+# Licensed under the Apache License, Version 2.0 (the "License"); you may
35+# not use this file except in compliance with the License. You may obtain
36+# a copy of the License at
37+#
38+# http://www.apache.org/licenses/LICENSE-2.0
39+#
40+# Unless required by applicable law or agreed to in writing, software
41+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
42+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
43+# License for the specific language governing permissions and limitations
44+# under the License.
45+
46+from migrate.changeset import *
47+from sqlalchemy import *
48+from sqlalchemy.sql import and_, not_
49+
50+from glance.registry.db.migrate_repo.schema import (
51+ Boolean, DateTime, Integer, String, Text, from_migration_import)
52+
53+
54+def get_images_table(meta):
55+ """
56+ No changes to the image properties table from 002...
57+ """
58+ (get_images_table,) = from_migration_import(
59+ '004_add_checksum', ['get_images_table'])
60+
61+ images = get_images_table(meta)
62+ return images
63+
64+
65+def get_image_properties_table(meta):
66+ """
67+ Returns the Table object for the image_properties table that
68+ corresponds to the image_properties table definition of this version.
69+ """
70+ (get_images_table,) = from_migration_import(
71+ '004_add_checksum', ['get_images_table'])
72+
73+ images = get_images_table(meta)
74+
75+ image_properties = Table('image_properties', meta,
76+ Column('id', Integer(), primary_key=True, nullable=False),
77+ Column('image_id', Integer(), ForeignKey('images.id'), nullable=False,
78+ index=True),
79+ Column('name', String(255), nullable=False),
80+ Column('value', Text()),
81+ Column('created_at', DateTime(), nullable=False),
82+ Column('updated_at', DateTime()),
83+ Column('deleted_at', DateTime()),
84+ Column('deleted', Boolean(), nullable=False, default=False,
85+ index=True),
86+ UniqueConstraint('image_id', 'name'),
87+ mysql_engine='InnoDB',
88+ useexisting=True)
89+
90+ return image_properties
91+
92+
93+def upgrade(migrate_engine):
94+ meta = MetaData()
95+ meta.bind = migrate_engine
96+
97+ (get_image_properties_table,) = from_migration_import(
98+ '004_add_checksum', ['get_image_properties_table'])
99+ image_properties = get_image_properties_table(meta)
100+
101+ index = Index('ix_image_properties_image_id_get',
102+ image_properties.c.image_id,
103+ image_properties.c.key)
104+ index.rename('ix_image_properties_image_id_name')
105+
106+ image_properties = get_image_properties_table(meta)
107+ image_properties.columns['key'].alter(name="name")
108+
109+
110+def downgrade(migrate_engine):
111+ meta = MetaData()
112+ meta.bind = migrate_engine
113+
114+ image_properties = get_image_properties_table(meta)
115+
116+ index = Index('ix_image_properties_image_id_name',
117+ image_properties.c.image_id,
118+ image_properties.c.name)
119+ index.rename('ix_image_properties_image_id_key')
120+
121+ image_properties.columns['name'].alter(name="key")
122
123=== added file 'glance/registry/db/migrate_repo/versions/006_mysql_downgrade.sql'
124--- glance/registry/db/migrate_repo/versions/006_mysql_downgrade.sql 1970-01-01 00:00:00 +0000
125+++ glance/registry/db/migrate_repo/versions/006_mysql_downgrade.sql 2011-04-07 19:09:30 +0000
126@@ -0,0 +1,11 @@
127+/*
128+ * This file is necessary because MySQL does not support
129+ * renaming indexes.
130+ */
131+DROP INDEX ix_image_properties_image_id_name ON image_properties;
132+
133+/* Rename the `key` column to `name` */
134+ALTER TABLE image_properties
135+CHANGE COLUMN name `key` VARCHAR(255) NOT NULL;
136+
137+CREATE UNIQUE INDEX ix_image_properties_image_id_key ON image_properties (image_id, key);
138
139=== added file 'glance/registry/db/migrate_repo/versions/006_mysql_upgrade.sql'
140--- glance/registry/db/migrate_repo/versions/006_mysql_upgrade.sql 1970-01-01 00:00:00 +0000
141+++ glance/registry/db/migrate_repo/versions/006_mysql_upgrade.sql 2011-04-07 19:09:30 +0000
142@@ -0,0 +1,11 @@
143+/*
144+ * This file is necessary because MySQL does not support
145+ * renaming indexes.
146+ */
147+DROP INDEX ix_image_properties_image_id_key ON image_properties;
148+
149+/* Rename the `key` column to `name` */
150+ALTER TABLE image_properties
151+CHANGE COLUMN `key` name VARCHAR(255) NOT NULL;
152+
153+CREATE UNIQUE INDEX ix_image_properties_image_id_name ON image_properties (image_id, name);
154
155=== added file 'glance/registry/db/migrate_repo/versions/006_sqlite_downgrade.sql'
156--- glance/registry/db/migrate_repo/versions/006_sqlite_downgrade.sql 1970-01-01 00:00:00 +0000
157+++ glance/registry/db/migrate_repo/versions/006_sqlite_downgrade.sql 2011-04-07 19:09:30 +0000
158@@ -0,0 +1,46 @@
159+/*
160+ * This is necessary because SQLite does not support
161+ * RENAME INDEX or ALTER TABLE CHANGE COLUMN.
162+ */
163+BEGIN TRANSACTION;
164+
165+CREATE TEMPORARY TABLE image_properties_backup (
166+ id INTEGER NOT NULL,
167+ image_id INTEGER NOT NULL,
168+ key VARCHAR(255) NOT NULL,
169+ value TEXT,
170+ created_at DATETIME NOT NULL,
171+ updated_at DATETIME,
172+ deleted_at DATETIME,
173+ deleted BOOLEAN NOT NULL,
174+ PRIMARY KEY (id)
175+);
176+
177+INSERT INTO image_properties_backup
178+SELECT id, image_id, name, value, created_at, updated_at, deleted_at, deleted
179+FROM image_properties;
180+
181+DROP TABLE image_properties;
182+
183+CREATE TABLE image_properties (
184+ id INTEGER NOT NULL,
185+ image_id INTEGER NOT NULL,
186+ key VARCHAR(255) NOT NULL,
187+ value TEXT,
188+ created_at DATETIME NOT NULL,
189+ updated_at DATETIME,
190+ deleted_at DATETIME,
191+ deleted BOOLEAN NOT NULL,
192+ PRIMARY KEY (id),
193+ CHECK (deleted IN (0, 1)),
194+ UNIQUE (image_id, key),
195+ FOREIGN KEY(image_id) REFERENCES images (id)
196+);
197+CREATE INDEX ix_image_properties_key ON image_properties (key);
198+
199+INSERT INTO image_properties (id, image_id, key, value, created_at, updated_at, deleted_at, deleted)
200+SELECT id, image_id, key, value, created_at, updated_at, deleted_at, deleted
201+FROM image_properties_backup;
202+
203+DROP TABLE image_properties_backup;
204+COMMIT;
205
206=== added file 'glance/registry/db/migrate_repo/versions/006_sqlite_upgrade.sql'
207--- glance/registry/db/migrate_repo/versions/006_sqlite_upgrade.sql 1970-01-01 00:00:00 +0000
208+++ glance/registry/db/migrate_repo/versions/006_sqlite_upgrade.sql 2011-04-07 19:09:30 +0000
209@@ -0,0 +1,46 @@
210+/*
211+ * This is necessary because SQLite does not support
212+ * RENAME INDEX or ALTER TABLE CHANGE COLUMN.
213+ */
214+BEGIN TRANSACTION;
215+
216+CREATE TEMPORARY TABLE image_properties_backup (
217+ id INTEGER NOT NULL,
218+ image_id INTEGER NOT NULL,
219+ name VARCHAR(255) NOT NULL,
220+ value TEXT,
221+ created_at DATETIME NOT NULL,
222+ updated_at DATETIME,
223+ deleted_at DATETIME,
224+ deleted BOOLEAN NOT NULL,
225+ PRIMARY KEY (id)
226+);
227+
228+INSERT INTO image_properties_backup
229+SELECT id, image_id, key, value, created_at, updated_at, deleted_at, deleted
230+FROM image_properties;
231+
232+DROP TABLE image_properties;
233+
234+CREATE TABLE image_properties (
235+ id INTEGER NOT NULL,
236+ image_id INTEGER NOT NULL,
237+ name VARCHAR(255) NOT NULL,
238+ value TEXT,
239+ created_at DATETIME NOT NULL,
240+ updated_at DATETIME,
241+ deleted_at DATETIME,
242+ deleted BOOLEAN NOT NULL,
243+ PRIMARY KEY (id),
244+ CHECK (deleted IN (0, 1)),
245+ UNIQUE (image_id, name),
246+ FOREIGN KEY(image_id) REFERENCES images (id)
247+);
248+CREATE INDEX ix_image_properties_name ON image_properties (name);
249+
250+INSERT INTO image_properties (id, image_id, name, value, created_at, updated_at, deleted_at, deleted)
251+SELECT id, image_id, name, value, created_at, updated_at, deleted_at, deleted
252+FROM image_properties_backup;
253+
254+DROP TABLE image_properties_backup;
255+COMMIT;
256
257=== modified file 'glance/registry/db/models.py'
258--- glance/registry/db/models.py 2011-03-31 14:43:28 +0000
259+++ glance/registry/db/models.py 2011-04-07 19:09:30 +0000
260@@ -110,13 +110,11 @@
261 class ImageProperty(BASE, ModelBase):
262 """Represents an image properties in the datastore"""
263 __tablename__ = 'image_properties'
264- __table_args__ = (UniqueConstraint('image_id', 'key'), {})
265+ __table_args__ = (UniqueConstraint('image_id', 'name'), {})
266
267 id = Column(Integer, primary_key=True)
268 image_id = Column(Integer, ForeignKey('images.id'), nullable=False)
269 image = relationship(Image, backref=backref('properties'))
270
271- # FIXME(sirp): KEY is a reserved word in SQL, might be a good idea to
272- # rename this column
273- key = Column(String(255), index=True, nullable=False)
274+ name = Column(String(255), index=True, nullable=False)
275 value = Column(Text)
276
277=== modified file 'glance/registry/server.py'
278--- glance/registry/server.py 2011-03-29 14:27:24 +0000
279+++ glance/registry/server.py 2011-04-07 19:09:30 +0000
280@@ -204,7 +204,7 @@
281 # TODO(sirp): should this be a dict, or a list of dicts?
282 # A plain dict is more convenient, but list of dicts would provide
283 # access to created_at, etc
284- properties = dict((p['key'], p['value'])
285+ properties = dict((p['name'], p['value'])
286 for p in image['properties'] if not p['deleted'])
287
288 image_dict = _fetch_attrs(image, db_api.IMAGE_ATTRS)
289
290=== modified file 'tests/stubs.py'
291--- tests/stubs.py 2011-04-04 14:32:14 +0000
292+++ tests/stubs.py 2011-04-07 19:09:30 +0000
293@@ -285,7 +285,7 @@
294 'checksum': None,
295 'size': 13,
296 'location': "swift://user:passwd@acct/container/obj.tar.0",
297- 'properties': [{'key': 'type',
298+ 'properties': [{'name': 'type',
299 'value': 'kernel',
300 'deleted': False}]},
301 {'id': 2,
302@@ -331,7 +331,7 @@
303 if 'properties' in values.keys():
304 for k, v in values['properties'].items():
305 p = {}
306- p['key'] = k
307+ p['name'] = k
308 p['value'] = v
309 p['deleted'] = False
310 p['created_at'] = datetime.datetime.utcnow()
311@@ -358,7 +358,7 @@
312 if 'properties' in values.keys():
313 for k, v in values['properties'].items():
314 p = {}
315- p['key'] = k
316+ p['name'] = k
317 p['value'] = v
318 p['deleted'] = False
319 p['created_at'] = datetime.datetime.utcnow()

Subscribers

People subscribed via source and target branches