Skip to content

Commit 60e7dee

Browse files
committed
fix metadata table name to lower case
This fixes the case where Psql doesn't found the table which to exist
1 parent d852502 commit 60e7dee

5 files changed

Lines changed: 16 additions & 17 deletions

File tree

cosmotech/coal/postgresql/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def send_runner_metadata_to_postgresql(
4444
# Connect to PostgreSQL and update runner metadata
4545
with dbapi.connect(_psql.full_uri, autocommit=True) as conn:
4646
with conn.cursor() as curs:
47-
schema_table = f"{str(_psql.db_schema)}.{str(_psql.table_prefix)}RunnerMetadata"
47+
schema_table = f"{str(_psql.db_schema)}.{str(_psql.metadata_table_name)}"
4848
sql_create_table = f"""
4949
CREATE TABLE IF NOT EXISTS {schema_table} (
5050
id varchar(32) PRIMARY KEY,
@@ -102,7 +102,7 @@ def remove_runner_metadata_from_postgresql(
102102
# Connect to PostgreSQL and remove runner metadata row
103103
with dbapi.connect(_psql.full_uri, autocommit=True) as conn:
104104
with conn.cursor() as curs:
105-
schema_table = f"{_psql.db_schema}.{_psql.table_prefix}RunnerMetadata"
105+
schema_table = f"{_psql.db_schema}.{_psql.metadata_table_name}"
106106
last_run_id = runner.get("lastRunInfo").get("lastRunId")
107107
sql_delete_from_metatable = f"""
108108
DELETE FROM {schema_table}

cosmotech/coal/postgresql/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def dump_store_to_postgresql_from_conf(
114114
replace,
115115
)
116116
if fk_id and _psql.is_metadata_exists():
117-
metadata_table = f"{_psql.table_prefix}RunnerMetadata"
117+
metadata_table = f"{_psql.metadata_table_name}"
118118
_psql.add_fk_constraint(table_name, "csm_run_id", metadata_table, "last_csm_run_id")
119119

120120
total_rows += rows

cosmotech/coal/postgresql/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def __init__(self, configuration: Configuration):
2626
@property
2727
def table_prefix(self):
2828
if "table_prefix" in self._configuration:
29-
return self._configuration.table_prefix
30-
return "Cosmotech_"
29+
return self._configuration.table_prefix.lower()
30+
return "Cosmotech_".lower()
3131

3232
@property
3333
def db_name(self):
@@ -77,7 +77,7 @@ def full_uri(self) -> str:
7777

7878
@property
7979
def metadata_table_name(self) -> str:
80-
return f"{self.table_prefix}RunnerMetadata"
80+
return f"{self.table_prefix}RunnerMetadata".lower()
8181

8282
def get_postgresql_table_schema(self, target_table_name: str) -> Optional[pa.Schema]:
8383
"""

tests/unit/coal/test_postgresql/test_postgresql_runner.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_send_runner_metadata_to_postgresql(self, mock_connect, mock_postgres_ut
4646
mock_postgres_utils_instance = MagicMock()
4747
mock_postgres_utils_instance.full_uri = "postgresql://user:password@localhost:5432/testdb"
4848
mock_postgres_utils_instance.db_schema = "public"
49-
mock_postgres_utils_instance.table_prefix = "Test_"
49+
mock_postgres_utils_instance.metadata_table_name = "test_runnermetadata"
5050
mock_postgres_utils_class.return_value = mock_postgres_utils_instance
5151

5252
# Mock PostgreSQL connection and cursor
@@ -77,11 +77,11 @@ def test_send_runner_metadata_to_postgresql(self, mock_connect, mock_postgres_ut
7777
# Verify the SQL statements (partially, since the exact SQL is complex)
7878
create_table_call = mock_cursor.execute.call_args_list[0]
7979
assert "CREATE TABLE IF NOT EXISTS" in create_table_call[0][0]
80-
assert "public.Test_RunnerMetadata" in create_table_call[0][0]
80+
assert "public.test_runnermetadata" in create_table_call[0][0]
8181

8282
upsert_call = mock_cursor.execute.call_args_list[1]
8383
assert "INSERT INTO" in upsert_call[0][0]
84-
assert "public.Test_RunnerMetadata" in upsert_call[0][0]
84+
assert "public.test_runnermetadata" in upsert_call[0][0]
8585
assert upsert_call[0][1] == (
8686
mock_runner["id"],
8787
mock_runner["name"],
@@ -125,7 +125,7 @@ def test_remove_runner_metadata_to_postgresql(self, mock_connect, mock_postgres_
125125
mock_postgres_utils_instance = MagicMock()
126126
mock_postgres_utils_instance.full_uri = "postgresql://user:password@localhost:5432/testdb"
127127
mock_postgres_utils_instance.db_schema = "public"
128-
mock_postgres_utils_instance.table_prefix = "Test_"
128+
mock_postgres_utils_instance.metadata_table_name = "test_runnermetadata"
129129
mock_postgres_utils_class.return_value = mock_postgres_utils_instance
130130

131131
# Mock PostgreSQL connection and cursor
@@ -156,7 +156,7 @@ def test_remove_runner_metadata_to_postgresql(self, mock_connect, mock_postgres_
156156
# Verify the SQL statements (partially, since the exact SQL is complex)
157157
delete_call = mock_cursor.execute.call_args_list[0]
158158
assert "DELETE FROM" in delete_call[0][0]
159-
assert "public.Test_RunnerMetadata" in delete_call[0][0]
159+
assert "public.test_runnermetadata" in delete_call[0][0]
160160

161161
# Check that commits were called
162162
assert mock_conn.commit.call_count == 1

tests/unit/coal/test_postgresql/test_postgresql_store.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def test_dump_store_to_postgresql_with_tables(self, mock_send_to_postgresql, moc
4242
postgres_schema = "public"
4343
postgres_user = "user"
4444
postgres_password = "password"
45-
table_prefix = "Test_"
4645
replace = True
4746

4847
_config = Configuration(
@@ -58,7 +57,7 @@ def test_dump_store_to_postgresql_with_tables(self, mock_send_to_postgresql, moc
5857
"user_name": postgres_user,
5958
"user_password": postgres_password,
6059
"password_encoding": False,
61-
"table_prefix": table_prefix,
60+
"table_prefix": "Test_",
6261
},
6362
}
6463
)
@@ -72,7 +71,7 @@ def test_dump_store_to_postgresql_with_tables(self, mock_send_to_postgresql, moc
7271
postgres_schema,
7372
postgres_user,
7473
postgres_password,
75-
table_prefix,
74+
"Test_",
7675
replace,
7776
)
7877

@@ -93,12 +92,12 @@ def test_dump_store_to_postgresql_with_tables(self, mock_send_to_postgresql, moc
9392
[
9493
call(
9594
table1_data,
96-
f"{table_prefix}table1",
95+
f"test_table1",
9796
replace,
9897
),
9998
call(
10099
table2_data,
101-
f"{table_prefix}table2",
100+
f"test_table2",
102101
replace,
103102
),
104103
]
@@ -266,6 +265,6 @@ def test_dump_store_to_postgresql_default_parameters(self, mock_send_to_postgres
266265
# Check that send_pyarrow_table_to_postgresql was called with default parameters
267266
mock_send_to_postgresql.assert_called_once_with(
268267
table_data,
269-
"Cosmotech_table1", # Default table_prefix is "Cosmotech_"
268+
"cosmotech_table1", # Default table_prefix is "Cosmotech_" but is sanitized to "cosmotech_" for psql
270269
True, # Default replace is True
271270
)

0 commit comments

Comments
 (0)