Skip to content

Stop Sequelize schema-altering sync during app boot; require explicit setup sync - #217

Merged
jiayangc1 merged 1 commit into
mainfrom
copilot/fix-postgresql-startup-crash
Jul 9, 2026
Merged

Stop Sequelize schema-altering sync during app boot; require explicit setup sync#217
jiayangc1 merged 1 commit into
mainfrom
copilot/fix-postgresql-startup-crash

Conversation

Copilot AI commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Under clustered PM2 startup, each worker was executing sequelize.sync({ alter: true }), triggering concurrent ALTER TABLE DDL and exhausting PostgreSQL locks (53200: out of shared memory). This change makes runtime boot authenticate-only and moves schema alteration to explicit setup flow.

  • Database init contract (models/index.js)

    • initDatabase now accepts options: initDatabase({ sync = false, alter = false } = {}).
    • Default behavior is connection/authentication only.
    • Model sync executes only when sync is explicitly enabled.
  • Explicit schema setup path (setup-db.js)

    • Setup script now opts into schema changes with:
      • initDatabase({ sync: true, alter: true }).
  • Runtime impact

    • node . no longer performs schema-altering DDL during startup.
    • node setup-db.js remains the explicit entrypoint for schema sync/alter.
// models/index.js
const initDatabase = async ({ sync = false, alter = false } = {}) => {
  await sequelize.authenticate();
  if (sync) await sequelize.sync({ alter });
};

// setup-db.js
await initDatabase({ sync: true, alter: true });

@jiayangc1
jiayangc1 marked this pull request as ready for review July 9, 2026 05:29
Copilot AI review requested due to automatic review settings July 9, 2026 05:29
@jiayangc1
jiayangc1 merged commit 60c1493 into main Jul 9, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR prevents runtime application startup from performing Sequelize schema-altering DDL by default, moving sequelize.sync({ alter: true }) behind an explicit, opt-in setup flow to avoid lock exhaustion under clustered/parallel boots.

Changes:

  • Updated initDatabase to accept options ({ sync, alter }) and default to authenticate-only behavior.
  • Gated model syncing behind an explicit sync: true option.
  • Updated setup-db.js to explicitly request schema sync + alter during the setup script.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
models/index.js Makes DB initialization authenticate-only by default; sync/alter now opt-in via options.
setup-db.js Explicitly opts into schema sync/alter for the setup script entrypoint.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread models/index.js
console.log("✅ Database models synchronized successfully.");
}
} catch (error) {
console.error("❌ Unable to connect to the database:", error);
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.

3 participants