Store files in the raw_template table

With the get_files intrinsic function, the files data will need to be
available at any time. This change adds a files column to the raw_template
table and saves/loads files from Template class.

Change-Id: I9fd705326ecfe7ca9e00624c67e7dd1965f95a96
This commit is contained in:
Steve Baker 2014-01-18 16:02:19 +13:00
parent a3e9eb075a
commit c640072a4f
5 changed files with 44 additions and 2 deletions

View File

@ -0,0 +1,33 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sqlalchemy
from heat.db.sqlalchemy.types import Json
def upgrade(migrate_engine):
meta = sqlalchemy.MetaData()
meta.bind = migrate_engine
raw_template = sqlalchemy.Table('raw_template', meta, autoload=True)
files = sqlalchemy.Column('files', Json, default='{}')
files.create(raw_template)
def downgrade(migrate_engine):
meta = sqlalchemy.MetaData()
meta.bind = migrate_engine
raw_template = sqlalchemy.Table('raw_template', meta, autoload=True)
raw_template.c.files.drop()

View File

@ -87,6 +87,7 @@ class RawTemplate(BASE, HeatBase):
__tablename__ = 'raw_template'
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
template = sqlalchemy.Column(Json)
files = sqlalchemy.Column(Json)
class Stack(BASE, HeatBase, SoftDelete):

View File

@ -57,12 +57,15 @@ class Template(collections.Mapping):
def load(cls, context, template_id):
'''Retrieve a Template with the given ID from the database.'''
t = db_api.raw_template_get(context, template_id)
return cls(t.template, template_id)
return cls(t.template, template_id=template_id, files=t.files)
def store(self, context=None):
'''Store the Template in the database and return its ID.'''
if self.id is None:
rt = {'template': self.t}
rt = {
'template': self.t,
'files': self.files
}
new_rt = db_api.raw_template_create(context, rt)
self.id = new_rt.id
return self.id

View File

@ -172,3 +172,6 @@ class TestHeatMigrations(test_migrations.BaseMigrationTestCase,
self.assertColumnExists(engine, 'stack_lock', 'engine_id')
self.assertColumnExists(engine, 'stack_lock', 'created_at')
self.assertColumnExists(engine, 'stack_lock', 'updated_at')
def _check_034(self, engine, data):
self.assertColumnExists(engine, 'raw_template', 'files')

View File

@ -755,6 +755,7 @@ def create_raw_template(context, **kwargs):
t = template_format.parse(wp_template)
template = {
'template': t,
'files': {'foo': 'bar'}
}
template.update(kwargs)
return db_api.raw_template_create(context, template)
@ -856,6 +857,7 @@ class DBAPIRawTemplateTest(HeatTestCase):
tp = create_raw_template(self.ctx, template=t)
self.assertIsNotNone(tp.id)
self.assertEqual(t, tp.template)
self.assertEqual({'foo': 'bar'}, tp.files)
def test_raw_template_get(self):
t = template_format.parse(wp_template)