server/schemahooks.py
changeset 2476 1294a6bdf3bf
parent 2462 9e670072884d
child 2599 79bd12769c55
equal deleted inserted replaced
2475:b6753521129d 2476:1294a6bdf3bf
   103     def commit_event(self):
   103     def commit_event(self):
   104         self.repo.set_schema(self.repo.schema)
   104         self.repo.set_schema(self.repo.schema)
   105 
   105 
   106 
   106 
   107 class DropTableOp(PreCommitOperation):
   107 class DropTableOp(PreCommitOperation):
   108     """actually remove a database from the application's schema"""
   108     """actually remove a database from the instance's schema"""
   109     table = None # make pylint happy
   109     table = None # make pylint happy
   110     def precommit_event(self):
   110     def precommit_event(self):
   111         dropped = self.session.transaction_data.setdefault('droppedtables',
   111         dropped = self.session.transaction_data.setdefault('droppedtables',
   112                                                            set())
   112                                                            set())
   113         if self.table in dropped:
   113         if self.table in dropped:
   135 
   135 
   136 
   136 
   137 # deletion ####################################################################
   137 # deletion ####################################################################
   138 
   138 
   139 class DeleteCWETypeOp(SchemaOperation):
   139 class DeleteCWETypeOp(SchemaOperation):
   140     """actually remove the entity type from the application's schema"""
   140     """actually remove the entity type from the instance's schema"""
   141     def commit_event(self):
   141     def commit_event(self):
   142         try:
   142         try:
   143             # del_entity_type also removes entity's relations
   143             # del_entity_type also removes entity's relations
   144             self.schema.del_entity_type(self.kobj)
   144             self.schema.del_entity_type(self.kobj)
   145         except KeyError:
   145         except KeyError:
   164     session.execute('DELETE State X WHERE NOT X state_of Y')
   164     session.execute('DELETE State X WHERE NOT X state_of Y')
   165     session.execute('DELETE Transition X WHERE NOT X transition_of Y')
   165     session.execute('DELETE Transition X WHERE NOT X transition_of Y')
   166 
   166 
   167 
   167 
   168 class DeleteCWRTypeOp(SchemaOperation):
   168 class DeleteCWRTypeOp(SchemaOperation):
   169     """actually remove the relation type from the application's schema"""
   169     """actually remove the relation type from the instance's schema"""
   170     def commit_event(self):
   170     def commit_event(self):
   171         try:
   171         try:
   172             self.schema.del_relation_type(self.kobj)
   172             self.schema.del_relation_type(self.kobj)
   173         except KeyError:
   173         except KeyError:
   174             # s/o entity type have already been deleted
   174             # s/o entity type have already been deleted
   188                     {'x': eid})
   188                     {'x': eid})
   189     DeleteCWRTypeOp(session, name)
   189     DeleteCWRTypeOp(session, name)
   190 
   190 
   191 
   191 
   192 class DeleteRelationDefOp(SchemaOperation):
   192 class DeleteRelationDefOp(SchemaOperation):
   193     """actually remove the relation definition from the application's schema"""
   193     """actually remove the relation definition from the instance's schema"""
   194     def commit_event(self):
   194     def commit_event(self):
   195         subjtype, rtype, objtype = self.kobj
   195         subjtype, rtype, objtype = self.kobj
   196         try:
   196         try:
   197             self.schema.del_relation_def(subjtype, rtype, objtype)
   197             self.schema.del_relation_def(subjtype, rtype, objtype)
   198         except KeyError:
   198         except KeyError:
   242 
   242 
   243 
   243 
   244 # addition ####################################################################
   244 # addition ####################################################################
   245 
   245 
   246 class AddCWETypeOp(EarlySchemaOperation):
   246 class AddCWETypeOp(EarlySchemaOperation):
   247     """actually add the entity type to the application's schema"""
   247     """actually add the entity type to the instance's schema"""
   248     eid = None # make pylint happy
   248     eid = None # make pylint happy
   249     def commit_event(self):
   249     def commit_event(self):
   250         self.schema.add_entity_type(self.kobj)
   250         self.schema.add_entity_type(self.kobj)
   251 
   251 
   252 def before_add_eetype(session, entity):
   252 def before_add_eetype(session, entity):
   262     """after adding a CWEType entity:
   262     """after adding a CWEType entity:
   263     * create the necessary table
   263     * create the necessary table
   264     * set creation_date and modification_date by creating the necessary
   264     * set creation_date and modification_date by creating the necessary
   265       CWAttribute entities
   265       CWAttribute entities
   266     * add owned_by relation by creating the necessary CWRelation entity
   266     * add owned_by relation by creating the necessary CWRelation entity
   267     * register an operation to add the entity type to the application's
   267     * register an operation to add the entity type to the instance's
   268       schema on commit
   268       schema on commit
   269     """
   269     """
   270     if entity.get('final'):
   270     if entity.get('final'):
   271         return
   271         return
   272     schema = session.repo.schema
   272     schema = session.repo.schema
   304     for rql, kwargs in relrqls:
   304     for rql, kwargs in relrqls:
   305         session.execute(rql, kwargs)
   305         session.execute(rql, kwargs)
   306 
   306 
   307 
   307 
   308 class AddCWRTypeOp(EarlySchemaOperation):
   308 class AddCWRTypeOp(EarlySchemaOperation):
   309     """actually add the relation type to the application's schema"""
   309     """actually add the relation type to the instance's schema"""
   310     eid = None # make pylint happy
   310     eid = None # make pylint happy
   311     def commit_event(self):
   311     def commit_event(self):
   312         rschema = self.schema.add_relation_type(self.kobj)
   312         rschema = self.schema.add_relation_type(self.kobj)
   313         rschema.set_default_groups()
   313         rschema.set_default_groups()
   314 
   314 
   315 def before_add_ertype(session, entity):
   315 def before_add_ertype(session, entity):
   316     """before adding a CWRType entity:
   316     """before adding a CWRType entity:
   317     * check that we are not using an existing relation type,
   317     * check that we are not using an existing relation type,
   318     * register an operation to add the relation type to the application's
   318     * register an operation to add the relation type to the instance's
   319       schema on commit
   319       schema on commit
   320 
   320 
   321     We don't know yeat this point if a table is necessary
   321     We don't know yeat this point if a table is necessary
   322     """
   322     """
   323     name = entity['name']
   323     name = entity['name']
   324     if name in session.repo.schema.relations():
   324     if name in session.repo.schema.relations():
   325         raise RepositoryError('a relation type %s already exists' % name)
   325         raise RepositoryError('a relation type %s already exists' % name)
   326 
   326 
   327 def after_add_ertype(session, entity):
   327 def after_add_ertype(session, entity):
   328     """after a CWRType entity has been added:
   328     """after a CWRType entity has been added:
   329     * register an operation to add the relation type to the application's
   329     * register an operation to add the relation type to the instance's
   330       schema on commit
   330       schema on commit
   331     We don't know yeat this point if a table is necessary
   331     We don't know yeat this point if a table is necessary
   332     """
   332     """
   333     rtype = RelationType(name=entity['name'],
   333     rtype = RelationType(name=entity['name'],
   334                          description=entity.get('description'),
   334                          description=entity.get('description'),
   338     rtype.eid = entity.eid
   338     rtype.eid = entity.eid
   339     AddCWRTypeOp(session, rtype)
   339     AddCWRTypeOp(session, rtype)
   340 
   340 
   341 
   341 
   342 class AddErdefOp(EarlySchemaOperation):
   342 class AddErdefOp(EarlySchemaOperation):
   343     """actually add the attribute relation definition to the application's
   343     """actually add the attribute relation definition to the instance's
   344     schema
   344     schema
   345     """
   345     """
   346     def commit_event(self):
   346     def commit_event(self):
   347         self.schema.add_relation_def(self.kobj)
   347         self.schema.add_relation_def(self.kobj)
   348 
   348 
   361 class AddCWAttributePreCommitOp(PreCommitOperation):
   361 class AddCWAttributePreCommitOp(PreCommitOperation):
   362     """an attribute relation (CWAttribute) has been added:
   362     """an attribute relation (CWAttribute) has been added:
   363     * add the necessary column
   363     * add the necessary column
   364     * set default on this column if any and possible
   364     * set default on this column if any and possible
   365     * register an operation to add the relation definition to the
   365     * register an operation to add the relation definition to the
   366       application's schema on commit
   366       instance's schema on commit
   367 
   367 
   368     constraints are handled by specific hooks
   368     constraints are handled by specific hooks
   369     """
   369     """
   370     entity = None # make pylint happy
   370     entity = None # make pylint happy
   371     def precommit_event(self):
   371     def precommit_event(self):
   434     """an actual relation has been added:
   434     """an actual relation has been added:
   435     * if this is an inlined relation, add the necessary column
   435     * if this is an inlined relation, add the necessary column
   436       else if it's the first instance of this relation type, add the
   436       else if it's the first instance of this relation type, add the
   437       necessary table and set default permissions
   437       necessary table and set default permissions
   438     * register an operation to add the relation definition to the
   438     * register an operation to add the relation definition to the
   439       application's schema on commit
   439       instance's schema on commit
   440 
   440 
   441     constraints are handled by specific hooks
   441     constraints are handled by specific hooks
   442     """
   442     """
   443     entity = None # make pylint happy
   443     entity = None # make pylint happy
   444     def precommit_event(self):
   444     def precommit_event(self):