Skip to content

Commit d3afe5d

Browse files
authored
feature/pdct 1394 step 2 use new locations for geographies (#199)
* remove use of geography_id * bump admin version * Remove use of geography_id on family update * fix test * fix tests
1 parent eeaf0b3 commit d3afe5d

File tree

4 files changed

+126
-102
lines changed

4 files changed

+126
-102
lines changed

app/repository/family.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from db_client.models.organisation.users import Organisation
2323
from sqlalchemy import Column, and_
2424
from sqlalchemy import delete as db_delete
25-
from sqlalchemy import desc, or_
25+
from sqlalchemy import desc, func, or_
2626
from sqlalchemy import update as db_update
2727
from sqlalchemy.exc import NoResultFound, OperationalError
2828
from sqlalchemy.orm import Query, Session
@@ -34,29 +34,38 @@
3434

3535
_LOGGER = logging.getLogger(__name__)
3636

37-
FamilyGeoMetaOrg = Tuple[Family, Geography, FamilyMetadata, Corpus, Organisation]
37+
FamilyGeoMetaOrg = Tuple[Family, str, FamilyMetadata, Corpus, Organisation]
3838

3939

4040
def _get_query(db: Session) -> Query:
4141
# NOTE: SqlAlchemy will make a complete hash of query generation
4242
# if columns are used in the query() call. Therefore, entire
4343
# 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+
4454
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
4756
.join(FamilyMetadata, FamilyMetadata.family_import_id == Family.import_id)
4857
.join(FamilyCorpus, FamilyCorpus.family_import_id == Family.import_id)
4958
.join(Corpus, Corpus.import_id == FamilyCorpus.corpus_import_id)
5059
.join(Organisation, Corpus.organisation_id == Organisation.id)
60+
.filter(geo_subquery.c.family_import_id == Family.import_id) # type: ignore
5161
)
5262

5363

5464
def _family_to_dto(
5565
db: Session, fam_geo_meta_corp_org: FamilyGeoMetaOrg
5666
) -> FamilyReadDTO:
57-
fam, geo, meta, corpus, org = fam_geo_meta_corp_org
67+
fam, geo_value, meta, corpus, org = fam_geo_meta_corp_org
5868

59-
geo_value = cast(str, geo.value)
6069
metadata = cast(dict, meta.value)
6170
org = cast(str, org.name)
6271
return FamilyReadDTO(
@@ -102,10 +111,18 @@ def _update_intention(
102111
]
103112
update_collections = set(original_collections) != set(family.collections)
104113
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+
)
105122
update_basics = (
106123
update_title
124+
or update_geo
107125
or original_family.description != family.summary
108-
or original_family.geography_id != geo_id
109126
or original_family.family_category != family.category
110127
)
111128
existing_metadata = (
@@ -255,12 +272,11 @@ def update(db: Session, import_id: str, family: FamilyWriteDTO, geo_id: int) ->
255272
.values(
256273
title=new_values["title"],
257274
description=new_values["summary"],
258-
geography_id=geo_id,
259275
family_category=new_values["category"],
260276
)
261277
)
262278
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
264280
result = db.execute(
265281
db_update(FamilyGeography)
266282
.where(FamilyGeography.family_import_id == import_id)
@@ -358,11 +374,11 @@ def create(db: Session, family: FamilyCreateDTO, geo_id: int, org_id: int) -> st
358374
import_id=import_id,
359375
title=family.title,
360376
description=family.summary,
361-
geography_id=geo_id,
362377
family_category=family.category,
363378
)
364379
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
366382
db.add(FamilyGeography(family_import_id=import_id, geography_id=geo_id))
367383

368384
# Add corpus - family link.

0 commit comments

Comments
 (0)