Skip to content

Conversation

RohitR311
Copy link
Collaborator

@RohitR311 RohitR311 commented Oct 2, 2025

What this PR does?

  1. Automates the process of db migrations on docker image pull.
  2. Adds migration for the retryCount run table field

Summary by CodeRabbit

  • New Features
    • Added a retry count attribute for runs to enable tracking of execution retries.
  • Bug Fixes
    • Database migrations now safely check for existing columns and indexes before adding or removing them, reducing deployment and rollback errors.
  • Chores
    • Improved startup process: runs migrations via the standard script, provides clearer success/failure messages, and proceeds to start the server for greater reliability.

Copy link

coderabbitai bot commented Oct 2, 2025

Walkthrough

Updates the Docker entrypoint to run migrations via npm and continue starting the server regardless of migration outcome. Converts migrate.js to CommonJS. Adds a new retryCount migration and makes Run.retryCount optional. Hardens existing migrations to check for column/index existence before adding/removing.

Changes

Cohort / File(s) Summary
Migration invocation & control flow
server/docker-entrypoint.sh, server/src/db/migrate.js
Entrypoint now runs npm run migrate, logs pre/post messages, and proceeds even on migration failure. migrate.js converted to CommonJS, retains execSync-based CLI call with try/catch and boolean return.
Database migrations (new/guarded)
server/src/db/migrations/20250202000000-add-retry-count.js, server/src/db/migrations/20250327111003-add-airtable-columns.js, server/src/db/migrations/20250527105655-add-webhooks.js
New migration adds retryCount to run table with existence guard and reversible down. Existing Airtable and webhooks migrations now conditionally add/remove columns and indexes based on schema inspection.
Model alignment
server/src/models/Run.ts
Makes retryCount optional in the Run model (number → number?).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant U as Container/Orchestrator
  participant E as docker-entrypoint.sh
  participant N as npm (migrate script)
  participant M as migrate.js (CommonJS)
  participant S as Sequelize CLI
  participant DB as Database
  participant SRV as App Server

  U->>E: Start container with CMD
  E->>E: Echo "Running database migrations..."
  E->>N: npm run migrate
  N->>M: invoke runMigrations()
  M->>S: execSync sequelize db:migrate
  S->>DB: Apply migrations
  DB-->>S: Result (success/failure)
  S-->>M: exit code
  alt success
    M-->>N: true
    E->>E: Echo "✅ Migrations completed successfully!"
  else failure
    M-->>N: false
    E->>E: Echo "⚠️  Migration failed, but continuing..."
  end
  E->>SRV: exec "$@" (start server)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

Type: Infra, Scope: Setup | Installation, Scope: Infra

Suggested reviewers

  • amhsirak

Poem

I hopped through scripts at break of day,
npm drums the migrate way.
If it trips—no fear, I dash—
server starts in bunny flash.
Columns checked, indexes neat,
retry counts beneath my feet.
Thump-thump: deployments complete! 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly conveys the primary change—adding automated database migrations—without unnecessary detail, and it aligns with the PR’s objectives of running migrations on container startup. It uses a clear “feat:” prefix and avoids vague language or noise. The phrasing “automatic db migrations” directly reflects the implementation in the docker-entrypoint and migration scripts. Therefore it provides sufficient context for reviewers and project history without overspecifying or omitting key information.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9a2d60e and 0ffd66f.

📒 Files selected for processing (6)
  • server/docker-entrypoint.sh (1 hunks)
  • server/src/db/migrate.js (1 hunks)
  • server/src/db/migrations/20250202000000-add-retry-count.js (1 hunks)
  • server/src/db/migrations/20250327111003-add-airtable-columns.js (1 hunks)
  • server/src/db/migrations/20250527105655-add-webhooks.js (1 hunks)
  • server/src/models/Run.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
server/src/db/migrations/20250527105655-add-webhooks.js (1)
server/src/db/migrations/20250202000000-add-retry-count.js (1)
  • tableInfo (5-5)
server/src/db/migrations/20250202000000-add-retry-count.js (1)
server/src/db/migrations/20250527105655-add-webhooks.js (2)
  • tableInfo (5-5)
  • tableInfo (40-40)
server/src/db/migrate.js (1)
server/src/db/models/index.js (1)
  • __dirname (10-10)
server/src/db/migrations/20250327111003-add-airtable-columns.js (2)
server/src/db/migrations/20250202000000-add-retry-count.js (1)
  • tableInfo (5-5)
server/src/db/migrations/20250527105655-add-webhooks.js (2)
  • tableInfo (5-5)
  • tableInfo (40-40)
🔇 Additional comments (7)
server/src/models/Run.ts (1)

50-50: LGTM! Consistent with schema and migration.

