Drop indexes only if they exist (closes #167781).

This cset follows from two needs:

  • sometimes the database does not know an index (coherency issue, see https://www.cubicweb.org/ticket/6171521)
  • the new handling of sync-schema hooks to please sqlserver has a side effect of sometimes generating two index removals in a row
authorAurelien Campeas <aurelien.campeas@pythonian.fr>
changeset65e98f57d604
branchdefault
phasepublic
hiddenno
parent revision#1226d931a3d9 Added tag 1.13.3, debian/1.13.3-1, centos/1.13.3-1 for changeset c1948986afb0
child revision#83314451efa5 [sqlserver] Fix `index_exists` predicate for unique indexes
files modified by this revision
__init__.py
sqlserver2005.py
# HG changeset patch
# User Aurelien Campeas <aurelien.campeas@pythonian.fr>
# Date 1440580110 -7200
# Wed Aug 26 11:08:30 2015 +0200
# Node ID 65e98f57d6041b94e5f11b7e660596ed4ee1f9a9
# Parent 1226d931a3d927807067d7576a957dec94659add
Drop indexes only if they exist (closes #167781).

This cset follows from two needs:

* sometimes the database does not know an index
(coherency issue, see https://www.cubicweb.org/ticket/6171521)

* the new handling of sync-schema hooks to please sqlserver
has a side effect of sometimes generating two index removals
in a row

diff --git a/__init__.py b/__init__.py
@@ -891,11 +891,11 @@
1      def sql_drop_index(self, table, column, unique=False):
2          idx = self._index_name(table, column, unique)
3          if unique:
4              return 'ALTER TABLE %s DROP CONSTRAINT %s' % (table, idx)
5          else:
6 -            return 'DROP INDEX %s' % idx
7 +            return 'DROP INDEX IF EXISTS %s' % idx
8 
9      def sqls_create_multicol_unique_index(self, table, columns, indexname=None):
10          columns = sorted(columns)
11          if indexname is None:
12              warn('You should provide an explicit index name else you risk '
@@ -911,11 +911,11 @@
13          if indexname is None:
14              warn('You should provide an explicit index name else you risk '
15                   'a silent truncation of the computed index name.',
16                   DeprecationWarning)
17              indexname = 'unique_%s_%s_idx' % (table, '_'.join(columns))
18 -        sql = 'DROP INDEX %s;' % (indexname.lower())
19 +        sql = 'DROP INDEX IF EXISTS %s;' % (indexname.lower())
20          return [sql]
21 
22      # sequence protocol
23 
24      def sql_create_sequence(self, seq_name):
diff --git a/sqlserver2005.py b/sqlserver2005.py
@@ -258,11 +258,12 @@
25              warn('You should provide an explicit index name else you risk '
26                   'a silent truncation of the computed index name.',
27                   DeprecationWarning)
28          columns = sorted(columns)
29          view = 'utv_%s_%s' % (table, indexname or '_'.join(columns))
30 -        sql = 'DROP VIEW %s' % (view.lower()) # also drops the index
31 +        sql = ("IF OBJECT_ID ('%(v)s', 'V') IS NOT NULL DROP VIEW %(v)s"
32 +               % {'v': view.lower()}) # also drops the index
33          return [sql]
34 
35      def sql_drop_index(self, table, column, unique=False):
36          if unique:
37              return super(_SqlServer2005FuncHelper, self).sql_drop_index(table, column, unique)
obsoletes