Skip to content

Commit

Permalink
update migration + change where extent is saved
Browse files Browse the repository at this point in the history
  • Loading branch information
mariogiampieri committed Oct 28, 2024
1 parent f7d40ee commit fac39b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 60 deletions.
81 changes: 24 additions & 57 deletions backend/app/alembic/versions/dc391733e10a_add_extent_to_gerrydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,60 @@

from alembic import op
import sqlalchemy as sa
from app.models import GerryDBTable
from app.models import DistrictrMap

# revision identifiers, used by Alembic.
revision: str = "dc391733e10a"
down_revision: Union[str, None] = "5ab466c5650a"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
depends_on = ("167892041d95",) # geometry col renaming


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
# Make column nullable to avoid errors during initial insert
op.add_column(
"gerrydbtable", sa.Column("extent", sa.ARRAY(sa.Float()), nullable=True)
"districtrmap", sa.Column("extent", sa.ARRAY(sa.Float()), nullable=True)
)

# Use a session to interact with the database
bind = op.get_bind()
Session = sa.orm.sessionmaker(bind=bind)
session = Session()

# Step 1: Retrieve all table names from public.gerrydbtable
table_names = session.execute(sa.select(GerryDBTable.name)).scalars().all()
# retreive table names from the districtrmap parent_layer field
table_names = session.execute(sa.select(DistrictrMap.parent_layer)).scalars().all()

if not table_names:
raise ValueError("No matching table names found in the index table")

def column_exists(table_name, column_name):
column_check_query = sa.text(
f"""
SELECT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'gerrydb'
AND table_name = :table_name
AND column_name = :column_name
)
"""
)
result = bind.execute(
column_check_query, {"table_name": table_name, "column_name": column_name}
).scalar()
return result

cases = []
for table_name in table_names:
has_geometry = column_exists(table_name, "geometry")
has_geography = column_exists(table_name, "geography")

if has_geometry:
geom_column = "geometry"
elif has_geography:
geom_column = "geography"
# Add the CASE statement with the correct column name

if has_geometry or has_geography:
case = f"""
WHEN name = '{table_name}' THEN (
SELECT
ARRAY[
ST_XMin(ST_Extent(ST_Transform({geom_column}, 4326))),
ST_YMin(ST_Extent(ST_Transform({geom_column}, 4326))),
ST_XMax(ST_Extent(ST_Transform({geom_column}, 4326))),
ST_YMax(ST_Extent(ST_Transform({geom_column}, 4326)))
]
FROM gerrydb."{table_name}"
)
"""

else:
# just skip for now and return generic placeholder array
print(f"No geometry or geography column found in table '{table_name}'.")
case = f"""
WHEN name = '{table_name}' THEN ARRAY[0, 0, 0, 0]
"""
cases.append(case)
case = f"""
WHEN districtrmap."parent_layer" = '{table_name}' THEN (
SELECT
ARRAY[
ST_XMin(ST_Extent(ST_Transform(geometry, 4326))),
ST_YMin(ST_Extent(ST_Transform(geometry, 4326))),
ST_XMax(ST_Extent(ST_Transform(geometry, 4326))),
ST_YMax(ST_Extent(ST_Transform(geometry, 4326)))
]
FROM gerrydb."{table_name}", public.districtrmap
)
"""

cases.append(case)
# Combine all cases into a single SQL statement
case_statement = " ".join(cases)

# Execute a single UPDATE statement
update_query = sa.text(
f"""
UPDATE gerrydbtable
UPDATE districtrmap
SET extent = CASE
{case_statement}
ELSE extent -- Leave unchanged if no match; this should never happen
-- ELSE ARRAY[0, 0, 0, 0] if this fails, there is no
-- matching table from parent_layer and that's a problem
END;
"""
)
Expand All @@ -105,13 +72,13 @@ def column_exists(table_name, column_name):

# Make the `extent` column non-nullable
op.alter_column(
"gerrydbtable", "extent", existing_type=sa.ARRAY(sa.Float()), nullable=False
"districtrmap", "extent", existing_type=sa.ARRAY(sa.Float()), nullable=False
)

session.commit()


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("gerrydbtable", "extent")
op.drop_column("districtrmap", "extent")
# ### end Alembic commands ###
4 changes: 1 addition & 3 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ class DistrictrMap(TimeStampMixin, SQLModel, table=True):
String, ForeignKey("gerrydbtable.name"), default=None, nullable=True
)
)
extent: list[float] = Field(
sa_column=Column(ARRAY(Float), nullable=False)
) # at the gdb table level and not the doc level
extent: list[float] = Field(sa_column=Column(ARRAY(Float), nullable=False))
# schema? will need to contrain the schema
# where does this go?
# when you create the view, pull the columns that you need
Expand Down

0 comments on commit fac39b1

Please sign in to comment.