mirror of
https://github.com/withastro/astro.git
synced 2025-02-24 22:46:02 -05:00
fix: disable foreign keys when recreating tables
This commit is contained in:
parent
a0cebe3d08
commit
30854cd635
1 changed files with 9 additions and 17 deletions
|
@ -1,4 +1,3 @@
|
||||||
import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
|
|
||||||
import type {
|
import type {
|
||||||
BooleanColumn,
|
BooleanColumn,
|
||||||
DBTable,
|
DBTable,
|
||||||
|
@ -9,14 +8,12 @@ import type {
|
||||||
JsonColumn,
|
JsonColumn,
|
||||||
NumberColumn,
|
NumberColumn,
|
||||||
TextColumn,
|
TextColumn,
|
||||||
ColumnsConfig,
|
|
||||||
MaybeArray,
|
MaybeArray,
|
||||||
ResolvedCollectionConfig,
|
|
||||||
} from '../core/types.js';
|
} from '../core/types.js';
|
||||||
import { bold } from 'kleur/colors';
|
import { bold } from 'kleur/colors';
|
||||||
import { type SQL, sql, getTableName } from 'drizzle-orm';
|
import { type SQL, sql } from 'drizzle-orm';
|
||||||
import { SQLiteAsyncDialect, SQLiteTable } from 'drizzle-orm/sqlite-core';
|
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core';
|
||||||
import { hasPrimaryKey } from './index.js';
|
import { hasPrimaryKey, type SqliteDB } from './index.js';
|
||||||
import { isSerializedSQL } from './types.js';
|
import { isSerializedSQL } from './types.js';
|
||||||
import { SEED_EMPTY_ARRAY_ERROR } from '../core/errors.js';
|
import { SEED_EMPTY_ARRAY_ERROR } from '../core/errors.js';
|
||||||
|
|
||||||
|
@ -48,7 +45,7 @@ export async function seedDev({
|
||||||
// Glob all potential seed files to catch renames and deletions.
|
// Glob all potential seed files to catch renames and deletions.
|
||||||
fileGlob,
|
fileGlob,
|
||||||
}: {
|
}: {
|
||||||
db: SqliteRemoteDatabase;
|
db: SqliteDB;
|
||||||
tables: DBTables;
|
tables: DBTables;
|
||||||
fileGlob: Record<string, () => Promise<void>>;
|
fileGlob: Record<string, () => Promise<void>>;
|
||||||
}) {
|
}) {
|
||||||
|
@ -62,13 +59,7 @@ export async function seedDev({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function recreateTables({
|
export async function recreateTables({ db, tables }: { db: SqliteDB; tables: DBTables }) {
|
||||||
db,
|
|
||||||
tables,
|
|
||||||
}: {
|
|
||||||
db: SqliteRemoteDatabase;
|
|
||||||
tables: DBTables;
|
|
||||||
}) {
|
|
||||||
const setupQueries: SQL[] = [];
|
const setupQueries: SQL[] = [];
|
||||||
for (const [name, collection] of Object.entries(tables)) {
|
for (const [name, collection] of Object.entries(tables)) {
|
||||||
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
|
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
|
||||||
|
@ -76,9 +67,10 @@ export async function recreateTables({
|
||||||
const indexQueries = getCreateIndexQueries(name, collection);
|
const indexQueries = getCreateIndexQueries(name, collection);
|
||||||
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
|
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
|
||||||
}
|
}
|
||||||
for (const q of setupQueries) {
|
await db.batch([
|
||||||
await db.run(q);
|
db.run(sql`pragma defer_foreign_keys=true;`),
|
||||||
}
|
...setupQueries.map((q) => db.run(q)),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add error checks to seed file by intercepting db.insert()
|
// TODO: add error checks to seed file by intercepting db.insert()
|
||||||
|
|
Loading…
Add table
Reference in a new issue