850 except Exception: |
850 except Exception: |
851 self.exception('failed to query entities table for eid %s', eid) |
851 self.exception('failed to query entities table for eid %s', eid) |
852 raise UnknownEid(eid) |
852 raise UnknownEid(eid) |
853 |
853 |
854 def eid_type_source(self, cnx, eid): # pylint: disable=E0202 |
854 def eid_type_source(self, cnx, eid): # pylint: disable=E0202 |
855 """return a tuple (type, source, extid) for the entity with id <eid>""" |
855 """return a tuple (type, extid, source) for the entity with id <eid>""" |
856 sql = 'SELECT type, extid, asource FROM entities WHERE eid=%s' % eid |
856 sql = 'SELECT type, extid, asource FROM entities WHERE eid=%s' % eid |
857 res = self._eid_type_source(cnx, eid, sql) |
857 res = self._eid_type_source(cnx, eid, sql) |
858 if not isinstance(res, list): |
858 if not isinstance(res, list): |
859 res = list(res) |
859 res = list(res) |
860 res[-2] = self.decode_extid(res[-2]) |
860 res[-2] = self.decode_extid(res[-2]) |
861 return res |
861 return res |
862 |
862 |
863 def eid_type_source_pre_131(self, cnx, eid): |
863 def eid_type_source_pre_131(self, cnx, eid): |
864 """return a tuple (type, source, extid) for the entity with id <eid>""" |
864 """return a tuple (type, extid, source) for the entity with id <eid>""" |
865 sql = 'SELECT type, extid FROM entities WHERE eid=%s' % eid |
865 sql = 'SELECT type, extid FROM entities WHERE eid=%s' % eid |
866 res = self._eid_type_source(cnx, eid, sql) |
866 res = self._eid_type_source(cnx, eid, sql) |
867 if not isinstance(res, list): |
867 if not isinstance(res, list): |
868 res = list(res) |
868 res = list(res) |
869 res[-1] = self.decode_extid(extid) |
869 res[-1] = self.decode_extid(extid) |
931 (entity.eid, eschema_eid(cnx, eschema))) |
931 (entity.eid, eschema_eid(cnx, eschema))) |
932 if 'CWSource' in self.schema and source.eid is not None: # else, cw < 3.10 |
932 if 'CWSource' in self.schema and source.eid is not None: # else, cw < 3.10 |
933 self._handle_is_relation_sql(cnx, 'INSERT INTO cw_source_relation(eid_from,eid_to) VALUES (%s,%s)', |
933 self._handle_is_relation_sql(cnx, 'INSERT INTO cw_source_relation(eid_from,eid_to) VALUES (%s,%s)', |
934 (entity.eid, source.eid)) |
934 (entity.eid, source.eid)) |
935 # now we can update the full text index |
935 # now we can update the full text index |
936 if self.do_fti and self.need_fti_indexation(entity.cw_etype): |
936 if self.need_fti_indexation(entity.cw_etype): |
937 self.index_entity(cnx, entity=entity) |
937 self.index_entity(cnx, entity=entity) |
938 |
938 |
939 def update_info(self, cnx, entity, need_fti_update): |
939 def update_info(self, cnx, entity, need_fti_update): |
940 """mark entity as being modified, fulltext reindex if needed""" |
940 """mark entity as being modified, fulltext reindex if needed""" |
941 if self.do_fti and need_fti_update: |
941 if need_fti_update: |
942 # reindex the entity only if this query is updating at least |
942 # reindex the entity only if this query is updating at least |
943 # one indexable attribute |
943 # one indexable attribute |
944 self.index_entity(cnx, entity=entity) |
944 self.index_entity(cnx, entity=entity) |
945 |
945 |
946 def delete_info_multi(self, cnx, entities): |
946 def delete_info_multi(self, cnx, entities): |
1332 |
1332 |
1333 def index_entity(self, cnx, entity): |
1333 def index_entity(self, cnx, entity): |
1334 """create an operation to [re]index textual content of the given entity |
1334 """create an operation to [re]index textual content of the given entity |
1335 on commit |
1335 on commit |
1336 """ |
1336 """ |
1337 FTIndexEntityOp.get_instance(cnx).add_data(entity.eid) |
1337 if self.do_fti: |
|
1338 FTIndexEntityOp.get_instance(cnx).add_data(entity.eid) |
1338 |
1339 |
1339 def fti_unindex_entities(self, cnx, entities): |
1340 def fti_unindex_entities(self, cnx, entities): |
1340 """remove text content for entities from the full text index |
1341 """remove text content for entities from the full text index |
1341 """ |
1342 """ |
1342 cursor = cnx.cnxset.cu |
1343 cursor = cnx.cnxset.cu |
1399 |
1400 |
1400 CREATE TABLE entities ( |
1401 CREATE TABLE entities ( |
1401 eid INTEGER PRIMARY KEY NOT NULL, |
1402 eid INTEGER PRIMARY KEY NOT NULL, |
1402 type VARCHAR(64) NOT NULL, |
1403 type VARCHAR(64) NOT NULL, |
1403 asource VARCHAR(128) NOT NULL, |
1404 asource VARCHAR(128) NOT NULL, |
1404 extid VARCHAR(256) UNIQUE |
1405 extid VARCHAR(256) |
1405 );; |
1406 );; |
1406 CREATE INDEX entities_type_idx ON entities(type);; |
1407 CREATE INDEX entities_type_idx ON entities(type);; |
1407 CREATE TABLE moved_entities ( |
1408 CREATE TABLE moved_entities ( |
1408 eid INTEGER PRIMARY KEY NOT NULL, |
1409 eid INTEGER PRIMARY KEY NOT NULL, |
1409 extid VARCHAR(256) UNIQUE |
1410 extid VARCHAR(256) UNIQUE NOT NULL |
1410 );; |
1411 );; |
1411 |
1412 |
1412 CREATE TABLE transactions ( |
1413 CREATE TABLE transactions ( |
1413 tx_uuid CHAR(32) PRIMARY KEY NOT NULL, |
1414 tx_uuid CHAR(32) PRIMARY KEY NOT NULL, |
1414 tx_user INTEGER NOT NULL, |
1415 tx_user INTEGER NOT NULL, |
1457 FOR EACH ROW BEGIN |
1458 FOR EACH ROW BEGIN |
1458 DELETE FROM tx_entity_actions WHERE tx_uuid=OLD.tx_uuid; |
1459 DELETE FROM tx_entity_actions WHERE tx_uuid=OLD.tx_uuid; |
1459 DELETE FROM tx_relation_actions WHERE tx_uuid=OLD.tx_uuid; |
1460 DELETE FROM tx_relation_actions WHERE tx_uuid=OLD.tx_uuid; |
1460 END;; |
1461 END;; |
1461 ''' |
1462 ''' |
|
1463 schema += ';;'.join(helper.sqls_create_multicol_unique_index('entities', ['extid'])) |
|
1464 schema += ';;\n' |
1462 return schema |
1465 return schema |
1463 |
1466 |
1464 |
1467 |
1465 def sql_drop_schema(driver): |
1468 def sql_drop_schema(driver): |
1466 helper = get_db_helper(driver) |
1469 helper = get_db_helper(driver) |
1467 return """ |
1470 return """ |
|
1471 %s; |
1468 %s |
1472 %s |
1469 DROP TABLE entities; |
1473 DROP TABLE entities; |
1470 DROP TABLE tx_entity_actions; |
1474 DROP TABLE tx_entity_actions; |
1471 DROP TABLE tx_relation_actions; |
1475 DROP TABLE tx_relation_actions; |
1472 DROP TABLE transactions; |
1476 DROP TABLE transactions; |
1473 """ % helper.sql_drop_numrange('entities_id_seq') |
1477 """ % (';'.join(helper.sqls_drop_multicol_unique_index('entities', ['extid'])), |
|
1478 helper.sql_drop_numrange('entities_id_seq')) |
1474 |
1479 |
1475 |
1480 |
1476 def grant_schema(user, set_owner=True): |
1481 def grant_schema(user, set_owner=True): |
1477 result = '' |
1482 result = '' |
1478 for table in ('entities', 'entities_id_seq', |
1483 for table in ('entities', 'entities_id_seq', |
1718 sequences, numranges, tables, table_chunks = self.read_metadata(archive, backupfile) |
1723 sequences, numranges, tables, table_chunks = self.read_metadata(archive, backupfile) |
1719 for seq in sequences: |
1724 for seq in sequences: |
1720 self.logger.info('restoring sequence %s', seq) |
1725 self.logger.info('restoring sequence %s', seq) |
1721 self.read_sequence(archive, seq) |
1726 self.read_sequence(archive, seq) |
1722 for numrange in numranges: |
1727 for numrange in numranges: |
1723 self.logger.info('restoring numrange %s', seq) |
1728 self.logger.info('restoring numrange %s', numrange) |
1724 self.read_numrange(archive, numrange) |
1729 self.read_numrange(archive, numrange) |
1725 for table in tables: |
1730 for table in tables: |
1726 self.logger.info('restoring table %s', table) |
1731 self.logger.info('restoring table %s', table) |
1727 self.read_table(archive, table, sorted(table_chunks[table])) |
1732 self.read_table(archive, table, sorted(table_chunks[table])) |
1728 self.cnx.close() |
1733 self.cnx.close() |