The optional retryCount property aligns with the nullable column definition (lines 125–129) and the migration in server/src/db/migrations/20250202000000-add-retry-count.js, which adds a nullable integer column with a default of 0.

server/src/db/migrations/20250327111003-add-airtable-columns.js (1)

70-90: LGTM! Idempotent down migration.

The down migration now mirrors the up migration's idempotency by checking column existence before removal. This prevents errors if the migration is rolled back multiple times or if the columns were never added.

server/src/db/migrations/20250527105655-add-webhooks.js (2)

5-27: LGTM! Idempotent up migration with proper index guard.

The up migration correctly uses describeTable to check column existence and showIndex to verify the index before adding it. This prevents duplicate columns or indexes and ensures idempotency.


30-43: LGTM! Idempotent down migration.

The down migration mirrors the up migration's safety checks by verifying index and column existence before removal, preventing errors on repeated rollbacks.

server/src/db/migrate.js (2)

3-4: LGTM! Correct CommonJS conversion.

The module correctly uses require for child_process and path, aligning with the CommonJS context required by the npm script invocation in server/docker-entrypoint.sh.


9-12: cwd resolution and configuration path are correct
The cwd resolves to the repo root, where .sequelizerc and server/src/db/config/database.js exist, so sequelize-cli will locate the config without issues.

server/src/db/migrations/20250202000000-add-retry-count.js (1)

4-14: LGTM! Idempotent up migration.

The up migration correctly checks for the existence of the retryCount column before adding it, ensuring idempotency and preventing duplicate column errors.

Comment on lines +29 to +37
# Run database migrations using npm script
echo "Running database migrations..."
npm run migrate

# Run the server normally
if [ $? -eq 0 ]; then
echo "✅ Migrations completed successfully!"
else
echo "⚠️ Migration failed, but continuing to start server..."
fi
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Consider failing fast on migration errors.

The script continues to start the server even if migrations fail (line 36). This could lead to runtime errors if the schema is out of sync with the application code (e.g., missing retryCount column).

Consider changing the behavior to exit on migration failure to prevent cascading errors:

 if [ $? -eq 0 ]; then
   echo "✅ Migrations completed successfully!"
 else
-  echo "⚠️  Migration failed, but continuing to start server..."
+  echo "❌ Migration failed. Exiting to prevent runtime errors."
+  exit 1
 fi

Alternatively, if continuing on failure is intentional (e.g., for local development), document the rationale in a comment.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Run database migrations using npm script
echo "Running database migrations..."
npm run migrate
# Run the server normally
if [ $? -eq 0 ]; then
echo "✅ Migrations completed successfully!"
else
echo "⚠️ Migration failed, but continuing to start server..."
fi
# Run database migrations using npm script
echo "Running database migrations..."
npm run migrate
if [ $? -eq 0 ]; then
echo "✅ Migrations completed successfully!"
else
echo "❌ Migration failed. Exiting to prevent runtime errors."
exit 1
fi
🤖 Prompt for AI Agents
In server/docker-entrypoint.sh around lines 29 to 37, the script currently
continues to start the server when migrations fail which can cause runtime
errors; update the else branch to fail fast by exiting with a non-zero code
(e.g., exit 1) so the container stops on migration failure, or if the intent is
to allow startup despite migration errors (local/dev), add a clear comment
explaining that choice and why it's safe and desired.

Comment on lines +17 to +19
down: async (queryInterface, Sequelize) => {
await queryInterface.removeColumn('run', 'retryCount');
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Make the down migration idempotent.

The down migration unconditionally removes the retryCount column without checking if it exists. This is inconsistent with the guarded patterns in other migrations (e.g., 20250327111003-add-airtable-columns.js and 20250527105655-add-webhooks.js) and will fail if the column was never added or was already removed.

Apply this diff to make the down migration idempotent:

 down: async (queryInterface, Sequelize) => {
-  await queryInterface.removeColumn('run', 'retryCount');
+  const tableInfo = await queryInterface.describeTable('run');
+  if (tableInfo.retryCount) {
+    await queryInterface.removeColumn('run', 'retryCount');
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
down: async (queryInterface, Sequelize) => {
await queryInterface.removeColumn('run', 'retryCount');
}
down: async (queryInterface, Sequelize) => {
const tableInfo = await queryInterface.describeTable('run');
if (tableInfo.retryCount) {
await queryInterface.removeColumn('run', 'retryCount');
}
}
🤖 Prompt for AI Agents
In server/src/db/migrations/20250202000000-add-retry-count.js around lines 17 to
19, the down migration unconditionally calls queryInterface.removeColumn('run',
'retryCount') which can fail if the column does not exist; update the down
migration to first inspect the table (e.g., using
queryInterface.describeTable('run') or a similar metadata call) and only call
removeColumn if the returned description shows retryCount exists, so the down
migration becomes idempotent and mirrors the guarded pattern used in other
migrations.

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