Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cosmotech/coal/postgresql/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@ def send_runner_metadata_to_postgresql(
CREATE TABLE IF NOT EXISTS {schema_table} (
id varchar(32) PRIMARY KEY,
name varchar(256),
last_csm_run_id varchar(32),
last_csm_run_id varchar(32) UNIQUE,
run_template_id varchar(32)
);
"""
LOGGER.info(T("coal.services.postgresql.creating_table").format(schema_table=schema_table))
curs.execute(sql_create_table)
conn.commit()

runner_id = runner.get("id")
sql_delete_from_metatable = f"""
DELETE FROM {schema_table}
WHERE id= $1;
"""
curs.execute(sql_delete_from_metatable, (runner_id,))
conn.commit()

sql_upsert = f"""
INSERT INTO {schema_table} (id, name, last_csm_run_id, run_template_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (id)
DO
UPDATE SET name = EXCLUDED.name, last_csm_run_id = EXCLUDED.last_csm_run_id;
VALUES ($1, $2, $3, $4)
"""
LOGGER.debug(runner)
curs.execute(
Expand Down
2 changes: 1 addition & 1 deletion cosmotech/coal/postgresql/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def dump_store_to_postgresql_from_conf(
)
if fk_id and _psql.is_metadata_exists():
metadata_table = f"{_psql.metadata_table_name}"
_psql.add_fk_constraint(table_name, "csm_run_id", metadata_table, "last_csm_run_id")
_psql.add_fk_constraint(target_table_name, "csm_run_id", metadata_table, "last_csm_run_id")

total_rows += rows
_up_time = perf_counter()
Expand Down
18 changes: 15 additions & 3 deletions cosmotech/coal/postgresql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,24 @@ def add_fk_constraint(
to_table: str,
to_col: str,
) -> None:
# Connect to PostgreSQL and remove runner metadata row
# Connect to PostgreSQL and add a foreign key constraint
with dbapi.connect(self.full_uri, autocommit=True) as conn:
with conn.cursor() as curs:
sql_add_fk = f"""
ALTER TABLE {self.db_schema}.{from_table}
CONSTRAINT metadata FOREIGN KEY ({from_col}) REFERENCES {to_table}({to_col})
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'metadata'
AND conrelid = '{self.db_schema}.{from_table}'::regclass
) THEN
ALTER TABLE {self.db_schema}.{from_table}
ADD CONSTRAINT metadata FOREIGN KEY ({from_col})
REFERENCES {self.db_schema}.{to_table}({to_col})
ON DELETE CASCADE;
END IF;
END $$;
"""
curs.execute(sql_add_fk)
conn.commit()
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/coal/test_postgresql/test_postgresql_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ def test_send_runner_metadata_to_postgresql(self, mock_connect, mock_postgres_ut
mock_connect.assert_called_once_with("postgresql://user:password@localhost:5432/testdb", autocommit=True)

# Check that SQL statements were executed
assert mock_cursor.execute.call_count == 2
assert mock_cursor.execute.call_count == 3

# Verify the SQL statements (partially, since the exact SQL is complex)
create_table_call = mock_cursor.execute.call_args_list[0]
assert "CREATE TABLE IF NOT EXISTS" in create_table_call[0][0]
assert "public.test_runnermetadata" in create_table_call[0][0]

upsert_call = mock_cursor.execute.call_args_list[1]
delete_call = mock_cursor.execute.call_args_list[1]
assert "DELETE FROM" in delete_call[0][0]
assert "public.test_runnermetadata" in delete_call[0][0]
assert delete_call[0][1] == ("test-runner-id",)

upsert_call = mock_cursor.execute.call_args_list[2]
assert "INSERT INTO" in upsert_call[0][0]
assert "public.test_runnermetadata" in upsert_call[0][0]
assert upsert_call[0][1] == (
Expand All @@ -90,7 +95,7 @@ def test_send_runner_metadata_to_postgresql(self, mock_connect, mock_postgres_ut
)

# Check that commits were called
assert mock_conn.commit.call_count == 2
assert mock_conn.commit.call_count == 3

# Verify the function returns the lastRunId
assert result == "test-run-id"
Expand Down
Loading