Merge "Drop foreign key contraints of alarm in sqlalchemy"
This commit is contained in:
@@ -312,8 +312,6 @@ class Connection(base.Connection):
|
|||||||
query = session.query(models.User).filter(
|
query = session.query(models.User).filter(
|
||||||
~models.User.id.in_(session.query(models.Meter.user_id)
|
~models.User.id.in_(session.query(models.Meter.user_id)
|
||||||
.group_by(models.Meter.user_id)),
|
.group_by(models.Meter.user_id)),
|
||||||
~models.User.id.in_(session.query(models.Alarm.user_id)
|
|
||||||
.group_by(models.Alarm.user_id)),
|
|
||||||
~models.User.id.in_(session.query(models.AlarmChange.user_id)
|
~models.User.id.in_(session.query(models.AlarmChange.user_id)
|
||||||
.group_by(models.AlarmChange.user_id))
|
.group_by(models.AlarmChange.user_id))
|
||||||
)
|
)
|
||||||
@@ -324,9 +322,6 @@ class Connection(base.Connection):
|
|||||||
.filter(~models.Project.id.in_(
|
.filter(~models.Project.id.in_(
|
||||||
session.query(models.Meter.project_id)
|
session.query(models.Meter.project_id)
|
||||||
.group_by(models.Meter.project_id)),
|
.group_by(models.Meter.project_id)),
|
||||||
~models.Project.id.in_(
|
|
||||||
session.query(models.Alarm.project_id)
|
|
||||||
.group_by(models.Alarm.project_id)),
|
|
||||||
~models.Project.id.in_(
|
~models.Project.id.in_(
|
||||||
session.query(models.AlarmChange.project_id)
|
session.query(models.AlarmChange.project_id)
|
||||||
.group_by(models.AlarmChange.project_id)),
|
.group_by(models.AlarmChange.project_id)),
|
||||||
@@ -729,10 +724,6 @@ class Connection(base.Connection):
|
|||||||
"""
|
"""
|
||||||
session = sqlalchemy_session.get_session()
|
session = sqlalchemy_session.get_session()
|
||||||
with session.begin():
|
with session.begin():
|
||||||
Connection._create_or_update(session, models.User,
|
|
||||||
alarm.user_id)
|
|
||||||
Connection._create_or_update(session, models.Project,
|
|
||||||
alarm.project_id)
|
|
||||||
alarm_row = models.Alarm(id=alarm.alarm_id)
|
alarm_row = models.Alarm(id=alarm.alarm_id)
|
||||||
alarm_row.update(alarm.as_dict())
|
alarm_row.update(alarm.as_dict())
|
||||||
session.add(alarm_row)
|
session.add(alarm_row)
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright © 2014 Intel Crop.
|
||||||
|
#
|
||||||
|
# Author: Lianhao Lu <lianhao.lu@intel.com>
|
||||||
|
#
|
||||||
|
# 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 migrate import ForeignKeyConstraint
|
||||||
|
from sqlalchemy import MetaData, Table
|
||||||
|
from sqlalchemy.sql.expression import select
|
||||||
|
|
||||||
|
from ceilometer.openstack.common.db.sqlalchemy import utils
|
||||||
|
|
||||||
|
TABLES = ['user', 'project', 'alarm']
|
||||||
|
|
||||||
|
INDEXES = {
|
||||||
|
"alarm": (('user_id', 'user', 'id'),
|
||||||
|
('project_id', 'project', 'id')),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade(migrate_engine):
|
||||||
|
if migrate_engine.name == 'sqlite':
|
||||||
|
return
|
||||||
|
meta = MetaData(bind=migrate_engine)
|
||||||
|
load_tables = dict((table_name, Table(table_name, meta, autoload=True))
|
||||||
|
for table_name in TABLES)
|
||||||
|
for table_name, indexes in INDEXES.items():
|
||||||
|
table = load_tables[table_name]
|
||||||
|
for column, ref_table_name, ref_column_name in indexes:
|
||||||
|
ref_table = load_tables[ref_table_name]
|
||||||
|
params = {'columns': [table.c[column]],
|
||||||
|
'refcolumns': [ref_table.c[ref_column_name]]}
|
||||||
|
if migrate_engine.name == 'mysql':
|
||||||
|
params['name'] = "_".join(('fk', table_name, column))
|
||||||
|
fkey = ForeignKeyConstraint(**params)
|
||||||
|
fkey.drop()
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade(migrate_engine):
|
||||||
|
if migrate_engine.name == 'sqlite':
|
||||||
|
return
|
||||||
|
meta = MetaData(bind=migrate_engine)
|
||||||
|
load_tables = dict((table_name, Table(table_name, meta, autoload=True))
|
||||||
|
for table_name in TABLES)
|
||||||
|
for table_name, indexes in INDEXES.items():
|
||||||
|
table = load_tables[table_name]
|
||||||
|
# Save data that conflicted with FK.
|
||||||
|
columns = [column.copy() for column in table.columns]
|
||||||
|
table_dump = Table('dump027_' + table_name, meta, *columns)
|
||||||
|
table_dump.create()
|
||||||
|
for column, ref_table_name, ref_column_name in indexes:
|
||||||
|
ref_table = load_tables[ref_table_name]
|
||||||
|
subq = select([getattr(ref_table.c, ref_column_name)])
|
||||||
|
sql = utils.InsertFromSelect(table_dump, table.select().where(
|
||||||
|
~ getattr(table.c, column).in_(subq)))
|
||||||
|
sql_del = table.delete().where(
|
||||||
|
~ getattr(table.c, column).in_(subq))
|
||||||
|
migrate_engine.execute(sql)
|
||||||
|
migrate_engine.execute(sql_del)
|
||||||
|
|
||||||
|
params = {'columns': [table.c[column]],
|
||||||
|
'refcolumns': [ref_table.c[ref_column_name]]}
|
||||||
|
if migrate_engine.name == 'mysql':
|
||||||
|
params['name'] = "_".join(('fk', table_name, column))
|
||||||
|
fkey = ForeignKeyConstraint(**params)
|
||||||
|
fkey.create()
|
||||||
@@ -267,8 +267,8 @@ class Alarm(Base):
|
|||||||
description = Column(Text)
|
description = Column(Text)
|
||||||
timestamp = Column(PreciseTimestamp, default=timeutils.utcnow)
|
timestamp = Column(PreciseTimestamp, default=timeutils.utcnow)
|
||||||
|
|
||||||
user_id = Column(String(255), ForeignKey('user.id'))
|
user_id = Column(String(255))
|
||||||
project_id = Column(String(255), ForeignKey('project.id'))
|
project_id = Column(String(255))
|
||||||
|
|
||||||
state = Column(String(255))
|
state = Column(String(255))
|
||||||
state_timestamp = Column(PreciseTimestamp, default=timeutils.utcnow)
|
state_timestamp = Column(PreciseTimestamp, default=timeutils.utcnow)
|
||||||
|
|||||||
@@ -695,16 +695,15 @@ class RawSampleTest(DBTestBase,
|
|||||||
self.conn.create_alarm(alarm)
|
self.conn.create_alarm(alarm)
|
||||||
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
|
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
|
||||||
self.conn.clear_expired_metering_data(5)
|
self.conn.clear_expired_metering_data(5)
|
||||||
# user and project with Alarms associated with it aren't deleted.
|
|
||||||
f = storage.SampleFilter(meter='instance')
|
f = storage.SampleFilter(meter='instance')
|
||||||
results = list(self.conn.get_samples(f))
|
results = list(self.conn.get_samples(f))
|
||||||
self.assertEqual(len(results), 2)
|
self.assertEqual(len(results), 2)
|
||||||
results = list(self.conn.get_users())
|
results = list(self.conn.get_users())
|
||||||
self.assertEqual(len(results), 3)
|
self.assertEqual(len(results), 2)
|
||||||
self.assertIn('user-id', results)
|
self.assertNotIn('user-id', results)
|
||||||
results = list(self.conn.get_projects())
|
results = list(self.conn.get_projects())
|
||||||
self.assertEqual(len(results), 3)
|
self.assertEqual(len(results), 2)
|
||||||
self.assertIn('project-id', results)
|
self.assertNotIn('project-id', results)
|
||||||
results = list(self.conn.get_resources())
|
results = list(self.conn.get_resources())
|
||||||
self.assertEqual(len(results), 2)
|
self.assertEqual(len(results), 2)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user