Skip to content

Commit

Permalink
Feature/pdct 937 change the schema to remove unused link tables (#122)
Browse files Browse the repository at this point in the history
* Bump dbclient version to 3.4.0

* Remove all references to MetadataTax, FamilyOrg & MetadataOrg

* Bump package version to 2.3.0

* Add more options for type of change

* Fix assertion to look at corpus.import_id

* Check for one or none and then not none
  • Loading branch information
katybaulch authored Apr 8, 2024
1 parent cfc1f5f commit 90aaa59
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Please select the option(s) below that are most relevant:
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] GitHub workflow update
- [ ] Documentation update
- [ ] Remove legacy code

## How Has This Been Tested?

Expand Down
40 changes: 8 additions & 32 deletions app/repository/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
Family,
FamilyCorpus,
FamilyDocument,
FamilyOrganisation,
FamilyStatus,
Slug,
)
from db_client.models.dfce.geography import Geography
from db_client.models.dfce.metadata import FamilyMetadata, MetadataOrganisation
from db_client.models.dfce.metadata import FamilyMetadata
from db_client.models.organisation.corpus import Corpus
from db_client.models.organisation.counters import CountedEntity
from db_client.models.organisation.users import Organisation
Expand Down Expand Up @@ -50,22 +49,6 @@ def _get_query(db: Session) -> Query:
)


def _family_org_from_dto(
dto: FamilyCreateDTO, geo_id: int, org_id: int
) -> Tuple[Family, Organisation]:
return (
Family(
import_id="",
title=dto.title,
description=dto.summary,
geography_id=geo_id,
family_category=dto.category,
),
# TODO: Remove use of FamilyOrganisation
FamilyOrganisation(family_import_id="", organisation_id=org_id),
)


