Update an object and its updated_timestamp in the database considering :param conn: a postgres connection :param update_dict: update dict :param table_name: the table name :param equal_filters: the equal filters :return: None
(
conn,
table_name: str,
update_dict: Dict,
equal_filters: Dict,
)
| 7 | |
| 8 | |
| 9 | async def update_object( |
| 10 | conn, |
| 11 | table_name: str, |
| 12 | update_dict: Dict, |
| 13 | equal_filters: Dict, |
| 14 | ) -> None: |
| 15 | """ |
| 16 | Update an object and its updated_timestamp in the database considering |
| 17 | :param conn: a postgres connection |
| 18 | :param update_dict: update dict |
| 19 | :param table_name: the table name |
| 20 | :param equal_filters: the equal filters |
| 21 | :return: None |
| 22 | """ |
| 23 | |
| 24 | if not update_dict: |
| 25 | raise ValueError("update_dict cannot be empty") |
| 26 | |
| 27 | if not equal_filters: |
| 28 | raise ValueError("equal_filters cannot be empty") |
| 29 | |
| 30 | # 1. build update dict |
| 31 | upsert_dict = prepare_db_dict(update_dict) |
| 32 | |
| 33 | # 2. Prepare the SET clause for the update query |
| 34 | updates = ", ".join(f"{key} = ${idx}" for idx, key in enumerate(upsert_dict.keys(), start=len(equal_filters) + 2)) |
| 35 | updates += ", updated_timestamp = $1" |
| 36 | |
| 37 | # 3. Prepare the WHERE clause for the update query, including check |
| 38 | conditions = " AND ".join(f"{key} = ${idx}" for idx, key in enumerate(equal_filters.keys(), start=2)) |
| 39 | |
| 40 | # 4. Prepare the final query |
| 41 | query = f"UPDATE {table_name} SET {updates} WHERE {conditions}" |
| 42 | |
| 43 | # 5. Prepare the arguments for the query |
| 44 | args = [current_timestamp_int_milliseconds(), *equal_filters.values(), *upsert_dict.values()] |
| 45 | |
| 46 | # 6. Execute the query |
| 47 | await conn.execute(query, *args) |
| 48 | |
| 49 | logger.debug(f"update_object: query={query}, args={args}") |
nothing calls this directly
no test coverage detected