diff --git a/arkindex_worker/cache.py b/arkindex_worker/cache.py index a6751c2d09d085118bfd72c4e237b07e8a365bc3..57823b7c75ea8907b832bbdfc1588bede9c88229 100644 --- a/arkindex_worker/cache.py +++ b/arkindex_worker/cache.py @@ -156,6 +156,7 @@ class CachedTranscriptionEntity(Model): entity = ForeignKeyField(CachedEntity, backref="transcription_entities") offset = IntegerField(constraints=[Check("offset >= 0")]) length = IntegerField(constraints=[Check("length > 0")]) + worker_version_id = UUIDField() class Meta: primary_key = CompositeKey("transcription", "entity") diff --git a/arkindex_worker/worker/entity.py b/arkindex_worker/worker/entity.py index fb8d9f0caae57fc492227746017dc2597af6c9fb..7dd5e85d294987701e258fee0869917cc7507128 100644 --- a/arkindex_worker/worker/entity.py +++ b/arkindex_worker/worker/entity.py @@ -111,6 +111,7 @@ class EntityMixin(object): "entity": entity, "length": length, "offset": offset, + "worker_version_id": self.worker_version_id, }, ) # TODO: Report transcription entity creation @@ -118,15 +119,13 @@ class EntityMixin(object): if self.use_cache: # Store transcription entity in local cache try: - to_insert = [ - { - "transcription": transcription, - "entity": entity, - "offset": offset, - "length": length, - } - ] - CachedTranscriptionEntity.insert_many(to_insert).execute() + CachedTranscriptionEntity.create( + transcription=transcription, + entity=entity, + offset=offset, + length=length, + worker_version_id=self.worker_version_id, + ) except IntegrityError as e: logger.warning( f"Couldn't save created transcription entity in local cache: {e}" diff --git a/tests/test_cache.py b/tests/test_cache.py index 127201901a11ee0cea1b29d8536da2cc27fb73e8..b034ed8b8846f732854550b60bfb460773f47e3d 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -58,7 +58,7 @@ def test_create_tables(tmp_path): CREATE TABLE "elements" ("id" TEXT NOT NULL PRIMARY KEY, "parent_id" TEXT, "type" VARCHAR(50) NOT NULL, "image_id" TEXT, "polygon" text, "initial" INTEGER NOT NULL, "worker_version_id" TEXT, FOREIGN KEY ("image_id") REFERENCES "images" ("id")) CREATE TABLE "entities" ("id" TEXT NOT NULL PRIMARY KEY, "type" VARCHAR(50) NOT NULL, "name" TEXT NOT NULL, "validated" INTEGER NOT NULL, "metas" text, "worker_version_id" TEXT NOT NULL) CREATE TABLE "images" ("id" TEXT NOT NULL PRIMARY KEY, "width" INTEGER NOT NULL, "height" INTEGER NOT NULL, "url" TEXT NOT NULL) -CREATE TABLE "transcription_entities" ("transcription_id" TEXT NOT NULL, "entity_id" TEXT NOT NULL, "offset" INTEGER NOT NULL CHECK (offset >= 0), "length" INTEGER NOT NULL CHECK (length > 0), PRIMARY KEY ("transcription_id", "entity_id"), FOREIGN KEY ("transcription_id") REFERENCES "transcriptions" ("id"), FOREIGN KEY ("entity_id") REFERENCES "entities" ("id")) +CREATE TABLE "transcription_entities" ("transcription_id" TEXT NOT NULL, "entity_id" TEXT NOT NULL, "offset" INTEGER NOT NULL CHECK (offset >= 0), "length" INTEGER NOT NULL CHECK (length > 0), "worker_version_id" TEXT NOT NULL, PRIMARY KEY ("transcription_id", "entity_id"), FOREIGN KEY ("transcription_id") REFERENCES "transcriptions" ("id"), FOREIGN KEY ("entity_id") REFERENCES "entities" ("id")) CREATE TABLE "transcriptions" ("id" TEXT NOT NULL PRIMARY KEY, "element_id" TEXT NOT NULL, "text" TEXT NOT NULL, "confidence" REAL NOT NULL, "worker_version_id" TEXT NOT NULL, FOREIGN KEY ("element_id") REFERENCES "elements" ("id"))""" actual_schema = "\n".join( diff --git a/tests/test_elements_worker/test_entities.py b/tests/test_elements_worker/test_entities.py index 4bc557d4a1d9d31de678507149ebdc92e83111f9..8b16ba15fe409fe0c19568fac7f3cfb4cdcef83b 100644 --- a/tests/test_elements_worker/test_entities.py +++ b/tests/test_elements_worker/test_entities.py @@ -449,6 +449,7 @@ def test_create_transcription_entity(responses, mock_elements_worker): "entity": "11111111-1111-1111-1111-111111111111", "offset": 5, "length": 10, + "worker_version_id": "12341234-1234-1234-1234-123412341234", } @@ -504,6 +505,7 @@ def test_create_transcription_entity_with_cache( "entity": "11111111-1111-1111-1111-111111111111", "offset": 5, "length": 10, + "worker_version_id": "12341234-1234-1234-1234-123412341234", } # Check that created transcription entity was properly stored in SQLite cache @@ -513,5 +515,6 @@ def test_create_transcription_entity_with_cache( entity=UUID("11111111-1111-1111-1111-111111111111"), offset=5, length=10, + worker_version_id=UUID("12341234-1234-1234-1234-123412341234"), ) ]