Skip to content

categories.js runs ad-hoc CREATE TABLE/ALTER TABLE on every module load outside the versioned migrations system, swallowing all errors #1122

Description

@zeekman

Project

FarmersMarketplace — Backend (Categories / Schema Management)

Description

backend/src/routes/categories.js defines and immediately invokes ensureCategorySchema() at module load time:

async function ensureCategorySchema() {
  const createSql = `CREATE TABLE IF NOT EXISTS categories (...)`;
  const alterSql = `ALTER TABLE products ADD COLUMN category_id INTEGER REFERENCES categories(id)`;
  try {
    if (typeof db.exec === 'function') { await db.exec(createSql); await db.exec(alterSql); }
    else if (typeof db.query === 'function') { await db.query(createSql); await db.query(alterSql); }
  } catch {
    // Ignore migration failures during test/bootstrap; route handlers will still work.
  }
}
void ensureCategorySchema();

This entirely bypasses the versioned migration system documented in the README (backend/migrations/NNN_description.sql, tracked in a migrations table, applied via npm run migrate). Running schema DDL as a side effect of require()-ing a route file means: (1) the categories table's existence depends on whether this specific route module has been loaded at least once, not on a reproducible migration history; (2) the bare catch {} swallows every error, not just "already exists" — a genuine permissions error, a locked table, or a real syntax error on some future Postgres version would fail completely silently and the app would continue running with categories/category_id missing, later surfacing as confusing 500s deep inside route handlers instead of a clear startup failure.

Acceptance Criteria

  • The categories table and products.category_id column are created via a proper file in backend/migrations/, tracked like every other schema change.
  • ensureCategorySchema()/void ensureCategorySchema() is removed from categories.js.
  • The blanket catch {} pattern is not reintroduced — any migration failure surfaces at startup, consistent with how runMigrations() already process.exit(1)s on failure for the Postgres path in db/schema.js.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendBackend / API / database issues

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions