Skip to content

Commit

Permalink
Fix update.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktlim committed May 2, 2024
1 parent 80352ae commit 344c01f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
8 changes: 6 additions & 2 deletions python/lsst/consdb/pqclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def insert_flexible_metadata(
obs_type: str,
obs_id: int,
values: dict[str, Any],
allow_update: bool = False,
**kwargs,
) -> requests.Response:
values.update(kwargs)
Expand All @@ -214,6 +215,8 @@ def insert_flexible_metadata(
"obs",
quote(str(obs_id)),
)
if allow_update:
url += "?u=1"
return self._handle_post(url, data)

def insert(
Expand All @@ -227,8 +230,9 @@ def insert(
) -> requests.Response:
values.update(kwargs)
data = {"table": table, "obs_id": obs_id, "values": values}
op = "upsert" if allow_update else "insert"
url = urljoin(self.url, op, quote(instrument))
url = urljoin(self.url, "insert", quote(instrument), quote(table), "obs", quote(str(obs_id)))
if allow_update:
url += "?u=1"
return self._handle_post(url, data)

def query(self, query: str) -> Table:
Expand Down
42 changes: 20 additions & 22 deletions python/lsst/consdb/pqserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def get_flexible_metadata(instrument: str, obs_type: str, obs_id: int):
table = instrument_tables.get_flexible_metadata_table(instrument, obs_type)
result = dict()
stmt = sqlalchemy.select(table.c["key", "value"]).where(table.c.obs_id == obs_id)
if request.args:
if request.args and "k" in request_args:
cols = request.args["k"]
stmt = stmt.where(table.c.key in cols)
logger.debug(str(stmt))
Expand Down Expand Up @@ -219,7 +219,16 @@ def insert_flexible_metadata(instrument: str, obs_type: str, obs_id: int):
elif dtype == "float" and str(float(value)) != value:
raise BadValueException("float value", value, [])

stmt = sqlalchemy.insert(table).values(obs_id=obs_id, key=key, value=value)
if request.args and request.args.get("u") == "1":
stmt = (
sqlalchemy.dialects.postgresql.insert(table)
.values(obs_id=obs_id, key=key, value=value)
.on_conflict_do_update(
index_dlements=["obs_id", "key"], set_={"value": value}
)
)
else:
stmt = sqlalchemy.insert(table).values(obs_id=obs_id, key=key, value=value)
logger.debug(str(stmt))
with engine.connect() as conn:
_ = conn.execute(stmt)
Expand All @@ -228,7 +237,7 @@ def insert_flexible_metadata(instrument: str, obs_type: str, obs_id: int):


@app.post("/consdb/insert/<instrument>")
def insert(instrument: str, upsert: bool = False):
def insert(instrument: str):
logger.info(request)
if instrument not in instrument_tables.schemas:
raise BadValueException(
Expand All @@ -238,27 +247,16 @@ def insert(instrument: str, upsert: bool = False):
table_name = f"cdb_{instrument}." + info["table"]
table = instrument_tables.schemas[instrument].tables[table_name]
valdict = info["values"]
valdict["obs_id"] = info["obs_id"]

stmt = sqlalchemy.insert(table).values(valdict)
logger.debug(str(stmt))
with engine.connect() as conn:
_ = conn.execute(stmt)
conn.commit()
return ("OK", 200)


@app.post("/consdb/upsert/<instrument>")
def upsert(instrument: str):
logger.info(request)
if instrument not in instrument_tables.schemas:
raise BadValueException(
"instrument", instrument, list(instrument_tables.schemas.keys())
if request.args and request.args.get("u") == "1":
stmt = (
sqlalchemy.dialects.postgresql.insert(table)
.values(valdict)
.on_conflict_do_update(index_elements=["obs_id"], set_=valdict)
)
info = request.json
table = instrument + "." + info["table"]
valdict = info["values"]

stmt = sqlalchemy.update(table).values(valdict)
else:
stmt = sqlalchemy.insert(table).values(valdict)
logger.debug(str(stmt))
with engine.connect() as conn:
_ = conn.execute(stmt)
Expand Down

0 comments on commit 344c01f

Please sign in to comment.