Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bailliekova committed Jan 29, 2025
1 parent 6927fcb commit d11a400
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gerrydb_meta/crud/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def set_values(
# Add the new column values and invalidate the old ones where present.
geo_ids = [geo.geo_id for geo, _ in values]

#make sure partitions exist for all geos
# make sure partitions exist for all geos
for geo_id in set(geo_ids):
db.execute(create_column_value_partition_text(geo_id=geo_id))

Expand Down
6 changes: 2 additions & 4 deletions gerrydb_meta/crud/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def create_bulk(
"Cannot create geographies with duplicate paths.",
paths=[path for path in paths if paths.count(path) > 1],
)

with db.begin(nested=True):
geos = list(
db.scalars(
Expand Down Expand Up @@ -112,11 +112,9 @@ def create_bulk(
)
raise BulkCreateError(
"Failed to insert geometries. Geometries must be encoded in WKB format."
) from ex
) from ex

etag = self._update_etag(db, namespace)



db.flush()
return list(zip(geos, geo_versions)), etag
Expand Down
16 changes: 11 additions & 5 deletions gerrydb_meta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)
from gerrydb_meta.utils import create_column_value_partition_text

SCHEMA= "gerrydb"
SCHEMA = "gerrydb"
metadata_obj = MetaData(schema=SCHEMA)


Expand Down Expand Up @@ -373,10 +373,14 @@ def full_path(self):
"""Path with namespace prefix."""
return f"/{self.namespace.path}/{self.path}"


@event.listens_for(Geography, "after_insert")
def create_geo_partition_in_column_value(mapper, connection, geo):
geo_id=geo.geo_id
Session.object_session(geo).execute(create_column_value_partition_text(geo_id=geo_id))
geo_id = geo.geo_id
Session.object_session(geo).execute(
create_column_value_partition_text(geo_id=geo_id)
)


class GeoImport(Base):
__tablename__ = "geo_import"
Expand Down Expand Up @@ -553,8 +557,10 @@ class ColumnSetMember(Base):

class ColumnValue(Base):
__tablename__ = "column_value"
__table_args__ = (UniqueConstraint("col_id", "geo_id", "valid_from"),
{"postgresql_partition_by": "LIST (geo_id)" })
__table_args__ = (
UniqueConstraint("col_id", "geo_id", "valid_from"),
{"postgresql_partition_by": "LIST (geo_id)"},
)

col_id: Mapped[int] = mapped_column(
Integer,
Expand Down
7 changes: 4 additions & 3 deletions gerrydb_meta/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from sqlalchemy import text
from gerrydb_meta import models


def create_column_value_partition_text(geo_id: int):
table_name=models.ColumnValue.__table__.name
sql=f"CREATE TABLE IF NOT EXISTS {models.SCHEMA}.{table_name}_{geo_id} PARTITION OF {models.SCHEMA}.{table_name} FOR VALUES IN ({geo_id})"
return text(sql)
table_name = models.ColumnValue.__table__.name
sql = f"CREATE TABLE IF NOT EXISTS {models.SCHEMA}.{table_name}_{geo_id} PARTITION OF {models.SCHEMA}.{table_name} FOR VALUES IN ({geo_id})"
return text(sql)
13 changes: 8 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from gerrydb_meta.utils import create_column_value_partition_text
from sqlalchemy import text


def test_create_column_value_partition_text():
geo_id=42
got=create_column_value_partition_text(geo_id=geo_id)
wanted=text("CREATE TABLE IF NOT EXISTS gerrydb.column_value_42 PARTITION OF gerrydb.column_value FOR VALUES IN (42)")
#different object instances, so compare string form
assert str(got)==str(wanted)
geo_id = 42
got = create_column_value_partition_text(geo_id=geo_id)
wanted = text(
"CREATE TABLE IF NOT EXISTS gerrydb.column_value_42 PARTITION OF gerrydb.column_value FOR VALUES IN (42)"
)
# different object instances, so compare string form
assert str(got) == str(wanted)

0 comments on commit d11a400

Please sign in to comment.