def _family_to_dto(db: Session, fam_geo_meta_org: FamilyGeoMetaOrg) -> FamilyReadDTO:
f = fam_geo_meta_org[0]
geo_value = cast(str, fam_geo_meta_org[1].value)
Expand Down Expand Up @@ -343,16 +326,16 @@ def create(db: Session, family: FamilyCreateDTO, geo_id: int, org_id: int) -> st
:return bool: True if new Family was created otherwise False.
"""
try:
new_family, new_fam_org = _family_org_from_dto(family, geo_id, org_id)
new_family.import_id = cast(
Column, generate_import_id(db, CountedEntity.Family, org_id)
import_id = cast(Column, generate_import_id(db, CountedEntity.Family, org_id))
new_family = Family(
import_id=import_id,
title=family.title,
description=family.summary,
geography_id=geo_id,
family_category=family.category,
)
db.add(new_family)

# Old schema (to be removed in PDCT-937).
new_fam_org.family_import_id = new_family.import_id
db.add(new_fam_org)

# New schema.
new_fam_corpus = db.query(Corpus).filter(Corpus.organisation_id == org_id).one()
db.add(
Expand Down Expand Up @@ -380,16 +363,9 @@ def create(db: Session, family: FamilyCreateDTO, geo_id: int, org_id: int) -> st
# TODO Validate that the metadata being added conforms to corpus type. PDCT-945

# Add the metadata
# tax to be removed in PDCT-937.
tax = (
db.query(MetadataOrganisation)
.filter(MetadataOrganisation.organisation_id == org_id) # TODO: Remove
.one()
)
db.add(
FamilyMetadata(
family_import_id=new_family.import_id,
taxonomy_id=tax.taxonomy_id, # TODO Remove as part PDCT-937
value=family.metadata,
)
)
Expand Down
9 changes: 4 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "admin_backend"
version = "2.2.1"
version = "2.3.0"
description = ""
authors = ["CPR-dev-team <[email protected]>"]
packages = [{ include = "app" }, { include = "tests" }]
Expand Down Expand Up @@ -29,7 +29,7 @@ boto3 = "^1.28.46"
moto = "^4.2.2"
types-sqlalchemy = "^1.4.53.38"
urllib3 = "^1.26.17"
db-client = { git = "https://github.com/climatepolicyradar/navigator-db-client.git", tag = "v3.3.0" }
db-client = { git = "https://github.com/climatepolicyradar/navigator-db-client.git", tag = "v3.4.0" }

[tool.poetry.dev-dependencies]
pre-commit = "^2.17.0"
Expand Down
20 changes: 7 additions & 13 deletions tests/integration_tests/family/test_create.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

from db_client.models.dfce.collection import CollectionFamily
from db_client.models.dfce.family import Family, FamilyCorpus, FamilyOrganisation, Slug
from db_client.models.dfce.family import Family, FamilyCorpus, Slug
from db_client.models.dfce.metadata import FamilyMetadata
from db_client.models.organisation.corpus import Corpus
from fastapi import status
Expand Down Expand Up @@ -51,16 +51,6 @@ def test_create_family(client: TestClient, data_db: Session, user_header_token):
assert len(db_collection) == 1
assert db_collection[0].collection_import_id == "C.0.0.3"

# Old schema test.
org_id = (
data_db.query(FamilyOrganisation.organisation_id) # To be removed
.filter(
FamilyOrganisation.family_import_id == expected_import_id
) # To be removed
.scalar()
)
assert org_id is not None

# New schema tests.
fc = (
data_db.query(FamilyCorpus)
Expand All @@ -69,8 +59,12 @@ def test_create_family(client: TestClient, data_db: Session, user_header_token):
)
assert len(fc) == 1
assert fc[-1].corpus_import_id is not None
corpus = data_db.query(Corpus).filter(Corpus.organisation_id == org_id).one()
assert fc[-1].corpus_import_id == corpus.import_id
corpus = (
data_db.query(Corpus)
.filter(Corpus.import_id == fc[-1].corpus_import_id)
.one_or_none()
)
assert corpus is not None


def test_create_family_when_not_authorised(client: TestClient, data_db: Session):
Expand Down
32 changes: 1 addition & 31 deletions tests/integration_tests/setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
FamilyEvent,
Slug,
)
from db_client.models.dfce.metadata import (
FamilyMetadata,
MetadataOrganisation,
MetadataTaxonomy,
)
from db_client.models.dfce.metadata import FamilyMetadata
from db_client.models.document.physical_document import (
LanguageSource,
PhysicalDocument,
Expand Down Expand Up @@ -245,15 +241,6 @@ def _setup_organisation(test_db: Session) -> tuple[int, int]:
# Now an organisation
org = test_db.query(Organisation).filter(Organisation.name == "CCLW").one()

# Remove default taxonomy from CCLW - old schema
# TODO: Remove this deletion in Milestone 4
mo = (
test_db.query(MetadataOrganisation) # remove
.filter(MetadataOrganisation.organisation_id == org.id) # remove
.one()
)
test_db.delete(mo)

another_org = Organisation(
name="Another org",
description="because we will have more than one org",
Expand Down Expand Up @@ -367,22 +354,6 @@ def _setup_family_data(
"allowed_values": [],
},
}
dummy_tax = MetadataTaxonomy(
id=99, description="to go", valid_metadata=valid_metadata
)

test_db.add(dummy_tax)
test_db.flush()

# Old Schema modification (to be removed)
# MetadataOrganisation
mo = MetadataOrganisation(taxonomy_id=dummy_tax.id, organisation_id=default_org_id)
test_db.add(mo)
test_db.flush()

omo = MetadataOrganisation(taxonomy_id=dummy_tax.id, organisation_id=other_org_id)
test_db.add(omo)
# End of "to be removed"

# New Schema modification
# CorpusType
Expand Down Expand Up @@ -446,7 +417,6 @@ def _setup_family_data(
test_db.add(
FamilyMetadata(
family_import_id=data["import_id"],
taxonomy_id=dummy_tax.id, # soon no longer needed
value=data["metadata"],
)
)
Expand Down

0 comments on commit 90aaa59

Please sign in to comment.