Skip to content

feat(migrations): fix automation tables migration for idempotency#1458

Merged
MODSetter merged 1 commit into
mainfrom
docker-hot-patch
Jun 2, 2026
Merged

feat(migrations): fix automation tables migration for idempotency#1458
MODSetter merged 1 commit into
mainfrom
docker-hot-patch

Conversation

@MODSetter
Copy link
Copy Markdown
Owner

@MODSetter MODSetter commented Jun 2, 2026

  • Updated migration script to ensure ENUM types and tables are created only if they do not already exist, preventing errors on re-runs.
  • Added 'IF NOT EXISTS' clauses to table and index creation statements for improved safety during migrations.
  • Ensured that the migration can be safely re-applied without causing conflicts or failures.

Description

Motivation and Context

FIX #

Screenshots

API Changes

  • This PR includes API changes

Change Type

  • Bug fix
  • New feature
  • Performance improvement
  • Refactoring
  • Documentation
  • Dependency/Build system
  • Breaking change
  • Other (specify):

Testing Performed

  • Tested locally
  • Manual/QA verification

Checklist

  • Follows project coding standards and conventions
  • Documentation updated as needed
  • Dependencies updated as needed
  • No lint/build errors or new warnings
  • All relevant tests are passing

High-level PR Summary

This PR makes the automation tables migration script idempotent by adding conditional checks to prevent errors when the migration is re-run. The changes wrap ENUM type creation in DO $$ BEGIN ... END $$ blocks with IF NOT EXISTS checks, and add IF NOT EXISTS clauses to all CREATE TABLE and CREATE INDEX statements. This ensures that if a migration partially completes and fails, it can be safely re-applied without causing conflicts or duplicate object errors in PostgreSQL.

⏱️ Estimated Review Time: 15-30 minutes

💡 Review Order Suggestion
Order File Path
1 surfsense_backend/alembic/versions/144_add_automation_tables.py

Need help? Join our Discord

Summary by CodeRabbit

  • New Features

    • Added database infrastructure for automation functionality with enhanced reliability.
  • Chores

    • Improved migration resilience by implementing conditional logic for safer re-runs after partial failures.

- Updated migration script to ensure ENUM types and tables are created only if they do not already exist, preventing errors on re-runs.
- Added 'IF NOT EXISTS' clauses to table and index creation statements for improved safety during migrations.
- Ensured that the migration can be safely re-applied without causing conflicts or failures.
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
surf-sense-frontend Building Building Preview, Comment Jun 2, 2026 12:40am

Request Review

@MODSetter MODSetter merged commit abea00d into main Jun 2, 2026
5 of 10 checks passed
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 2, 2026

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3365b8f8-50a5-41a9-9204-564087ccec65

📥 Commits

Reviewing files that changed from the base of the PR and between 2881b1b and bb5c8e4.

📒 Files selected for processing (1)
  • surfsense_backend/alembic/versions/144_add_automation_tables.py

📝 Walkthrough

Walkthrough

This Alembic migration adds three automation persistence tables with idempotent creation logic. ENUM types are guarded with DO $$ ... IF NOT EXISTS blocks, and all tables and indexes use IF NOT EXISTS clauses to survive partial failures and re-runs.

Changes

Automation persistence schema

Layer / File(s) Summary
ENUM types and automations table with indexes
surfsense_backend/alembic/versions/144_add_automation_tables.py
Creates automation_status, automation_trigger_type, and automation_run_status ENUM types using guarded DO $$ ... IF NOT EXISTS blocks. Adds automations table with idempotent CREATE TABLE IF NOT EXISTS and indexes on user_id, name, and created_at.
automation_triggers table with indexes
surfsense_backend/alembic/versions/144_add_automation_tables.py
Adds automation_triggers table with idempotent indexes on automation_id, type, enabled, and a partial index on next_fire_at constrained to enabled schedule triggers with non-null next_fire_at.
automation_runs table with indexes
surfsense_backend/alembic/versions/144_add_automation_tables.py
Adds automation_runs table with idempotent indexes on automation_id, trigger_id, status, and created_at.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A migration so guarded, with IF NOT EXISTS care,
Three tables of automation dancing in the air,
ENUM types wrapped safely in logic's embrace,
Idempotent and humble, they won't fail or race!

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch docker-hot-patch

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.

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.

1 participant