-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add region to database * make function names and help more descriptive
- Loading branch information
Showing
13 changed files
with
325 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import csv | ||
|
||
from mirrors_qa_backend import logger | ||
from mirrors_qa_backend.db import Session | ||
from mirrors_qa_backend.db.country import create_country | ||
from mirrors_qa_backend.db.region import create_region | ||
from mirrors_qa_backend.schemas import Country, Region | ||
|
||
|
||
def create_regions_and_countries(countries: list[Country]) -> None: | ||
"""Create the region and associated countries in the database.""" | ||
with Session.begin() as session: | ||
for country in countries: | ||
db_country = create_country( | ||
session, | ||
country_code=country.code, | ||
country_name=country.name, | ||
) | ||
if country.region: | ||
db_region = create_region( | ||
session, | ||
region_code=country.region.code, | ||
region_name=country.region.name, | ||
) | ||
db_country.region = db_region | ||
session.add(db_country) | ||
|
||
|
||
def extract_country_regions_from_csv(csv_data: list[str]) -> list[Country]: | ||
regions: list[Country] = [] | ||
for row in csv.DictReader(csv_data): | ||
country_code = row["country_iso_code"] | ||
country_name = row["country_name"] | ||
region_code = row["continent_code"] | ||
region_name = row["continent_name"] | ||
if all([country_code, country_name, region_code, region_name]): | ||
regions.append( | ||
Country( | ||
code=country_code.lower(), | ||
name=country_name.title(), | ||
region=Region( | ||
code=region_code.lower(), | ||
name=region_name.title(), | ||
), | ||
) | ||
) | ||
else: | ||
logger.critical( | ||
f"Skipping row with missing entries: country_code: {country_code}, " | ||
f"country_name: {country_name}, region_code: {region_code}, " | ||
f"region_name: {region_name}" | ||
) | ||
return regions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from sqlalchemy import select | ||
from sqlalchemy.dialects.postgresql import insert | ||
from sqlalchemy.orm import Session as OrmSession | ||
|
||
from mirrors_qa_backend.db.exceptions import RecordDoesNotExistError | ||
from mirrors_qa_backend.db.models import Country, Region | ||
|
||
|
||
def get_countries_for(session: OrmSession, region_code: str) -> list[Country]: | ||
"""Get countries belonging to the provided region.""" | ||
|
||
return list( | ||
session.scalars(select(Country).where(Country.region_code == region_code)).all() | ||
) | ||
|
||
|
||
def get_region_or_none(session: OrmSession, region_code: str) -> Region | None: | ||
return session.scalars( | ||
select(Region).where(Region.code == region_code) | ||
).one_or_none() | ||
|
||
|
||
def get_region(session: OrmSession, region_code: str) -> Region: | ||
if region := get_region_or_none(session, region_code): | ||
return region | ||
raise RecordDoesNotExistError(f"Region with code {region_code} does not exist.") | ||
|
||
|
||
def create_region(session: OrmSession, *, region_code: str, region_name: str) -> Region: | ||
"""Creates a new continental region in the database.""" | ||
session.execute( | ||
insert(Region) | ||
.values(code=region_code, name=region_name) | ||
.on_conflict_do_nothing(index_elements=["code"]) | ||
) | ||
return get_region(session, region_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
backend/src/mirrors_qa_backend/migrations/versions/074ae280bb70_introduce_regions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""introduce regions | ||
Revision ID: 074ae280bb70 | ||
Revises: 17d587447299 | ||
Create Date: 2024-08-22 11:57:17.239215 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "074ae280bb70" | ||
down_revision = "17d587447299" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"region", | ||
sa.Column("code", sa.String(), nullable=False), | ||
sa.Column("name", sa.String(), nullable=False), | ||
sa.PrimaryKeyConstraint("code", name=op.f("pk_region")), | ||
) | ||
op.add_column("country", sa.Column("region_code", sa.String(), nullable=True)) | ||
op.create_foreign_key( | ||
op.f("fk_country_region_code_region"), | ||
"country", | ||
"region", | ||
["region_code"], | ||
["code"], | ||
) | ||
op.add_column("mirror", sa.Column("region_code", sa.String(), nullable=True)) | ||
op.add_column("mirror", sa.Column("country_code", sa.String(), nullable=True)) | ||
op.create_foreign_key( | ||
op.f("fk_mirror_country_code_country"), | ||
"mirror", | ||
"country", | ||
["country_code"], | ||
["code"], | ||
) | ||
op.create_foreign_key( | ||
op.f("fk_mirror_region_code_region"), | ||
"mirror", | ||
"region", | ||
["region_code"], | ||
["code"], | ||
) | ||
op.drop_column("mirror", "region") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column( | ||
"mirror", sa.Column("region", sa.VARCHAR(), autoincrement=False, nullable=True) | ||
) | ||
op.drop_constraint( | ||
op.f("fk_mirror_region_code_region"), "mirror", type_="foreignkey" | ||
) | ||
op.drop_constraint( | ||
op.f("fk_mirror_country_code_country"), "mirror", type_="foreignkey" | ||
) | ||
op.drop_column("mirror", "country_code") | ||
op.drop_column("mirror", "region_code") | ||
op.drop_constraint( | ||
op.f("fk_country_region_code_region"), "country", type_="foreignkey" | ||
) | ||
op.drop_column("country", "region_code") | ||
op.drop_table("region") | ||
# ### end Alembic commands ### |
Oops, something went wrong.