// Prints one word: is this database already carrying the app's schema? // // ready the baseline tables exist; migrations can be applied on top // empty nothing here yet // unreachable could not connect or could not tell // // Used by docker-entrypoint.sh to decide whether it may migrate before the app // starts. The schema has two layers — src/db/database.js creates the baseline // with CREATE TABLE IF NOT EXISTS on first connect, and the migrations layer on // top of it. Against an empty database the migrations fail, because the // earliest of them alters a table only the baseline creates. // // Never exits non-zero: an answer of "unreachable" is for the caller to handle, // and a database still opening its socket is not an error worth stopping on. const { Client } = require('pg'); const client = new Client({ connectionString: process.env.DATABASE_URL }); client.connect() .then(() => client.query("select to_regclass('public.users') is not null as ready")) .then((result) => { process.stdout.write(result.rows[0] && result.rows[0].ready ? 'ready' : 'empty'); return client.end(); }) .catch(() => { process.stdout.write('unreachable'); try { client.end(); } catch (e) { /* already down */ } });