summaryrefslogtreecommitdiffstats
path: root/openstack/common
diff options
context:
space:
mode:
authorVictor Sergeyev <vsergeyev@mirantis.com>2013-07-17 12:39:37 +0300
committerVictor Sergeyev <vsergeyev@mirantis.com>2013-07-26 10:36:15 +0300
commitc76be5bbfe1064e5bb351876e9915ec4cc282236 (patch)
tree204ceec3f91ce27d9d77a3af21fb65cc4484ed5f /openstack/common
parentaafc227283042592b560b0a38d0ea7c39b402c50 (diff)
downloadoslo-c76be5bbfe1064e5bb351876e9915ec4cc282236.tar.gz
oslo-c76be5bbfe1064e5bb351876e9915ec4cc282236.tar.xz
oslo-c76be5bbfe1064e5bb351876e9915ec4cc282236.zip
Add function drop_unique_constraint()
We forgot to copy this function from nova, so we are not able to switch to oslo sqlalchemy.utils until add this function. Sqlalchemy doesn't supports some sqlite column types so we have no possibility to drop unique constraint in general way on sqlite (we loose these column types after migration). Added unified drop_unique_constraint() function to drop unique constraint for sql backends with tests. blueprint: oslo-sqlalchemy-utils Change-Id: I34000ad2277a97c31a29539d047faaf19876c9d5
Diffstat (limited to 'openstack/common')
-rwxr-xr-xopenstack/common/db/sqlalchemy/utils.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/openstack/common/db/sqlalchemy/utils.py b/openstack/common/db/sqlalchemy/utils.py
index 3ff7bdb..caf5569 100755
--- a/openstack/common/db/sqlalchemy/utils.py
+++ b/openstack/common/db/sqlalchemy/utils.py
@@ -18,6 +18,7 @@
# License for the specific language governing permissions and limitations
# under the License.
+from migrate.changeset import UniqueConstraint
import sqlalchemy
from sqlalchemy import Boolean
from sqlalchemy import CheckConstraint
@@ -191,6 +192,43 @@ def _get_not_supported_column(col_name_col_instance, column_name):
return column
+def drop_unique_constraint(migrate_engine, table_name, uc_name, *columns,
+ **col_name_col_instance):
+ """Drop unique constraint from table.
+
+ This method drops UC from table and works for mysql, postgresql and sqlite.
+ In mysql and postgresql we are able to use "alter table" construction.
+ Sqlalchemy doesn't support some sqlite column types and replaces their
+ type with NullType in metadata. We process these columns and replace
+ NullType with the correct column type.
+
+ :param migrate_engine: sqlalchemy engine
+ :param table_name: name of table that contains uniq constraint.
+ :param uc_name: name of uniq constraint that will be dropped.
+ :param columns: columns that are in uniq constraint.
+ :param col_name_col_instance: contains pair column_name=column_instance.
+ column_instance is instance of Column. These params
+ are required only for columns that have unsupported
+ types by sqlite. For example BigInteger.
+ """
+
+ meta = MetaData()
+ meta.bind = migrate_engine
+ t = Table(table_name, meta, autoload=True)
+
+ if migrate_engine.name == "sqlite":
+ override_cols = [
+ _get_not_supported_column(col_name_col_instance, col.name)
+ for col in t.columns
+ if isinstance(col.type, NullType)
+ ]
+ for col in override_cols:
+ t.columns.replace(col)
+
+ uc = UniqueConstraint(*columns, table=t, name=uc_name)
+ uc.drop()
+
+
def drop_old_duplicate_entries_from_table(migrate_engine, table_name,
use_soft_delete, *uc_column_names):
"""Drop all old rows having the same values for columns in uc_columns.