Skip to content

fix: auto-create notification channel tables on startup#82

Merged
bd73-com merged 4 commits intomainfrom
claude/fix-webhook-feature-doWe8
Mar 6, 2026
Merged

fix: auto-create notification channel tables on startup#82
bd73-com merged 4 commits intomainfrom
claude/fix-webhook-feature-doWe8

Conversation

@bd73-com
Copy link
Owner

@bd73-com bd73-com commented Mar 6, 2026

Summary

Notification channel routes (/api/monitors/:id/channels) return 503 "not available yet" when the notification_channels, delivery_log, and slack_connections tables don't exist — which happens on fresh deployments where schema:push hasn't been run after these tables were added to the Drizzle schema. This PR adds CREATE TABLE IF NOT EXISTS migrations at server startup (same pattern used for error_logs and api_keys), so the tables are always available.

Changes

Server startup migration (server/routes.ts)

  • Auto-create notification_channels table with monitor FK, JSONB config column, and indexes (monitor lookup + unique monitor+channel)
  • Auto-create delivery_log table with monitor and change FKs, status tracking, and composite index
  • Auto-create slack_connections table with unique user FK, team metadata, and a CHECK constraint on bot_token enforcing the AES-256-GCM encrypted ciphertext format (base64:base64:base64) to prevent accidental plaintext storage
  • Added SQL comment on config JSONB column noting it may contain webhook secrets
  • Errors are caught and logged without blocking route registration

Tests (server/routes.migration.test.ts)

  • Updated existing DDL count assertion (5 → 11) to account for 6 new statements
  • Added assertions verifying notification_channels, delivery_log, slack_connections DDL is issued
  • Added 5 new tests:
    • DDL structure verification for each of the 3 tables (columns, indexes, constraints, CHECK)
    • Error logging when channel table creation fails
    • Channel routes still register when table creation fails

How to test

  1. Run npm run test — all 1025 tests should pass
  2. Start the server against a fresh database (no prior schema:push) and verify /api/monitors/:id/channels returns 200 (not 503)
  3. Verify slack_connections rejects plaintext bot_token values by attempting a direct INSERT — should fail the CHECK constraint

https://claude.ai/code/session_011SJrk4Nqa62f3QE4WTqr9p

Summary by CodeRabbit

  • New Features

    • Notification-related database tables and indexes are now initialized automatically at startup.
  • Bug Fixes

    • Improved error handling so notification routes remain available if schema setup fails; non-fatal errors are logged and service continues.
  • Tests

    • Added tests covering schema creation, generated SQL inspection, error handling, and route fallback behavior.

claude added 3 commits March 6, 2026 11:57
The webhook feature returned "Notification channels are not available yet"
because the notification_channels, delivery_log, and slack_connections tables
did not exist in the database. This adds CREATE TABLE IF NOT EXISTS statements
during app startup, following the same pattern used for the api_keys table.

https://claude.ai/code/session_011SJrk4Nqa62f3QE4WTqr9p
Tests verify DDL structure for notification_channels, delivery_log, and
slack_connections tables, error logging on creation failure, and that
channel routes are still registered when table creation fails.

https://claude.ai/code/session_011SJrk4Nqa62f3QE4WTqr9p
Adds a PostgreSQL CHECK constraint ensuring bot_token matches the
AES-256-GCM ciphertext format (base64:base64:base64), preventing
accidental plaintext storage. Also adds a SQL comment on the config
JSONB column noting it may contain webhook secrets.

https://claude.ai/code/session_011SJrk4Nqa62f3QE4WTqr9p
@github-actions github-actions bot added the fix label Mar 6, 2026
@coderabbitai
Copy link

coderabbitai bot commented Mar 6, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: abfbbbec-73ac-45a5-b331-d9b4285c1224

📥 Commits

Reviewing files that changed from the base of the PR and between 707bf77 and daff84b.

📒 Files selected for processing (2)
  • server/routes.migration.test.ts
  • server/routes.ts

📝 Walkthrough

Walkthrough

