Skip to content

Commit

Permalink
Merge pull request #4 from mggg/delete-columns
Browse files Browse the repository at this point in the history
Drop primary key and json value columns from column_value table
  • Loading branch information
peterrrock2 authored Oct 22, 2024
2 parents ed77d9f + 2d6df9f commit ec8cded
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
21 changes: 16 additions & 5 deletions gerrydb_meta/crud/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,12 @@ def set_values(

# Add the new column values and invalidate the old ones where present.
geo_ids = [geo.geo_id for geo, _ in values]
with_values = (
db.query(models.ColumnValue.val_id)
with_tuples = (
db.query(
models.ColumnValue.col_id,
models.ColumnValue.geo_id,
models.ColumnValue.valid_from,
)
.filter(
models.ColumnValue.col_id == col.col_id,
models.ColumnValue.geo_id.in_(geo_ids),
Expand All @@ -212,6 +216,8 @@ def set_values(
.all()
)

with_values = ["_".join([str(val) for val in tup]) for tup in with_tuples]

with db.begin(nested=True):
db.execute(insert(models.ColumnValue), rows)
# Optimization: most column values are only set once, so we don't
Expand All @@ -220,9 +226,14 @@ def set_values(
db.execute(
update(models.ColumnValue)
.where(
models.ColumnValue.val_id.in_(
val.val_id for val in with_values
),
"_".join(
[
str(models.ColumnValue.col_id),
str(models.ColumnValue.geo_id),
str(models.ColumnValue.valid_from),
]
)
in with_values
)
.values(valid_to=now)
)
Expand Down
5 changes: 3 additions & 2 deletions gerrydb_meta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,31 +549,32 @@ class ColumnValue(Base):
__tablename__ = "column_value"
__table_args__ = (UniqueConstraint("col_id", "geo_id", "valid_from"),)

val_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
col_id: Mapped[int] = mapped_column(
Integer,
ForeignKey("column.col_id"),
nullable=False,
primary_key=True,
)
geo_id: Mapped[int] = mapped_column(
Integer,
ForeignKey("geography.geo_id"),
nullable=False,
primary_key=True,
)
meta_id: Mapped[int] = mapped_column(
Integer, ForeignKey("meta.meta_id"), nullable=False
)
valid_from: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
primary_key=True,
)
valid_to: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=True)

val_float: Mapped[float] = mapped_column(postgresql.DOUBLE_PRECISION, nullable=True)
val_int: Mapped[int] = mapped_column(BigInteger, nullable=True)
val_str: Mapped[str] = mapped_column(Text, nullable=True)
val_bool: Mapped[bool] = mapped_column(Boolean, nullable=True)
val_json: Mapped[Any] = mapped_column(postgresql.JSONB, nullable=True)

meta: Mapped[ObjectMeta] = relationship("ObjectMeta")

Expand Down
4 changes: 2 additions & 2 deletions poetry.lock

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

3 changes: 1 addition & 2 deletions tests/api/test_column_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
(ColumnType.FLOAT, 1.0, float("inf")),
(ColumnType.BOOL, True, False),
(ColumnType.STR, "", "abc"),
(ColumnType.JSON, {"key": "value"}, [1, 2, "3"]),
],
ids=("int", "float", "bool", "str", "json"),
ids=("int", "float", "bool", "str"),
)
def test_api_column_value_set__two_geos(ctx_public_namespace_read_write, typed_vals):
col_type, val1, val2 = typed_vals
Expand Down
3 changes: 0 additions & 3 deletions tests/crud/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ def test_crud_column_set_values(db_with_meta):
assert cols_list[0].val_bool is None
assert cols_list[1].val_bool is None

assert cols_list[0].val_json is None
assert cols_list[1].val_json is None


def test_crud_column_patch(db_with_meta):
db, meta = db_with_meta
Expand Down

0 comments on commit ec8cded

Please sign in to comment.