The local D1 databases, brought up to date before the Worker starts: every
database the chosen environment binds (worker/wrangler.json's
d1_databases) gets wrangler d1 migrations apply <name> --local, into the
same local state the Worker then runs on. So a fresh checkout's first
//:dev has its tables, and a pulled migration applies on the next start.
Applying is idempotent: wrangler records what it has run in the database's
d1_migrations table.
stdin is not a terminal, so wrangler takes its "continue?" prompt's non-interactive answer (yes) instead of waiting on it.
14const CONFIG = 'worker/wrangler.json';
env as passed to wrangler --env ('dev', or '' for production);
persistTo when the Worker runs with its own --persist-to.
18export function migrateLocal({ env, persistTo }) { 19 const config = JSON.parse(readFileSync(CONFIG, 'utf8')); 20 const bindings = (env ? config.env[env] : config).d1_databases ?? []; 21 for (const { database_name: name } of bindings) { 22 const args = ['d1', 'migrations', 'apply', name, '--local', '--config', CONFIG, '--env', env]; 23 if (persistTo) args.push('--persist-to', persistTo); 24 execFileSync('wrangler', args, { 25 stdio: ['ignore', 'inherit', 'inherit'], 26 env: { ...process.env, WRANGLER_SEND_METRICS: 'false' }, 27 }); 28 } 29}