OpenStack Image Management (Glance)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

003_add_disk_format.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2011 OpenStack Foundation
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from sqlalchemy import * # noqa
  16. from glance.db.sqlalchemy.migrate_repo.schema import (
  17. Boolean, DateTime, Integer, String, Text, from_migration_import) # noqa
  18. def get_images_table(meta):
  19. """
  20. Returns the Table object for the images table that
  21. corresponds to the images table definition of this version.
  22. """
  23. images = Table('images',
  24. meta,
  25. Column('id', Integer(), primary_key=True, nullable=False),
  26. Column('name', String(255)),
  27. Column('disk_format', String(20)),
  28. Column('container_format', String(20)),
  29. Column('size', Integer()),
  30. Column('status', String(30), nullable=False),
  31. Column('is_public',
  32. Boolean(),
  33. nullable=False,
  34. default=False,
  35. index=True),
  36. Column('location', Text()),
  37. Column('created_at', DateTime(), nullable=False),
  38. Column('updated_at', DateTime()),
  39. Column('deleted_at', DateTime()),
  40. Column('deleted',
  41. Boolean(),
  42. nullable=False,
  43. default=False,
  44. index=True),
  45. mysql_engine='InnoDB',
  46. extend_existing=True)
  47. return images
  48. def upgrade(migrate_engine):
  49. meta = MetaData()
  50. meta.bind = migrate_engine
  51. (define_images_table,) = from_migration_import(
  52. '001_add_images_table', ['define_images_table'])
  53. (define_image_properties_table,) = from_migration_import(
  54. '002_add_image_properties_table', ['define_image_properties_table'])
  55. conn = migrate_engine.connect()
  56. images = define_images_table(meta)
  57. image_properties = define_image_properties_table(meta)
  58. # Steps to take, in this order:
  59. # 1) Move the existing type column from Image into
  60. # ImageProperty for all image records that have a non-NULL
  61. # type column
  62. # 2) Drop the type column in images
  63. # 3) Add the new columns to images
  64. # The below wackiness correlates to the following ANSI SQL:
  65. # SELECT images.* FROM images
  66. # LEFT JOIN image_properties
  67. # ON images.id = image_properties.image_id
  68. # AND image_properties.key = 'type'
  69. # WHERE image_properties.image_id IS NULL
  70. # AND images.type IS NOT NULL
  71. #
  72. # which returns all the images that have a type set
  73. # but that DO NOT yet have an image_property record
  74. # with key of type.
  75. from_stmt = [
  76. images.outerjoin(image_properties,
  77. and_(images.c.id == image_properties.c.image_id,
  78. image_properties.c.key == 'type'))
  79. ]
  80. and_stmt = and_(image_properties.c.image_id == None,
  81. images.c.type != None)
  82. sel = select([images], from_obj=from_stmt).where(and_stmt)
  83. image_records = conn.execute(sel).fetchall()
  84. property_insert = image_properties.insert()
  85. for record in image_records:
  86. conn.execute(property_insert,
  87. image_id=record.id,
  88. key='type',
  89. created_at=record.created_at,
  90. deleted=False,
  91. value=record.type)
  92. conn.close()
  93. disk_format = Column('disk_format', String(20))
  94. disk_format.create(images)
  95. container_format = Column('container_format', String(20))
  96. container_format.create(images)
  97. images.columns['type'].drop()