|
22 | 22 | from db_client.models.organisation.users import Organisation
|
23 | 23 | from sqlalchemy import Column, and_
|
24 | 24 | from sqlalchemy import delete as db_delete
|
25 |
| -from sqlalchemy import desc, or_ |
| 25 | +from sqlalchemy import desc, func, or_ |
26 | 26 | from sqlalchemy import update as db_update
|
27 | 27 | from sqlalchemy.exc import NoResultFound, OperationalError
|
28 | 28 | from sqlalchemy.orm import Query, Session
|
|
34 | 34 |
|
35 | 35 | _LOGGER = logging.getLogger(__name__)
|
36 | 36 |
|
37 |
| -FamilyGeoMetaOrg = Tuple[Family, Geography, FamilyMetadata, Corpus, Organisation] |
| 37 | +FamilyGeoMetaOrg = Tuple[Family, str, FamilyMetadata, Corpus, Organisation] |
38 | 38 |
|
39 | 39 |
|
40 | 40 | def _get_query(db: Session) -> Query:
|
41 | 41 | # NOTE: SqlAlchemy will make a complete hash of query generation
|
42 | 42 | # if columns are used in the query() call. Therefore, entire
|
43 | 43 | # objects are returned.
|
| 44 | + geo_subquery = ( |
| 45 | + db.query( |
| 46 | + func.min(Geography.value).label("value"), |
| 47 | + FamilyGeography.family_import_id, |
| 48 | + ) |
| 49 | + .join(FamilyGeography, FamilyGeography.geography_id == Geography.id) |
| 50 | + .filter(FamilyGeography.family_import_id == Family.import_id) |
| 51 | + .group_by(Geography.value, FamilyGeography.family_import_id) |
| 52 | + ).subquery("geo_subquery") |
| 53 | + |
44 | 54 | return (
|
45 |
| - db.query(Family, Geography, FamilyMetadata, Corpus, Organisation) |
46 |
| - .join(Geography, Family.geography_id == Geography.id) |
| 55 | + db.query(Family, geo_subquery.c.value, FamilyMetadata, Corpus, Organisation) # type: ignore |
47 | 56 | .join(FamilyMetadata, FamilyMetadata.family_import_id == Family.import_id)
|
48 | 57 | .join(FamilyCorpus, FamilyCorpus.family_import_id == Family.import_id)
|
49 | 58 | .join(Corpus, Corpus.import_id == FamilyCorpus.corpus_import_id)
|
50 | 59 | .join(Organisation, Corpus.organisation_id == Organisation.id)
|
| 60 | + .filter(geo_subquery.c.family_import_id == Family.import_id) # type: ignore |
51 | 61 | )
|
52 | 62 |
|
53 | 63 |
|
54 | 64 | def _family_to_dto(
|
55 | 65 | db: Session, fam_geo_meta_corp_org: FamilyGeoMetaOrg
|
56 | 66 | ) -> FamilyReadDTO:
|
57 |
| - fam, geo, meta, corpus, org = fam_geo_meta_corp_org |
| 67 | + fam, geo_value, meta, corpus, org = fam_geo_meta_corp_org |
58 | 68 |
|
59 |
| - geo_value = cast(str, geo.value) |
60 | 69 | metadata = cast(dict, meta.value)
|
61 | 70 | org = cast(str, org.name)
|
62 | 71 | return FamilyReadDTO(
|
@@ -102,10 +111,18 @@ def _update_intention(
|
102 | 111 | ]
|
103 | 112 | update_collections = set(original_collections) != set(family.collections)
|
104 | 113 | update_title = cast(str, original_family.title) != family.title
|
| 114 | + # TODO: PDCT-1406: Properly implement multi-geography support |
| 115 | + update_geo = ( |
| 116 | + db.query(FamilyGeography) |
| 117 | + .filter(FamilyGeography.family_import_id == import_id) |
| 118 | + .one() |
| 119 | + .geography_id |
| 120 | + != geo_id |
| 121 | + ) |
105 | 122 | update_basics = (
|
106 | 123 | update_title
|
| 124 | + or update_geo |
107 | 125 | or original_family.description != family.summary
|
108 |
| - or original_family.geography_id != geo_id |
109 | 126 | or original_family.family_category != family.category
|
110 | 127 | )
|
111 | 128 | existing_metadata = (
|
@@ -255,12 +272,11 @@ def update(db: Session, import_id: str, family: FamilyWriteDTO, geo_id: int) ->
|
255 | 272 | .values(
|
256 | 273 | title=new_values["title"],
|
257 | 274 | description=new_values["summary"],
|
258 |
| - geography_id=geo_id, |
259 | 275 | family_category=new_values["category"],
|
260 | 276 | )
|
261 | 277 | )
|
262 | 278 | updates = result.rowcount # type: ignore
|
263 |
| - # TODO: PDCT-1326 - Stage 3 - update to not assume single value |
| 279 | + # TODO: PDCT-1406: Properly implement multi-geography support |
264 | 280 | result = db.execute(
|
265 | 281 | db_update(FamilyGeography)
|
266 | 282 | .where(FamilyGeography.family_import_id == import_id)
|
@@ -358,11 +374,11 @@ def create(db: Session, family: FamilyCreateDTO, geo_id: int, org_id: int) -> st
|
358 | 374 | import_id=import_id,
|
359 | 375 | title=family.title,
|
360 | 376 | description=family.summary,
|
361 |
| - geography_id=geo_id, |
362 | 377 | family_category=family.category,
|
363 | 378 | )
|
364 | 379 | db.add(new_family)
|
365 |
| - # TODO: PDCT-1326 - Stage 3 - update to not assume single value |
| 380 | + |
| 381 | + # TODO: PDCT-1406: Properly implement multi-geography support |
366 | 382 | db.add(FamilyGeography(family_import_id=import_id, geography_id=geo_id))
|
367 | 383 |
|
368 | 384 | # Add corpus - family link.
|
|
0 commit comments