1475 * only works when the old type can be changed to the new type by the |
1475 * only works when the old type can be changed to the new type by the |
1476 underlying rdbms (eg using ALTER TABLE) |
1476 underlying rdbms (eg using ALTER TABLE) |
1477 * the actual schema won't be updated until next startup |
1477 * the actual schema won't be updated until next startup |
1478 """ |
1478 """ |
1479 rschema = self.repo.schema.rschema(attr) |
1479 rschema = self.repo.schema.rschema(attr) |
1480 oldtype = rschema.objects(etype)[0] |
1480 oldschema = rschema.objects(etype)[0] |
1481 rdefeid = rschema.rdef(etype, oldtype).eid |
1481 rdef = rschema.rdef(etype, oldschema) |
1482 allownull = rschema.rdef(etype, oldtype).cardinality[0] != '1' |
|
1483 sql = ("UPDATE cw_CWAttribute " |
1482 sql = ("UPDATE cw_CWAttribute " |
1484 "SET cw_to_entity=(SELECT cw_eid FROM cw_CWEType WHERE cw_name='%s')" |
1483 "SET cw_to_entity=(SELECT cw_eid FROM cw_CWEType WHERE cw_name='%s')" |
1485 "WHERE cw_eid=%s") % (newtype, rdefeid) |
1484 "WHERE cw_eid=%s") % (newtype, rdef.eid) |
1486 self.sqlexec(sql, ask_confirm=False) |
1485 self.sqlexec(sql, ask_confirm=False) |
1487 dbhelper = self.repo.system_source.dbhelper |
1486 dbhelper = self.repo.system_source.dbhelper |
1488 sqltype = dbhelper.TYPE_MAPPING[newtype] |
1487 sqltype = dbhelper.TYPE_MAPPING[newtype] |
1489 cursor = self.cnx.cnxset.cu |
1488 cursor = self.cnx.cnxset.cu |
1490 dbhelper.change_col_type(cursor, 'cw_%s' % etype, 'cw_%s' % attr, sqltype, allownull) |
1489 allownull = rdef.cardinality[0] != '1' |
|
1490 dbhelper.change_col_type(cursor, 'cw_%s' % etype, 'cw_%s' % attr, sqltype, allownull) |
1491 if commit: |
1491 if commit: |
1492 self.commit() |
1492 self.commit() |
|
1493 # manually update live schema |
|
1494 eschema = self.repo.schema[etype] |
|
1495 rschema._subj_schemas[eschema].remove(oldschema) |
|
1496 rschema._obj_schemas[oldschema].remove(eschema) |
|
1497 newschema = self.repo.schema[newtype] |
|
1498 rschema._update(eschema, newschema) |
|
1499 rdef.object = newschema |
|
1500 del rschema.rdefs[(eschema, oldschema)] |
|
1501 rschema.rdefs[(eschema, newschema)] = rdef |
1493 |
1502 |
1494 def cmd_add_entity_type_table(self, etype, commit=True): |
1503 def cmd_add_entity_type_table(self, etype, commit=True): |
1495 """low level method to create the sql table for an existing entity. |
1504 """low level method to create the sql table for an existing entity. |
1496 This may be useful on accidental desync between the repository schema |
1505 This may be useful on accidental desync between the repository schema |
1497 and a sql database |
1506 and a sql database |