Skip to content

Conversation

@pyramation
Copy link
Contributor

@pyramation pyramation commented Jan 2, 2026

feat(pgsql-test): add getErrorCode helper and fix pgpm_migrate exception context

Summary

This PR makes two related changes:

1. Add getErrorCode() helper to pgsql-test

Since pgsql-test 2.21.0, PgTestClient appends additional debug context (Where, Query, Values) to error messages by default. This breaks tests that use exact string matching like expect(err.message).toEqual('NONEXISTENT_TYPE').

The new helper extracts just the first line (the actual error code):

// Enhanced error: "NONEXISTENT_TYPE\nWhere: PL/pgSQL function...\nQuery: INSERT INTO..."
getErrorCode(err.message) // => "NONEXISTENT_TYPE"

2. Fix pgpm_migrate exception handling to preserve SQL context

The pgpm_migrate.deploy and pgpm_migrate.revert procedures were using RAISE EXCEPTION USING to re-raise errors, which loses the PG_EXCEPTION_CONTEXT (including the actual SQL statement that failed).

Changed to use simple RAISE; which preserves the full original exception context, keeping valuable debugging info like:

"where": "SQL statement \"INSERT INTO test_snapshot_products...\" PL/pgSQL function pgpm_migrate.deploy(...) line 46 at EXECUTE"

Updates since last revision

  • Fixed root cause in pgpm_migrate - Changed RAISE EXCEPTION USING to RAISE; in both deploy and revert procedures to preserve original exception context
  • Updated error message snapshots - The pgpm-migration-errors test snapshots now include the enhanced error context (Detail, Where, Internal Query, etc.) that pgsql-test appends to err.message. This is correct behavior - these tests verify enhanced error messages work in the migration flow.

Review & Testing Checklist for Human

  • Run pgsql-test suite (pnpm test in postgres/pgsql-test/) and verify all pgpm-migration-errors snapshots pass - the snapshots were updated based on CI output, not local testing
  • Verify SQL context is preserved - Deploy a migration that fails and confirm the error includes the actual SQL statement in the where field (should show "line 46 at EXECUTE", not "line 73 at RAISE")
  • Test getErrorCode helper - Verify it correctly extracts the first line from enhanced error messages
  • Check for regressions - Ensure normal successful migrations still work after the RAISE; change

Recommended test plan:

  1. Run pnpm test in postgres/pgsql-test/
  2. Verify the pgpm-migration-errors tests pass with SQL statement context in the where field
  3. Manually test a failing migration to confirm debugging info is preserved

Notes

This is part of fixing the test failures in constructive-db PR #190, where the pgsql-test upgrade caused tests to fail due to enhanced error message formatting.

Link to Devin run: https://app.devin.ai/sessions/6b89db7666a04391bbc93c493698f0df
Requested by: Dan Lynch (@pyramation)

Add a utility function to extract the error code from enhanced PostgreSQL
error messages. Enhanced errors from PgTestClient include additional context
(Where, Query, Values) on subsequent lines. This helper returns only the
first line containing the actual error code.

This is useful for tests that need to assert specific error codes while
still benefiting from the enhanced debugging context.
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Update snapshot expectations to match the new error format from pgpm_migrate:
- hint field now returns '' instead of undefined
- where field now shows 'line 73 at RAISE' instead of 'line 46 at EXECUTE'
- Some fields changed from undefined to '' (empty string)
- Error messages now include enhanced context (Detail, Where lines)
…dures

Use simple RAISE instead of RAISE EXCEPTION USING to preserve the full
PostgreSQL exception context including SQL statements in the 'where' field.

The previous approach captured diagnostics but lost the PG_EXCEPTION_CONTEXT
when re-raising, which removed valuable debugging information like the actual
SQL statement that failed.
@devin-ai-integration devin-ai-integration bot changed the title feat(pgsql-test): add getErrorCode helper for enhanced error messages feat(pgsql-test): add getErrorCode helper and fix pgpm_migrate exception context Jan 2, 2026
…ontext

The error message snapshots now include the enhanced debugging context
(Where, Detail, Internal Query, etc.) that pgsql-test appends to error
messages. This is the correct behavior - these tests verify that enhanced
error messages work correctly in the pgpm migration flow.

The error fields snapshots continue to show the raw PostgreSQL error
properties including the SQL statement context in the 'where' field.
@pyramation pyramation merged commit d47aae6 into main Jan 2, 2026
36 checks passed
@pyramation pyramation deleted the devin/1767343254-add-geterrorcode-helper branch January 2, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants