server/migractions.py
branchstable
changeset 6208 07b176640a8c
parent 6205 41e0e13e10b7
child 6217 e2aeb40d5983
equal deleted inserted replaced
6207:c3f11fadf9ed 6208:07b176640a8c
   475         its current definition:
   475         its current definition:
   476 
   476 
   477         * description
   477         * description
   478         * internationalizable, fulltextindexed, indexed, meta
   478         * internationalizable, fulltextindexed, indexed, meta
   479         * relations from/to this entity
   479         * relations from/to this entity
       
   480         * __unique_together__
   480         * permissions if `syncperms`
   481         * permissions if `syncperms`
   481         """
   482         """
   482         etype = str(etype)
   483         etype = str(etype)
   483         if etype in self._synchronized:
   484         if etype in self._synchronized:
   484             return
   485             return
   522                     for obj in objtypes:
   523                     for obj in objtypes:
   523                         if (subj, obj) not in reporschema.rdefs:
   524                         if (subj, obj) not in reporschema.rdefs:
   524                             continue
   525                             continue
   525                         self._synchronize_rdef_schema(subj, rschema, obj,
   526                         self._synchronize_rdef_schema(subj, rschema, obj,
   526                                                       syncprops=syncprops, syncperms=syncperms)
   527                                                       syncprops=syncprops, syncperms=syncperms)
       
   528         if syncprops: # need to process __unique_together__ after rdefs were processed
       
   529             repo_unique_together = set([frozenset(ut)
       
   530                                         for ut in repoeschema._unique_together])
       
   531             unique_together = set([frozenset(ut)
       
   532                                    for ut in eschema._unique_together])
       
   533             for ut in repo_unique_together - unique_together:
       
   534                 restrictions  = ', '.join(['C relations R%(i)d, '
       
   535                                            'R%(i)d relation_type T%(i)d, '
       
   536                                            'R%(i)d from_entity X, '
       
   537                                            'T%(i)d name %%(T%(i)d)s' % {'i': i,
       
   538                                                                         'col':col}
       
   539                                            for (i, col) in enumerate(ut)])
       
   540                 substs = {'etype': etype}
       
   541                 for i, col in enumerate(ut):
       
   542                     substs['T%d'%i] = col
       
   543                 self.rqlexec('DELETE CWUniqueTogetherConstraint C '
       
   544                              'WHERE C constraint_of E, '
       
   545                              '      E name %%(etype)s,'
       
   546                              '      %s' % restrictions,
       
   547                              substs)
       
   548             for ut in unique_together - repo_unique_together:
       
   549                 relations = ', '.join(['C relations R%d' % i
       
   550                                        for (i, col) in enumerate(ut)])
       
   551                 restrictions  = ', '.join(['R%(i)d relation_type T%(i)d, '
       
   552                                            'R%(i)d from_entity E, '
       
   553                                            'T%(i)d name %%(T%(i)d)s' % {'i': i,
       
   554                                                                         'col':col}
       
   555                                            for (i, col) in enumerate(ut)])
       
   556                 substs = {'etype': etype}
       
   557                 for i, col in enumerate(ut):
       
   558                     substs['T%d'%i] = col
       
   559                 self.rqlexec('INSERT CWUniqueTogetherConstraint C:'
       
   560                              '       C constraint_of E, '
       
   561                              '       %s '
       
   562                              'WHERE '
       
   563                              '      E name %%(etype)s,'
       
   564                              '      %s' % (relations, restrictions),
       
   565                              substs)
   527 
   566 
   528     def _synchronize_rdef_schema(self, subjtype, rtype, objtype,
   567     def _synchronize_rdef_schema(self, subjtype, rtype, objtype,
   529                                  syncperms=True, syncprops=True):
   568                                  syncperms=True, syncprops=True):
   530         """synchronize properties of the persistent relation definition schema
   569         """synchronize properties of the persistent relation definition schema
   531         against its current definition:
   570         against its current definition:
  1164                 # cleanup unused constraints
  1203                 # cleanup unused constraints
  1165                 self.rqlexec('DELETE CWConstraint C WHERE NOT X constrained_by C')
  1204                 self.rqlexec('DELETE CWConstraint C WHERE NOT X constrained_by C')
  1166         if commit:
  1205         if commit:
  1167             self.commit()
  1206             self.commit()
  1168 
  1207 
  1169     def cmd_add_unique_together_attrs(self, etype, attrlist, commit=True):
       
  1170         """
       
  1171         Add a (sql) UNIQUE index on all the underlying columns for the
       
  1172         attributes listed in attrlist. That list can also contain
       
  1173         inlined relations.
       
  1174         """
       
  1175         prefix = SQL_PREFIX
       
  1176         dbhelper = self.repo.system_source.dbhelper
       
  1177         cols  = ['%s%s' % (prefix, col) for col in attrlist]
       
  1178         table = '%s%s' % (prefix, etype)
       
  1179         sql = dbhelper.sql_create_multicol_unique_index(table, cols)
       
  1180         self.sqlexec(sql, ask_confirm=False)
       
  1181         if commit:
       
  1182             self.commit()
       
  1183 
       
  1184     def cmd_drop_unique_together_attrs(self, etype, attrlist, commit=True):
       
  1185         """
       
  1186         remove a UNIQUE index created with add_unique_together_attrs
       
  1187         """
       
  1188         prefix = SQL_PREFIX
       
  1189         dbhelper = self.repo.system_source.dbhelper
       
  1190         cols  = ['%s%s' % (prefix, col) for col in attrlist]
       
  1191         table = '%s%s' % (prefix, etype)
       
  1192         sql = dbhelper.sql_drop_multicol_unique_index(table, cols)
       
  1193         self.sqlexec(sql, ask_confirm=False)
       
  1194         if commit:
       
  1195             self.commit()
       
  1196 
       
  1197     @deprecated('[3.2] use sync_schema_props_perms(ertype, syncprops=False)')
  1208     @deprecated('[3.2] use sync_schema_props_perms(ertype, syncprops=False)')
  1198     def cmd_synchronize_permissions(self, ertype, commit=True):
  1209     def cmd_synchronize_permissions(self, ertype, commit=True):
  1199         self.cmd_sync_schema_props_perms(ertype, syncprops=False, commit=commit)
  1210         self.cmd_sync_schema_props_perms(ertype, syncprops=False, commit=commit)
  1200 
  1211 
  1201     # Workflows handling ######################################################
  1212     # Workflows handling ######################################################