0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

chore: enable foreign keys in local mode only

This commit is contained in:
bholmesdev 2024-02-01 18:07:45 -05:00
parent feed9ea0bd
commit 06f96661af
2 changed files with 14 additions and 3 deletions

View file

@ -71,6 +71,7 @@ function astroDBIntegration(): AstroIntegration {
data: configWithDb.db?.data,
logger,
mode: command === 'dev' ? 'dev' : 'build',
useForeignKeys: true,
});
logger.info('Collections set up 🚀');

View file

@ -25,17 +25,20 @@ export async function setupDbTables({
collections,
logger,
mode,
// TODO: Remove once Turso has foreign key PRAGMA support
useForeignKeys = false,
}: {
db: SqliteRemoteDatabase;
data?: DBUserConfig['data'];
collections: DBCollections;
logger?: AstroIntegrationLogger;
mode: 'dev' | 'build';
useForeignKeys?: boolean;
}) {
const setupQueries: SQL[] = [];
for (const [name, collection] of Object.entries(collections)) {
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${name}`);
const createQuery = sql.raw(getCreateTableQuery(name, collection));
const createQuery = sql.raw(getCreateTableQuery(name, collection, useForeignKeys));
const indexQueries = getCreateIndexQueries(name, collection);
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
}
@ -70,7 +73,12 @@ export async function setupDbTables({
}
}
export function getCreateTableQuery(collectionName: string, collection: DBCollection) {
export function getCreateTableQuery(
collectionName: string,
collection: DBCollection,
// TODO: Remove once Turso has foreign key PRAGMA support
useForeignKeys = false
) {
let query = `CREATE TABLE ${sqlite.escapeName(collectionName)} (`;
const colQueries = [];
@ -87,7 +95,9 @@ export function getCreateTableQuery(collectionName: string, collection: DBCollec
colQueries.push(colQuery);
}
colQueries.push(...getCreateForeignKeyQueries(collectionName, collection));
if (useForeignKeys) {
colQueries.push(...getCreateForeignKeyQueries(collectionName, collection));
}
query += colQueries.join(', ') + ')';
return query;