From 38e7ad70e09843e6114ff1223a59bdd50efd9aa4 Mon Sep 17 00:00:00 2001 From: Oleksii Chuprykov Date: Fri, 10 Oct 2014 16:12:13 +0300 Subject: [PATCH] Alter models and add migration We must have correct models i.e. models that correspond database schema to use sqlalchemy features. Update models.py and add migration script to correct database schema and get rid of the difference between schema and models. Remove Stack.user_creds_id nullable=False parameter because we can soft-delete stack, that leads to deletion user_credits and setting stack.user_creds_id to null (by cascade). Remove resource.id default value(in database) as specified in models. Since we will have sync check between models and migrations we need to add missing indexes to models for consistency. Partially implements bp: db-sync-models-with-migrations Change-Id: Iabc4aced0a95e29cea9c48abc4c31be98e8ff9b1 --- ...48_resource_id_server_default_none_psql.py | 34 +++++++++++++++++++ heat/db/sqlalchemy/models.py | 17 +++++----- 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 heat/db/sqlalchemy/migrate_repo/versions/048_resource_id_server_default_none_psql.py diff --git a/heat/db/sqlalchemy/migrate_repo/versions/048_resource_id_server_default_none_psql.py b/heat/db/sqlalchemy/migrate_repo/versions/048_resource_id_server_default_none_psql.py new file mode 100644 index 000000000..6bfd0ffce --- /dev/null +++ b/heat/db/sqlalchemy/migrate_repo/versions/048_resource_id_server_default_none_psql.py @@ -0,0 +1,34 @@ +# +# 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. + +from sqlalchemy import MetaData, Sequence, Table + + +def upgrade(migrate_engine): + meta = MetaData() + meta.bind = migrate_engine + meta.reflect(meta.bind) + + if migrate_engine.name == 'postgresql': + resource = Table('resource', meta) + resource.c.id.alter(server_default=None) + + +def downgrade(migrate_engine): + meta = MetaData() + meta.bind = migrate_engine + + if migrate_engine.name == 'postgresql': + resource = Table('resource', meta, autoload=True) + resource.c.id.alter(server_default=Sequence('resource_id_seq') + .next_value()) diff --git a/heat/db/sqlalchemy/models.py b/heat/db/sqlalchemy/models.py index 7c66ae7ce..e747128b5 100644 --- a/heat/db/sqlalchemy/models.py +++ b/heat/db/sqlalchemy/models.py @@ -127,8 +127,7 @@ class Stack(BASE, HeatBase, SoftDelete, StateAware): parameters = sqlalchemy.Column('parameters', Json) user_creds_id = sqlalchemy.Column( sqlalchemy.Integer, - sqlalchemy.ForeignKey('user_creds.id'), - nullable=False) + sqlalchemy.ForeignKey('user_creds.id')) owner_id = sqlalchemy.Column(sqlalchemy.String(36), nullable=True) timeout = sqlalchemy.Column(sqlalchemy.Integer) disable_rollback = sqlalchemy.Column(sqlalchemy.Boolean, nullable=False) @@ -167,7 +166,7 @@ class UserCreds(BASE, HeatBase): password = sqlalchemy.Column(sqlalchemy.String(255)) decrypt_method = sqlalchemy.Column(sqlalchemy.String(64)) tenant = sqlalchemy.Column(sqlalchemy.String(1024)) - auth_url = sqlalchemy.Column(sqlalchemy.String) + auth_url = sqlalchemy.Column(sqlalchemy.Text) tenant_id = sqlalchemy.Column(sqlalchemy.String(256)) trust_id = sqlalchemy.Column(sqlalchemy.String(255)) trustor_user_id = sqlalchemy.Column(sqlalchemy.String(64)) @@ -216,7 +215,7 @@ class ResourceData(BASE, HeatBase): primary_key=True, nullable=False) key = sqlalchemy.Column('key', sqlalchemy.String(255)) - value = sqlalchemy.Column('value', sqlalchemy.String) + value = sqlalchemy.Column('value', sqlalchemy.Text) redact = sqlalchemy.Column('redact', sqlalchemy.Boolean) decrypt_method = sqlalchemy.Column(sqlalchemy.String(64)) resource_id = sqlalchemy.Column('resource_id', @@ -301,7 +300,7 @@ class SoftwareConfig(BASE, HeatBase): group = sqlalchemy.Column('group', sqlalchemy.String(255)) config = sqlalchemy.Column('config', Json) tenant = sqlalchemy.Column( - 'tenant', sqlalchemy.String(64), nullable=False) + 'tenant', sqlalchemy.String(64), nullable=False, index=True) class SoftwareDeployment(BASE, HeatBase, StateAware): @@ -311,6 +310,8 @@ class SoftwareDeployment(BASE, HeatBase, StateAware): """ __tablename__ = 'software_deployment' + __table_args__ = ( + sqlalchemy.Index('ix_software_deployment_created_at', 'created_at'),) id = sqlalchemy.Column('id', sqlalchemy.String(36), primary_key=True, default=lambda: str(uuid.uuid4())) @@ -321,11 +322,11 @@ class SoftwareDeployment(BASE, HeatBase, StateAware): nullable=False) config = relationship(SoftwareConfig, backref=backref('deployments')) server_id = sqlalchemy.Column('server_id', sqlalchemy.String(36), - nullable=False) + nullable=False, index=True) input_values = sqlalchemy.Column('input_values', Json) output_values = sqlalchemy.Column('output_values', Json) tenant = sqlalchemy.Column( - 'tenant', sqlalchemy.String(64), nullable=False) + 'tenant', sqlalchemy.String(64), nullable=False, index=True) stack_user_project_id = sqlalchemy.Column(sqlalchemy.String(64), nullable=True) @@ -342,7 +343,7 @@ class Snapshot(BASE, HeatBase): name = sqlalchemy.Column('name', sqlalchemy.String(255), nullable=True) data = sqlalchemy.Column('data', Json) tenant = sqlalchemy.Column( - 'tenant', sqlalchemy.String(64), nullable=False) + 'tenant', sqlalchemy.String(64), nullable=False, index=True) status = sqlalchemy.Column('status', sqlalchemy.String(255)) status_reason = sqlalchemy.Column('status_reason', sqlalchemy.String(255)) stack = relationship(Stack, backref=backref('snapshot'))