[hooks/syncschema] drop / re-add unique together constraint around cardinality change

Related to #5560601.

authorAurelien Campeas <aurelien.campeas@pythonian.fr>
changeset32888a3f8d37
branchdefault
phasedraft
hiddenyes
parent revision#1310f34228b3 [hooks/syncschema] turn RdefUpdateOp into a data operation
child revision<not specified>
files modified by this revision
hooks/syncschema.py
# HG changeset patch
# User Aurelien Campeas <aurelien.campeas@pythonian.fr>
# Date 1435583353 -7200
# Mon Jun 29 15:09:13 2015 +0200
# Node ID 32888a3f8d37604be72105d8cd1ff33645570112
# Parent 1310f34228b36e04f6a2715d6663b680488f6923
[hooks/syncschema] drop / re-add unique together constraint around cardinality change

Related to #5560601.

diff --git a/hooks/syncschema.py b/hooks/syncschema.py
@@ -589,10 +589,20 @@
1          rdef.name = str(rdef.rtype)
2          if rdef.subject not in ETYPE_NAME_MAP and rdef.object not in ETYPE_NAME_MAP:
3              self.cnx.vreg.schema.add_relation_def(rdef)
4 
5 
6 +class RdefCardinalityUpdateOp(hook.DataOperationMixIn, hook.Operation):
7 +
8 +    def precommit_event(self):
9 +        cnx = self.cnx
10 +        print 'HANDLE CARDINALITY'
11 +        for rdef in self.get_data():
12 +            print 'RDEF', rdef
13 +            cnx.repo.system_source.update_rdef_null_allowed(cnx, rdef)
14 +
15 +
16  class RDefUpdateOp(hook.DataOperationMixIn, hook.Operation):
17      """actually update some properties of a relation definition"""
18 
19      def precommit_event(self):
20          cnx = self.cnx
@@ -615,11 +625,13 @@
21                  values['cardinality'][0] != oldvalues['cardinality'][0]):
22                  op = RdefRevertOp.get_instance(cnx)
23                  op.add_data(((rdef, oldvalues),
24                               lambda rdef, values: rdef.update(values),
25                               lambda rdef, _: syssource.update_rdef_null_allowed(cnx, rdef)))
26 -                syssource.update_rdef_null_allowed(cnx, rdef)
27 +                update_or_schedule_update_of_the_store(cnx, rdef, utc_using(cnx, rdef),
28 +                                                       RdefCardinalityUpdateOp,
29 +                                                       lambda: syssource.update_rdef_null_allowed(cnx, rdef))
30              if 'fulltextindexed' in values:
31                  UpdateFTIndexOp.get_instance(cnx).add_data(rdef.subject)
32 
33          MemSchemaOperation(cnx)
34 
@@ -636,11 +648,11 @@
35  def utc_using(cnx, rdefentity):
36      return cnx.execute('CWUniqueTogetherConstraint C WHERE C constraint_of ET, '
37                         'EXISTS(C relations RT, A relation_type RT), '
38                         'A from_entity ET, A eid %(a)s', {'a': rdefentity.eid})
39 
40 -def update_or_schedule_update_of_the_store(cnx, rdef, utcrset):
41 +def update_or_schedule_update_of_the_store(cnx, rdef, utcrset, operation, immediateaction):
42      if utcrset:# and cnx.repo.system_source.dbdriver.startswith('sqlserver'):
43          schema = cnx.vreg.schema
44          utcadd = CWUniqueTogetherConstraintAddOp.get_instance(cnx)
45          utcdel = CWUniqueTogetherConstraintDelOp.get_instance(cnx)
46          for utcentity in utcrset.entities():
@@ -649,13 +661,13 @@
47                  continue
48              utcadd.add_data(utcentity)
49              utcdel.add_data((schema.schema_by_eid(utcentity.constraint_of[0].eid),
50                               utcentity.name,
51                               tuple(r.name for r in utcentity.relations)))
52 -        CWSizeConstraintAddOp.get_instance(cnx).add_data(rdef)
53 +        operation.get_instance(cnx).add_data(rdef)
54      else:
55 -        cnx.repo.system_source.update_rdef_column(cnx, rdef)
56 +        immediateaction()
57 
58 
59  class CWSizeConstraintAddOp(hook.DataOperationMixIn, hook.Operation):
60 
61      def precommit_event(self):
@@ -701,11 +713,13 @@
62                                            'ename': rdef.subject.type}).one()
63                  op = RdefRevertOp.get_instance(cnx)
64                  op.add_data((rdef,
65                               lambda rdef: rdef.constraints.add(cstr),
66                               lambda rdef: syssource.update_rdef_column(cnx, rdef)))
67 -                update_or_schedule_update_of_the_store(cnx, rdef, utc_using(cnx, rdefentity))
68 +                update_or_schedule_update_of_the_store(cnx, rdef, utc_using(cnx, rdefentity),
69 +                                                       CWSizeConstraintAddOp,
70 +                                                       lambda: syssource.update_rdef_column(cnx, rdef))
71 
72              # in-place modification of in-memory schema comes
73              # *after* the size constraint handling (the information is needed
74              # to compute the right attribute sql type, if this was a mere modification
75              # of the maxsize)
@@ -762,11 +776,13 @@
76                               lambda rdef: rdef.constraints.remove(newcstr),
77                               lambda _: None))
78                  op.add_data((rdef,
79                               lambda rdef: rdef.constraints.append(oldcstr),
80                               lambda rdef: syssource.update_rdef_column(cnx, rdef)))
81 -                update_or_schedule_update_of_the_store(cnx, rdef, utc_using(cnx, rdefentity))
82 +                update_or_schedule_update_of_the_store(cnx, rdef, utc_using(cnx, rdefentity),
83 +                                                       CWSizeConstraintAddOp,
84 +                                                       lambda: syssource.update_rdef_column(cnx, rdef))
85              elif cstrtype == 'UniqueConstraint' and oldcstr is None:
86                  op = RdefRevertOp.get_instance(cnx)
87                  op.add_data((rdef,
88                               lambda rdef: rdef.constraints.remove(newcstr),
89                               lambda _: None))