444 # so there is nothing to do here |
444 # so there is nothing to do here |
445 if rdef.eid in session.transaction_data.get('neweids', ()): |
445 if rdef.eid in session.transaction_data.get('neweids', ()): |
446 return |
446 return |
447 subjtype, rtype, objtype = session.schema.schema_by_eid(rdef.eid) |
447 subjtype, rtype, objtype = session.schema.schema_by_eid(rdef.eid) |
448 cstrtype = self.entity.type |
448 cstrtype = self.entity.type |
449 cstr = rtype.constraint_by_type(subjtype, objtype, cstrtype) |
449 oldcstr = rtype.constraint_by_type(subjtype, objtype, cstrtype) |
450 prevcstr = CONSTRAINTS[cstrtype].deserialize(self.entity.value) |
450 newcstr = CONSTRAINTS[cstrtype].deserialize(self.entity.value) |
451 table = SQL_PREFIX + str(subjtype) |
451 table = SQL_PREFIX + str(subjtype) |
452 column = SQL_PREFIX + str(rtype) |
452 column = SQL_PREFIX + str(rtype) |
453 # alter the physical schema on size constraint changes |
453 # alter the physical schema on size constraint changes |
454 if prevcstr.type() == 'SizeConstraint' and ( |
454 if newcstr.type() == 'SizeConstraint' and ( |
455 cstr is None or cstr.max != prevcstr.max): |
455 oldcstr is None or oldcstr.max != newcstr.max): |
456 adbh = self.session.pool.source('system').dbhelper |
456 adbh = self.session.pool.source('system').dbhelper |
457 card = rtype.rproperty(subjtype, objtype, 'cardinality') |
457 card = rtype.rproperty(subjtype, objtype, 'cardinality') |
458 coltype = type_from_constraints(adbh, objtype, [prevcstr], |
458 coltype = type_from_constraints(adbh, objtype, [newcstr], |
459 creating=False) |
459 creating=False) |
460 sql = adbh.sql_change_col_type(table, column, coltype, card != '1') |
460 sql = adbh.sql_change_col_type(table, column, coltype, card != '1') |
461 try: |
461 try: |
462 session.system_sql(sql, rollback_on_failure=False) |
462 session.system_sql(sql, rollback_on_failure=False) |
463 self.info('altered column %s of table %s: now VARCHAR(%s)', |
463 self.info('altered column %s of table %s: now VARCHAR(%s)', |
464 column, table, prevcstr.max) |
464 column, table, newcstr.max) |
465 except Exception, ex: |
465 except Exception, ex: |
466 # not supported by sqlite for instance |
466 # not supported by sqlite for instance |
467 self.error('error while altering table %s: %s', table, ex) |
467 self.error('error while altering table %s: %s', table, ex) |
468 elif cstrtype == 'UniqueConstraint': |
468 elif cstrtype == 'UniqueConstraint' and oldcstr is None: |
469 session.pool.source('system').create_index( |
469 session.pool.source('system').create_index( |
470 self.session, table, column, unique=True) |
470 self.session, table, column, unique=True) |
471 |
471 |
472 |
472 |
473 class SourceDbCWConstraintDel(PreCommitOperation): |
473 class SourceDbCWConstraintDel(PreCommitOperation): |
596 return |
596 return |
597 subjtype, rtype, objtype = self.session.schema.schema_by_eid(rdef.eid) |
597 subjtype, rtype, objtype = self.session.schema.schema_by_eid(rdef.eid) |
598 self.prepare_constraints(subjtype, rtype, objtype) |
598 self.prepare_constraints(subjtype, rtype, objtype) |
599 cstrtype = self.entity.type |
599 cstrtype = self.entity.type |
600 self.cstr = rtype.constraint_by_type(subjtype, objtype, cstrtype) |
600 self.cstr = rtype.constraint_by_type(subjtype, objtype, cstrtype) |
601 self.prevcstr = CONSTRAINTS[cstrtype].deserialize(self.entity.value) |
601 self.newcstr = CONSTRAINTS[cstrtype].deserialize(self.entity.value) |
602 self.prevcstr.eid = self.entity.eid |
602 self.newcstr.eid = self.entity.eid |
603 |
603 |
604 def commit_event(self): |
604 def commit_event(self): |
605 if self.cancelled: |
605 if self.cancelled: |
606 return |
606 return |
607 # in-place modification |
607 # in-place modification |
608 if not self.cstr is None: |
608 if not self.cstr is None: |
609 self.constraints.remove(self.cstr) |
609 self.constraints.remove(self.cstr) |
610 self.constraints.append(self.prevcstr) |
610 self.constraints.append(self.newcstr) |
611 |
611 |
612 |
612 |
613 class MemSchemaCWConstraintDel(MemSchemaOperation): |
613 class MemSchemaCWConstraintDel(MemSchemaOperation): |
614 """actually remove a constraint of a relation definition |
614 """actually remove a constraint of a relation definition |
615 |
615 |