hooks/syncschema.py
changeset 7398 26695dd703d8
parent 7185 6e42b595e5df
child 7460 2455cdbeadca
equal deleted inserted replaced
7397:6a9e66d788b3 7398:26695dd703d8
    90         session.exception('error while adding column %s to table %s',
    90         session.exception('error while adding column %s to table %s',
    91                           table, column)
    91                           table, column)
    92     # create index before alter table which may expectingly fail during test
    92     # create index before alter table which may expectingly fail during test
    93     # (sqlite) while index creation should never fail (test for index existence
    93     # (sqlite) while index creation should never fail (test for index existence
    94     # is done by the dbhelper)
    94     # is done by the dbhelper)
    95     session.pool.source('system').create_index(session, table, column)
    95     session.cnxset.source('system').create_index(session, table, column)
    96     session.info('added index on %s(%s)', table, column)
    96     session.info('added index on %s(%s)', table, column)
    97 
    97 
    98 
    98 
    99 def insert_rdef_on_subclasses(session, eschema, rschema, rdefdef, props):
    99 def insert_rdef_on_subclasses(session, eschema, rschema, rdefdef, props):
   100     # XXX 'infered': True/False, not clear actually
   100     # XXX 'infered': True/False, not clear actually
   250         schema = session.vreg.schema
   250         schema = session.vreg.schema
   251         etype = ybo.EntityType(eid=entity.eid, name=entity.name,
   251         etype = ybo.EntityType(eid=entity.eid, name=entity.name,
   252                                description=entity.description)
   252                                description=entity.description)
   253         eschema = schema.add_entity_type(etype)
   253         eschema = schema.add_entity_type(etype)
   254         # create the necessary table
   254         # create the necessary table
   255         tablesql = y2sql.eschema2sql(session.pool.source('system').dbhelper,
   255         tablesql = y2sql.eschema2sql(session.cnxset.source('system').dbhelper,
   256                                      eschema, prefix=SQL_PREFIX)
   256                                      eschema, prefix=SQL_PREFIX)
   257         for sql in tablesql.split(';'):
   257         for sql in tablesql.split(';'):
   258             if sql.strip():
   258             if sql.strip():
   259                 session.system_sql(sql)
   259                 session.system_sql(sql)
   260         # add meta relations
   260         # add meta relations
   287 
   287 
   288     def rename(self, oldname, newname):
   288     def rename(self, oldname, newname):
   289         self.session.vreg.schema.rename_entity_type(oldname, newname)
   289         self.session.vreg.schema.rename_entity_type(oldname, newname)
   290         # we need sql to operate physical changes on the system database
   290         # we need sql to operate physical changes on the system database
   291         sqlexec = self.session.system_sql
   291         sqlexec = self.session.system_sql
   292         dbhelper= self.session.pool.source('system').dbhelper
   292         dbhelper= self.session.cnxset.source('system').dbhelper
   293         sql = dbhelper.sql_rename_table(SQL_PREFIX+oldname,
   293         sql = dbhelper.sql_rename_table(SQL_PREFIX+oldname,
   294                                         SQL_PREFIX+newname)
   294                                         SQL_PREFIX+newname)
   295         sqlexec(sql)
   295         sqlexec(sql)
   296         self.info('renamed table %s to %s', oldname, newname)
   296         self.info('renamed table %s to %s', oldname, newname)
   297         sqlexec('UPDATE entities SET type=%(newname)s WHERE type=%(oldname)s',
   297         sqlexec('UPDATE entities SET type=%(newname)s WHERE type=%(oldname)s',
   431                  'fulltextindexed': entity.fulltextindexed,
   431                  'fulltextindexed': entity.fulltextindexed,
   432                  'internationalizable': entity.internationalizable}
   432                  'internationalizable': entity.internationalizable}
   433         # update the in-memory schema first
   433         # update the in-memory schema first
   434         rdefdef = self.init_rdef(**props)
   434         rdefdef = self.init_rdef(**props)
   435         # then make necessary changes to the system source database
   435         # then make necessary changes to the system source database
   436         syssource = session.pool.source('system')
   436         syssource = session.cnxset.source('system')
   437         attrtype = y2sql.type_from_constraints(
   437         attrtype = y2sql.type_from_constraints(
   438             syssource.dbhelper, rdefdef.object, rdefdef.constraints)
   438             syssource.dbhelper, rdefdef.object, rdefdef.constraints)
   439         # XXX should be moved somehow into lgdb: sqlite doesn't support to
   439         # XXX should be moved somehow into lgdb: sqlite doesn't support to
   440         # add a new column with UNIQUE, it should be added after the ALTER TABLE
   440         # add a new column with UNIQUE, it should be added after the ALTER TABLE
   441         # using ADD INDEX
   441         # using ADD INDEX
   601         rdef = self.rdef = self.rschema.rdefs[self.rdefkey]
   601         rdef = self.rdef = self.rschema.rdefs[self.rdefkey]
   602         # update the in-memory schema first
   602         # update the in-memory schema first
   603         self.oldvalues = dict( (attr, getattr(rdef, attr)) for attr in self.values)
   603         self.oldvalues = dict( (attr, getattr(rdef, attr)) for attr in self.values)
   604         rdef.update(self.values)
   604         rdef.update(self.values)
   605         # then make necessary changes to the system source database
   605         # then make necessary changes to the system source database
   606         syssource = session.pool.source('system')
   606         syssource = session.cnxset.source('system')
   607         if 'indexed' in self.values:
   607         if 'indexed' in self.values:
   608             syssource.update_rdef_indexed(session, rdef)
   608             syssource.update_rdef_indexed(session, rdef)
   609             self.indexed_changed = True
   609             self.indexed_changed = True
   610         if 'cardinality' in self.values and (rdef.rtype.final or
   610         if 'cardinality' in self.values and (rdef.rtype.final or
   611                                              rdef.rtype.inlined) \
   611                                              rdef.rtype.inlined) \
   619         if self.rdef is None:
   619         if self.rdef is None:
   620             return
   620             return
   621         # revert changes on in memory schema
   621         # revert changes on in memory schema
   622         self.rdef.update(self.oldvalues)
   622         self.rdef.update(self.oldvalues)
   623         # revert changes on database
   623         # revert changes on database
   624         syssource = self.session.pool.source('system')
   624         syssource = self.session.cnxset.source('system')
   625         if self.indexed_changed:
   625         if self.indexed_changed:
   626             syssource.update_rdef_indexed(self.session, self.rdef)
   626             syssource.update_rdef_indexed(self.session, self.rdef)
   627         if self.null_allowed_changed:
   627         if self.null_allowed_changed:
   628             syssource.update_rdef_null_allowed(self.session, self.rdef)
   628             syssource.update_rdef_null_allowed(self.session, self.rdef)
   629 
   629 
   647         # in-place modification of in-memory schema first
   647         # in-place modification of in-memory schema first
   648         _set_modifiable_constraints(rdef)
   648         _set_modifiable_constraints(rdef)
   649         rdef.constraints.remove(self.oldcstr)
   649         rdef.constraints.remove(self.oldcstr)
   650         # then update database: alter the physical schema on size/unique
   650         # then update database: alter the physical schema on size/unique
   651         # constraint changes
   651         # constraint changes
   652         syssource = session.pool.source('system')
   652         syssource = session.cnxset.source('system')
   653         cstrtype = self.oldcstr.type()
   653         cstrtype = self.oldcstr.type()
   654         if cstrtype == 'SizeConstraint':
   654         if cstrtype == 'SizeConstraint':
   655             syssource.update_rdef_column(session, rdef)
   655             syssource.update_rdef_column(session, rdef)
   656             self.size_cstr_changed = True
   656             self.size_cstr_changed = True
   657         elif cstrtype == 'UniqueConstraint':
   657         elif cstrtype == 'UniqueConstraint':
   663         if self.newcstr is not None:
   663         if self.newcstr is not None:
   664             self.rdef.constraints.remove(self.newcstr)
   664             self.rdef.constraints.remove(self.newcstr)
   665         if self.oldcstr is not None:
   665         if self.oldcstr is not None:
   666             self.rdef.constraints.append(self.oldcstr)
   666             self.rdef.constraints.append(self.oldcstr)
   667         # revert changes on database
   667         # revert changes on database
   668         syssource = self.session.pool.source('system')
   668         syssource = self.session.cnxset.source('system')
   669         if self.size_cstr_changed:
   669         if self.size_cstr_changed:
   670             syssource.update_rdef_column(self.session, self.rdef)
   670             syssource.update_rdef_column(self.session, self.rdef)
   671         if self.unique_changed:
   671         if self.unique_changed:
   672             syssource.update_rdef_unique(self.session, self.rdef)
   672             syssource.update_rdef_unique(self.session, self.rdef)
   673 
   673 
   694         if oldcstr is not None:
   694         if oldcstr is not None:
   695             rdef.constraints.remove(oldcstr)
   695             rdef.constraints.remove(oldcstr)
   696         rdef.constraints.append(newcstr)
   696         rdef.constraints.append(newcstr)
   697         # then update database: alter the physical schema on size/unique
   697         # then update database: alter the physical schema on size/unique
   698         # constraint changes
   698         # constraint changes
   699         syssource = session.pool.source('system')
   699         syssource = session.cnxset.source('system')
   700         if cstrtype == 'SizeConstraint' and (oldcstr is None or
   700         if cstrtype == 'SizeConstraint' and (oldcstr is None or
   701                                              oldcstr.max != newcstr.max):
   701                                              oldcstr.max != newcstr.max):
   702             syssource.update_rdef_column(session, rdef)
   702             syssource.update_rdef_column(session, rdef)
   703             self.size_cstr_changed = True
   703             self.size_cstr_changed = True
   704         elif cstrtype == 'UniqueConstraint' and oldcstr is None:
   704         elif cstrtype == 'UniqueConstraint' and oldcstr is None:
   711     def precommit_event(self):
   711     def precommit_event(self):
   712         session = self.session
   712         session = self.session
   713         prefix = SQL_PREFIX
   713         prefix = SQL_PREFIX
   714         table = '%s%s' % (prefix, self.entity.constraint_of[0].name)
   714         table = '%s%s' % (prefix, self.entity.constraint_of[0].name)
   715         cols = ['%s%s' % (prefix, r.name) for r in self.entity.relations]
   715         cols = ['%s%s' % (prefix, r.name) for r in self.entity.relations]
   716         dbhelper= session.pool.source('system').dbhelper
   716         dbhelper= session.cnxset.source('system').dbhelper
   717         sqls = dbhelper.sqls_create_multicol_unique_index(table, cols)
   717         sqls = dbhelper.sqls_create_multicol_unique_index(table, cols)
   718         for sql in sqls:
   718         for sql in sqls:
   719             session.system_sql(sql)
   719             session.system_sql(sql)
   720 
   720 
   721     # XXX revertprecommit_event
   721     # XXX revertprecommit_event
   731     cols = [] # for pylint
   731     cols = [] # for pylint
   732     def precommit_event(self):
   732     def precommit_event(self):
   733         session = self.session
   733         session = self.session
   734         prefix = SQL_PREFIX
   734         prefix = SQL_PREFIX
   735         table = '%s%s' % (prefix, self.entity.type)
   735         table = '%s%s' % (prefix, self.entity.type)
   736         dbhelper= session.pool.source('system').dbhelper
   736         dbhelper= session.cnxset.source('system').dbhelper
   737         cols = ['%s%s' % (prefix, c) for c in self.cols]
   737         cols = ['%s%s' % (prefix, c) for c in self.cols]
   738         sqls = dbhelper.sqls_drop_multicol_unique_index(table, cols)
   738         sqls = dbhelper.sqls_drop_multicol_unique_index(table, cols)
   739         for sql in sqls:
   739         for sql in sqls:
   740             session.system_sql(sql)
   740             session.system_sql(sql)
   741 
   741 
   780 class MemSchemaPermissionAdd(MemSchemaOperation):
   780 class MemSchemaPermissionAdd(MemSchemaOperation):
   781     """synchronize schema when a *_permission relation has been added on a group
   781     """synchronize schema when a *_permission relation has been added on a group
   782     """
   782     """
   783 
   783 
   784     def precommit_event(self):
   784     def precommit_event(self):
   785         """the observed connections pool has been commited"""
   785         """the observed connections.cnxset has been commited"""
   786         try:
   786         try:
   787             erschema = self.session.vreg.schema.schema_by_eid(self.eid)
   787             erschema = self.session.vreg.schema.schema_by_eid(self.eid)
   788         except KeyError:
   788         except KeyError:
   789             # duh, schema not found, log error and skip operation
   789             # duh, schema not found, log error and skip operation
   790             self.warning('no schema for %s', self.eid)
   790             self.warning('no schema for %s', self.eid)
   809     """synchronize schema when a *_permission relation has been deleted from a
   809     """synchronize schema when a *_permission relation has been deleted from a
   810     group
   810     group
   811     """
   811     """
   812 
   812 
   813     def precommit_event(self):
   813     def precommit_event(self):
   814         """the observed connections pool has been commited"""
   814         """the observed connections set has been commited"""
   815         try:
   815         try:
   816             erschema = self.session.vreg.schema.schema_by_eid(self.eid)
   816             erschema = self.session.vreg.schema.schema_by_eid(self.eid)
   817         except KeyError:
   817         except KeyError:
   818             # duh, schema not found, log error and skip operation
   818             # duh, schema not found, log error and skip operation
   819             self.warning('no schema for %s', self.eid)
   819             self.warning('no schema for %s', self.eid)
  1221                     if still_fti or container is not entity:
  1221                     if still_fti or container is not entity:
  1222                         source.fti_unindex_entities(session, [container])
  1222                         source.fti_unindex_entities(session, [container])
  1223                         source.fti_index_entities(session, [container])
  1223                         source.fti_index_entities(session, [container])
  1224         if to_reindex:
  1224         if to_reindex:
  1225             # Transaction has already been committed
  1225             # Transaction has already been committed
  1226             session.pool.commit()
  1226             session.cnxset.commit()
  1227 
  1227 
  1228 
  1228 
  1229 
  1229 
  1230 
  1230 
  1231 # specializes synchronization hooks ############################################
  1231 # specializes synchronization hooks ############################################