hooks/syncschema.py
changeset 3400 93fc0070673a
parent 3376 f5c69485381f
child 3402 434946bb5356
equal deleted inserted replaced
3399:2b84f4adb6f8 3400:93fc0070673a
   144     def __init__(self, session):
   144     def __init__(self, session):
   145         hook.SingleLastOperation.__init__(self, session)
   145         hook.SingleLastOperation.__init__(self, session)
   146 
   146 
   147     def commit_event(self):
   147     def commit_event(self):
   148         rebuildinfered = self.session.data.get('rebuild-infered', True)
   148         rebuildinfered = self.session.data.get('rebuild-infered', True)
   149         self.session.repo.set_schema(self.repo.schema, rebuildinfered=rebuildinfered)
   149         repo = self.session.repo
       
   150         repo.set_schema(repo.schema, rebuildinfered=rebuildinfered)
   150 
   151 
   151 
   152 
   152 class MemSchemaOperation(hook.Operation):
   153 class MemSchemaOperation(hook.Operation):
   153     """base class for schema operations"""
   154     """base class for schema operations"""
   154     def __init__(self, session, kobj=None, **kwargs):
   155     def __init__(self, session, kobj=None, **kwargs):
   508 
   509 
   509 class MemSchemaCWETypeAdd(MemSchemaEarlyOperation):
   510 class MemSchemaCWETypeAdd(MemSchemaEarlyOperation):
   510     """actually add the entity type to the instance's schema"""
   511     """actually add the entity type to the instance's schema"""
   511     eid = None # make pylint happy
   512     eid = None # make pylint happy
   512     def commit_event(self):
   513     def commit_event(self):
   513         self.schema.add_entity_type(self.kobj)
   514         self.session.vreg.schema.add_entity_type(self.kobj)
   514 
   515 
   515 
   516 
   516 class MemSchemaCWETypeRename(MemSchemaOperation):
   517 class MemSchemaCWETypeRename(MemSchemaOperation):
   517     """this operation updates physical storage accordingly"""
   518     """this operation updates physical storage accordingly"""
   518     oldname = newname = None # make pylint happy
   519     oldname = newname = None # make pylint happy
   524 class MemSchemaCWETypeDel(MemSchemaOperation):
   525 class MemSchemaCWETypeDel(MemSchemaOperation):
   525     """actually remove the entity type from the instance's schema"""
   526     """actually remove the entity type from the instance's schema"""
   526     def commit_event(self):
   527     def commit_event(self):
   527         try:
   528         try:
   528             # del_entity_type also removes entity's relations
   529             # del_entity_type also removes entity's relations
   529             self.schema.del_entity_type(self.kobj)
   530             self.session.vreg.schema.del_entity_type(self.kobj)
   530         except KeyError:
   531         except KeyError:
   531             # s/o entity type have already been deleted
   532             # s/o entity type have already been deleted
   532             pass
   533             pass
   533 
   534 
   534 
   535 
   535 class MemSchemaCWRTypeAdd(MemSchemaEarlyOperation):
   536 class MemSchemaCWRTypeAdd(MemSchemaEarlyOperation):
   536     """actually add the relation type to the instance's schema"""
   537     """actually add the relation type to the instance's schema"""
   537     eid = None # make pylint happy
   538     eid = None # make pylint happy
   538     def commit_event(self):
   539     def commit_event(self):
   539         rschema = self.schema.add_relation_type(self.kobj)
   540         rschema = self.session.vreg.schema.add_relation_type(self.kobj)
   540         rschema.set_default_groups()
   541         rschema.set_default_groups()
   541 
   542 
   542 
   543 
   543 class MemSchemaCWRTypeUpdate(MemSchemaOperation):
   544 class MemSchemaCWRTypeUpdate(MemSchemaOperation):
   544     """actually update some properties of a relation definition"""
   545     """actually update some properties of a relation definition"""
   552 
   553 
   553 class MemSchemaCWRTypeDel(MemSchemaOperation):
   554 class MemSchemaCWRTypeDel(MemSchemaOperation):
   554     """actually remove the relation type from the instance's schema"""
   555     """actually remove the relation type from the instance's schema"""
   555     def commit_event(self):
   556     def commit_event(self):
   556         try:
   557         try:
   557             self.schema.del_relation_type(self.kobj)
   558             self.session.vreg.schema.del_relation_type(self.kobj)
   558         except KeyError:
   559         except KeyError:
   559             # s/o entity type have already been deleted
   560             # s/o entity type have already been deleted
   560             pass
   561             pass
   561 
   562 
   562 
   563 
   563 class MemSchemaRDefAdd(MemSchemaEarlyOperation):
   564 class MemSchemaRDefAdd(MemSchemaEarlyOperation):
   564     """actually add the attribute relation definition to the instance's
   565     """actually add the attribute relation definition to the instance's
   565     schema
   566     schema
   566     """
   567     """
   567     def commit_event(self):
   568     def commit_event(self):
   568         self.schema.add_relation_def(self.kobj)
   569         self.session.vreg.schema.add_relation_def(self.kobj)
   569 
   570 
   570 
   571 
   571 class MemSchemaRDefUpdate(MemSchemaOperation):
   572 class MemSchemaRDefUpdate(MemSchemaOperation):
   572     """actually update some properties of a relation definition"""
   573     """actually update some properties of a relation definition"""
   573     rschema = values = None # make pylint happy
   574     rschema = values = None # make pylint happy
   581 class MemSchemaRDefDel(MemSchemaOperation):
   582 class MemSchemaRDefDel(MemSchemaOperation):
   582     """actually remove the relation definition from the instance's schema"""
   583     """actually remove the relation definition from the instance's schema"""
   583     def commit_event(self):
   584     def commit_event(self):
   584         subjtype, rtype, objtype = self.kobj
   585         subjtype, rtype, objtype = self.kobj
   585         try:
   586         try:
   586             self.schema.del_relation_def(subjtype, rtype, objtype)
   587             self.session.vreg.schema.del_relation_def(subjtype, rtype, objtype)
   587         except KeyError:
   588         except KeyError:
   588             # relation type may have been already deleted
   589             # relation type may have been already deleted
   589             pass
   590             pass
   590 
   591 
   591 
   592 
   642             session, perm, etype_eid)
   643             session, perm, etype_eid)
   643 
   644 
   644     def commit_event(self):
   645     def commit_event(self):
   645         """the observed connections pool has been commited"""
   646         """the observed connections pool has been commited"""
   646         try:
   647         try:
   647             erschema = self.schema[self.name]
   648             erschema = self.session.vreg.schema[self.name]
   648         except KeyError:
   649         except KeyError:
   649             # duh, schema not found, log error and skip operation
   650             # duh, schema not found, log error and skip operation
   650             self.error('no schema for %s', self.name)
   651             self.error('no schema for %s', self.name)
   651             return
   652             return
   652         groups = list(erschema.get_groups(self.perm))
   653         groups = list(erschema.get_groups(self.perm))
   665     """
   666     """
   666 
   667 
   667     def commit_event(self):
   668     def commit_event(self):
   668         """the observed connections pool has been commited"""
   669         """the observed connections pool has been commited"""
   669         try:
   670         try:
   670             erschema = self.schema[self.name]
   671             erschema = self.session.vreg.schema[self.name]
   671         except KeyError:
   672         except KeyError:
   672             # duh, schema not found, log error and skip operation
   673             # duh, schema not found, log error and skip operation
   673             self.error('no schema for %s', self.name)
   674             self.error('no schema for %s', self.name)
   674             return
   675             return
   675         groups = list(erschema.get_groups(self.perm))
   676         groups = list(erschema.get_groups(self.perm))
   691             session, perm, etype_eid)
   692             session, perm, etype_eid)
   692 
   693 
   693     def commit_event(self):
   694     def commit_event(self):
   694         """the observed connections pool has been commited"""
   695         """the observed connections pool has been commited"""
   695         try:
   696         try:
   696             erschema = self.schema[self.name]
   697             erschema = self.session.vreg.schema[self.name]
   697         except KeyError:
   698         except KeyError:
   698             # duh, schema not found, log error and skip operation
   699             # duh, schema not found, log error and skip operation
   699             self.error('no schema for %s', self.name)
   700             self.error('no schema for %s', self.name)
   700             return
   701             return
   701         exprs = list(erschema.get_rqlexprs(self.perm))
   702         exprs = list(erschema.get_rqlexprs(self.perm))
   709     """
   710     """
   710 
   711 
   711     def commit_event(self):
   712     def commit_event(self):
   712         """the observed connections pool has been commited"""
   713         """the observed connections pool has been commited"""
   713         try:
   714         try:
   714             erschema = self.schema[self.name]
   715             erschema = self.session.vreg.schema[self.name]
   715         except KeyError:
   716         except KeyError:
   716             # duh, schema not found, log error and skip operation
   717             # duh, schema not found, log error and skip operation
   717             self.error('no schema for %s', self.name)
   718             self.error('no schema for %s', self.name)
   718             return
   719             return
   719         rqlexprs = list(erschema.get_rqlexprs(self.perm))
   720         rqlexprs = list(erschema.get_rqlexprs(self.perm))
   729 
   730 
   730 
   731 
   731 class MemSchemaSpecializesAdd(MemSchemaOperation):
   732 class MemSchemaSpecializesAdd(MemSchemaOperation):
   732 
   733 
   733     def commit_event(self):
   734     def commit_event(self):
   734         eschema = self.session.schema.schema_by_eid(self.etypeeid)
   735         eschema = self.session.vreg.schema.schema_by_eid(self.etypeeid)
   735         parenteschema = self.session.schema.schema_by_eid(self.parentetypeeid)
   736         parenteschema = self.session.vreg.schema.schema_by_eid(self.parentetypeeid)
   736         eschema._specialized_type = parenteschema.type
   737         eschema._specialized_type = parenteschema.type
   737         parenteschema._specialized_by.append(eschema.type)
   738         parenteschema._specialized_by.append(eschema.type)
   738 
   739 
   739 
   740 
   740 class MemSchemaSpecializesDel(MemSchemaOperation):
   741 class MemSchemaSpecializesDel(MemSchemaOperation):
   741 
   742 
   742     def commit_event(self):
   743     def commit_event(self):
   743         try:
   744         try:
   744             eschema = self.session.schema.schema_by_eid(self.etypeeid)
   745             eschema = self.session.vreg.schema.schema_by_eid(self.etypeeid)
   745             parenteschema = self.session.schema.schema_by_eid(self.parentetypeeid)
   746             parenteschema = self.session.vreg.schema.schema_by_eid(self.parentetypeeid)
   746         except KeyError:
   747         except KeyError:
   747             # etype removed, nothing to do
   748             # etype removed, nothing to do
   748             return
   749             return
   749         eschema._specialized_type = None
   750         eschema._specialized_type = None
   750         parenteschema._specialized_by.remove(eschema.type)
   751         parenteschema._specialized_by.remove(eschema.type)
   801 
   802 
   802     def __call__(self):
   803     def __call__(self):
   803         entity = self.entity
   804         entity = self.entity
   804         if entity.get('final'):
   805         if entity.get('final'):
   805             return
   806             return
   806         schema = self._cw.schema
   807         schema = self._cw.vreg.schema
   807         name = entity['name']
   808         name = entity['name']
   808         etype = EntityType(name=name, description=entity.get('description'),
   809         etype = EntityType(name=name, description=entity.get('description'),
   809                            meta=entity.get('meta')) # don't care about final
   810                            meta=entity.get('meta')) # don't care about final
   810         # fake we add it to the schema now to get a correctly initialized schema
   811         # fake we add it to the schema now to get a correctly initialized schema
   811         # but remove it before doing anything more dangerous...
   812         # but remove it before doing anything more dangerous...
   812         schema = self._cw.schema
   813         schema = self._cw.vreg.schema
   813         eschema = schema.add_entity_type(etype)
   814         eschema = schema.add_entity_type(etype)
   814         eschema.set_default_groups()
   815         eschema.set_default_groups()
   815         # generate table sql and rql to add metadata
   816         # generate table sql and rql to add metadata
   816         tablesql = eschema2sql(self._cw.pool.source('system').dbhelper, eschema,
   817         tablesql = eschema2sql(self._cw.pool.source('system').dbhelper, eschema,
   817                                prefix=SQL_PREFIX)
   818                                prefix=SQL_PREFIX)
   914     __regid__ = 'syncupdatecwrtype'
   915     __regid__ = 'syncupdatecwrtype'
   915     events = ('after_update_entity',)
   916     events = ('after_update_entity',)
   916 
   917 
   917     def __call__(self):
   918     def __call__(self):
   918         entity = self.entity
   919         entity = self.entity
   919         rschema = self._cw.schema.rschema(entity.name)
   920         rschema = self._cw.vreg.schema.rschema(entity.name)
   920         newvalues = {}
   921         newvalues = {}
   921         for prop in ('meta', 'symetric', 'inlined'):
   922         for prop in ('meta', 'symetric', 'inlined'):
   922             if prop in entity:
   923             if prop in entity:
   923                 newvalues[prop] = entity[prop]
   924                 newvalues[prop] = entity[prop]
   924         if newvalues:
   925         if newvalues:
  1010     def __call__(self):
  1011     def __call__(self):
  1011         entity = self.entity
  1012         entity = self.entity
  1012         if self._cw.deleted_in_transaction(entity.eid):
  1013         if self._cw.deleted_in_transaction(entity.eid):
  1013             return
  1014             return
  1014         desttype = entity.otype.name
  1015         desttype = entity.otype.name
  1015         rschema = self._cw.schema[entity.rtype.name]
  1016         rschema = self._cw.vreg.schema[entity.rtype.name]
  1016         newvalues = {}
  1017         newvalues = {}
  1017         for prop in rschema.rproperty_defs(desttype):
  1018         for prop in rschema.rproperty_defs(desttype):
  1018             if prop == 'constraints':
  1019             if prop == 'constraints':
  1019                 continue
  1020                 continue
  1020             if prop == 'order':
  1021             if prop == 'order':
  1056     events = ('before_delete_relation',)
  1057     events = ('before_delete_relation',)
  1057 
  1058 
  1058     def __call__(self):
  1059     def __call__(self):
  1059         if self._cw.deleted_in_transaction(self.eidfrom):
  1060         if self._cw.deleted_in_transaction(self.eidfrom):
  1060             return
  1061             return
  1061         schema = self._cw.schema
  1062         schema = self._cw.vreg.schema
  1062         entity = self._cw.entity_from_eid(self.eidto)
  1063         entity = self._cw.entity_from_eid(self.eidto)
  1063         subjtype, rtype, objtype = schema.schema_by_eid(self.eidfrom)
  1064         subjtype, rtype, objtype = schema.schema_by_eid(self.eidfrom)
  1064         try:
  1065         try:
  1065             cstr = rtype.constraint_by_type(subjtype, objtype,
  1066             cstr = rtype.constraint_by_type(subjtype, objtype,
  1066                                             entity.cstrtype[0].name)
  1067                                             entity.cstrtype[0].name)