Skip to content

Commit 6810d23

Browse files
committed
fix: auto add error message column
1 parent 330e69a commit 6810d23

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file.
77
### Added
88
- Optional flags to continue deploying remaining scripts after an error, recording full error messages in the change history table and listing failed scripts at completion.
99
### Fixed
10-
- Preserve original script errors when the change history table lacks the `ERROR_MESSAGE` column
10+
- Automatically add missing `ERROR_MESSAGE` column to the change history table to capture full script errors
1111

1212
## [4.0.1] - 2025-02-17
1313
### Changed

schemachange/session/SnowflakeSession.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -362,38 +362,21 @@ def apply_change_script(
362362
CURRENT_TIMESTAMP
363363
);
364364
"""
365+
dedent_query = dedent(query)
365366
try:
366-
self.execute_snowflake_query(dedent(query), logger=logger)
367+
self.execute_snowflake_query(dedent_query, logger=logger)
367368
except snowflake.connector.errors.ProgrammingError as e:
368369
if "ERROR_MESSAGE" in str(e):
369370
logger.warning(
370-
"Change history table missing ERROR_MESSAGE column, inserting without",
371+
"Change history table missing ERROR_MESSAGE column, adding column",
371372
error=str(e),
372373
)
373-
fallback = f"""\
374-
INSERT INTO {self.change_history_table.fully_qualified} (
375-
VERSION,
376-
DESCRIPTION,
377-
SCRIPT,
378-
SCRIPT_TYPE,
379-
CHECKSUM,
380-
EXECUTION_TIME,
381-
STATUS,
382-
INSTALLED_BY,
383-
INSTALLED_ON
384-
) VALUES (
385-
'{script_version}',
386-
'{script.description}',
387-
'{script.name}',
388-
'{script.type}',
389-
'{checksum}',
390-
{execution_time},
391-
'{status}',
392-
'{self.user}',
393-
CURRENT_TIMESTAMP
394-
);
395-
"""
396-
self.execute_snowflake_query(dedent(fallback), logger=logger)
374+
alter_query = (
375+
f"ALTER TABLE {self.change_history_table.fully_qualified} "
376+
"ADD COLUMN IF NOT EXISTS ERROR_MESSAGE VARCHAR"
377+
)
378+
self.execute_snowflake_query(alter_query, logger=logger)
379+
self.execute_snowflake_query(dedent_query, logger=logger)
397380
else:
398381
raise
399382
if status != "Success":

tests/session/test_SnowflakeSession.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def test_apply_change_script_missing_error_message_column(
7979
Exception("boom"),
8080
ProgrammingError("invalid identifier 'ERROR_MESSAGE'", 0, 0),
8181
None,
82+
None,
8283
]
8384
)
8485
with (
@@ -87,8 +88,12 @@ def test_apply_change_script_missing_error_message_column(
8788
pytest.raises(Exception),
8889
):
8990
session.apply_change_script(script, "select 1", False, session.logger)
90-
assert session.execute_snowflake_query.call_count == 3
91+
assert session.execute_snowflake_query.call_count == 4
9192
first_insert = session.execute_snowflake_query.call_args_list[1].args[0]
92-
fallback_insert = session.execute_snowflake_query.call_args_list[2].args[0]
93+
alter_stmt = session.execute_snowflake_query.call_args_list[2].args[0]
94+
retry_insert = session.execute_snowflake_query.call_args_list[3].args[0]
9395
assert "ERROR_MESSAGE" in first_insert
94-
assert "ERROR_MESSAGE" not in fallback_insert
96+
assert "ALTER TABLE" in alter_stmt
97+
assert "ADD COLUMN" in alter_stmt
98+
assert "ERROR_MESSAGE" in alter_stmt
99+
assert "ERROR_MESSAGE" in retry_insert

0 commit comments

Comments
 (0)