Files
shd.build/scripts/db-migrate.mjs
T

25 lines
813 B
JavaScript

import { connect } from '@tursodatabase/database';
import { mkdir, readFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import process from 'node:process';
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db');
const schemaPath = resolve('./database/schema.sql');
await mkdir(dirname(databasePath), { recursive: true });
const database = await connect(databasePath);
const schema = await readFile(schemaPath, 'utf8');
// The project schema deliberately avoids semicolons inside string literals.
const statements = schema
.split(';')
.map((statement) => statement.trim())
.filter(Boolean);
for (const statement of statements) {
await database.exec(statement);
}
console.log(`Applied ${statements.length} schema statements to ${databasePath}`);