Adds startup initialization in server/routes.ts to run idempotent DDL for notification_channels, delivery_log, and slack_connections inside a try/catch, and expands server/routes.migration.test.ts to assert generated SQL, db.execute call counts, and error-handling/route-registration behavior.

Changes

Cohort / File(s) Summary
Startup DDL Initialization
server/routes.ts
Adds a try/catch-wrapped startup block that executes CREATE TABLE/INDEX/CONSTRAINT DDL for notification_channels, delivery_log, and slack_connections. Logs non-fatal errors and proceeds to register routes regardless of DDL outcome.
Migration Test Coverage
server/routes.migration.test.ts
Expands tests to expect increased db.execute calls (from 5 → 11), inspects generated SQL for the three new tables and related constraints/indexes, and adds tests for failure of notification table creation to ensure errors are logged and routes still register.

Sequence Diagram(s)

sequenceDiagram
  participant Startup as Server Startup
  participant DB as Database
  participant Logger as Logger
  participant Router as Route Registrar

  Startup->>DB: Execute DDL (notification_channels, delivery_log, slack_connections)
  DB-->>Startup: Success / Error
  alt Success
    Startup->>Logger: Log DDL success (info)
  else Error
    Startup->>Logger: Log DDL failure (non-fatal)
  end
  Startup->>Router: Register routes (notification routes guarded at runtime)
  Router-->>Startup: Routes registered
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: auto-create notification channel tables on startup' directly and clearly summarizes the main change—automatically creating notification channel-related tables at server startup to prevent 503 errors on fresh deployments.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch claude/fix-webhook-feature-doWe8

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@server/routes.migration.test.ts`:
- Around line 342-344: The test currently only checks that the CREATE string
contains "CHECK" and "scope"; update the assertion that inspects scCreate to
validate the full CHECK constraint regex for encrypted ciphertext (e.g., the
base64:base64:base64 pattern) by asserting scCreate contains the expected
pattern string or a regexp such as
"CHECK.*'\\^[A-Za-z0-9+/=]+:[A-Za-z0-9+/=]+:[A-Za-z0-9+/=]+\\$'"; locate the
scCreate variable in the test and replace or add an expectation that matches
this exact constraint string/regexp to ensure the encryption format constraint
is enforced.

In `@server/routes.ts`:
- Line 135: The bot_token CHECK constraint currently allows very short base64
parts (e.g., "a:b:c"); update the CHECK regex on the bot_token column to enforce
minimum base64 lengths for each colon-separated part (e.g., IV >=16 chars,
ciphertext >=20 chars, auth tag >=22 chars) so the pattern requires at least
those character counts while still matching base64 characters and colons; modify
the CHECK expression that references bot_token to use the stricter regex so
stored values must meet the new minimum-lengths for IV, ciphertext, and auth
tag.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4c5283ea-e218-4eb1-a16f-0d4017682649

📥 Commits

Reviewing files that changed from the base of the PR and between 1c16f2b and 707bf77.

📒 Files selected for processing (2)
  • server/routes.migration.test.ts
  • server/routes.ts

… lengths

The CHECK regex previously accepted trivially short values like "a:b:c".
Now enforces minimum base64 lengths matching AES-256-GCM output sizes
(IV >=16, ciphertext >=20, auth tag >=22 chars). Also strengthens the
test assertion to verify the actual regex pattern, not just the CHECK keyword.

https://claude.ai/code/session_011SJrk4Nqa62f3QE4WTqr9p
@bd73-com bd73-com merged commit be07ed0 into main Mar 6, 2026
1 of 2 checks passed
@bd73-com bd73-com deleted the claude/fix-webhook-feature-doWe8 branch March 6, 2026 12:34
bd73-com pushed a commit that referenced this pull request Mar 6, 2026
Instead of showing a scary "Slack integration is not available" error
to end users (who cannot fix server configuration), hide the entire
Slack section when the backend reports available=false. This stops the
recurring cycle of "improving" the error message (#82, #83, #85).

https://claude.ai/code/session_017WdTqmNUwoVYvsgEDoLrHK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants