Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

5.6 KB
Raw Blame
import * as p from '@clack/prompts'
import type { Command } from 'commander'
import pc from 'picocolors'
import { DATABASE_RESET } from '../backend.js'
import { detectProject, hasMonorepoSpreePath } from '../context.js'
import {
  appServices,
  dockerCompose,
  dockerComposeExec,
  dockerComposeRun,
  dockerStackProblem,
  isServiceRunning,
} from '../docker.js'

export function registerDbCommand(program: Command): void {
  program
    .command('db:reset')
    .description(
      'Drop, create, migrate, and seed the dev database (destructive; self-heals a running stack)',
    )
    .option('--yes', 'skip the confirmation prompt (for CI)')
    .action(async (flags: { yes?: boolean }) => {
      const ctx = detectProject()

      const refuse = (lines: string[]): never => {
        p.cancel(lines.join('\n'))
        process.exit(1)
      }

      // `compose run` materializes the project-local docker-compose.yml, which
      // is not the running edge config — refuse like bundle.ts / upgrade.ts.
      // The edge stack heals its own dev DB via `pnpm server:*`.
      if (hasMonorepoSpreePath(ctx.projectDir)) {
        refuse([
          'This is a monorepo edge project (SPREE_PATH set in .env).',
          `Reset the dev database from the monorepo root with ${pc.bold('pnpm server:setup')}`,
          '(or the relevant `pnpm server:*` script) — the project-local compose is not the',
          'running config here.',
        ])
      }

      if (!flags.yes) {
        const confirmed = await p.confirm({
          message: 'This will drop the spree_development database. Continue?',
          initialValue: false,
        })
        if (p.isCancel(confirmed) || !confirmed) {
          p.cancel('Cancelled.')
          process.exit(0)
        }
      }

      // The long-running web + worker containers hold pooled
      // connections to spree_development. The drop issues a PLAIN
      // `DROP DATABASE` (no WITH FORCE), which PostgreSQL rejects while any other
      // session is connected — so a reset against a running stack deadlocks. We
      // stop both regardless of which is up (the worker alone holds up to
      // RAILS_MAX_THREADS pooled connections), so we only need to know if either
      // is running; probe them concurrently.
      let stackUp: boolean
      let services: string[]
      try {
        services = await appServices(ctx.projectDir)
        const running = await Promise.all(
          services.map((service) => isServiceRunning(service, ctx.projectDir)),
        )
        stackUp = running.some(Boolean)
      } catch (err) {
        // `compose ps` itself failed: daemon down, broken/stale compose, unknown
        // service (mirrors upgrade.ts). `return` is load-bearing: it tells TS
        // `stackUp` is assigned past here.
        return refuse(dockerStackProblem(err, 'spree db:reset'))
      }

      if (stackUp) {
        const s = p.spinner()
        s.start('Stopping the app to release database connections...')
        await dockerCompose(['stop', ...services], ctx.projectDir)
        s.stop('App stopped.')
      }

      try {
        // One-off container: `run`'s depends_on cold-starts postgres (and waits
        // for service_healthy) but never restarts web/worker, so nothing reopens
        // a blocking connection. Works whether the stack was up, down, or partial.
        // captureStderr so the Postgres "being accessed" error below is matchable.
        await dockerComposeRun(DATABASE_RESET, ctx.projectDir, { captureStderr: true })
      } catch (err) {
        const stderr = String((err as { stderr?: string }).stderr ?? (err as Error).message ?? '')
        if (/being accessed by other users|55006/.test(stderr)) {
          // We stopped the app containers, so the remaining connection is a host
          // client we can't see or stop (TablePlus/DataGrip/psql on port 5433).
          // If we stopped the stack to get here, also tell them how to restore it.
          refuse([
            'Could not drop spree_development — another client is still connected.',
            `A database client (TablePlus, DataGrip, psql) may be connected on port ${pc.bold('5433')}.`,
            'Disconnect it, then re-run `spree db:reset`.',
            ...(stackUp ? ['', `Bring the stack back up with ${pc.bold('spree dev')}.`] : []),
          ])
        }
        throw err
      }

      p.log.success('Database reset.')
      if (stackUp) {
        // We stopped the operator's running stack to free the drop — they may not
        // realize it, so tell them how to bring it back.
        p.note(`Bring the stack back up with ${pc.bold('spree dev')}.`, 'Done')
      }
    })

  program
    .command('db:console')
    .description('Open a psql session against the dev database')
    .action(async () => {
      const ctx = detectProject()

      // psql needs a live postgres. postgres usually stays warm even when web is
      // down, so guide rather than guess if it isn't running — never `compose run`
      // here, which would start an empty server racing the psql shell.
      let postgresUp: boolean
      try {
        postgresUp = await isServiceRunning('postgres', ctx.projectDir)
      } catch (err) {
        p.cancel(dockerStackProblem(err, 'spree db:console').join('\n'))
        process.exit(1)
      }

      if (!postgresUp) {
        p.cancel(
          [
            'Postgres is not running.',
            `Start the stack with ${pc.bold('spree dev')}, then re-run ${pc.bold('spree db:console')}.`,
          ].join('\n'),
        )
        process.exit(1)
      }

      await dockerComposeExec(['psql', '-U', 'postgres', 'spree_development'], ctx.projectDir, {
        service: 'postgres',
      })
    })
}