Skip to content

Commit

Permalink
chore: remove recreateTables from runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Mar 7, 2024
1 parent ee1574d commit 71e8987
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
25 changes: 24 additions & 1 deletion packages/db/src/core/integration/vite-plugin-db.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { fileURLToPath } from 'node:url';
import { normalizePath } from 'vite';
import { SEED_DEV_FILE_NAME, recreateTables } from '../../runtime/queries.js';
import {
SEED_DEV_FILE_NAME,
getCreateIndexQueries,
getCreateTableQuery,
} from '../../runtime/queries.js';
import { DB_PATH, RUNTIME_CONFIG_IMPORT, RUNTIME_IMPORT, VIRTUAL_MODULE_ID } from '../consts.js';
import type { DBTables } from '../types.js';
import { type VitePlugin, getDbDirectoryUrl, getRemoteDatabaseUrl } from '../utils.js';
import { createLocalDatabaseClient } from '../../runtime/db-client.js';
import { type SQL, sql } from 'drizzle-orm';
import type { SqliteDB } from '../../runtime/index.js';
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core';

const WITH_SEED_VIRTUAL_MODULE_ID = 'astro:db:seed';

Expand Down Expand Up @@ -144,3 +151,19 @@ function getStringifiedCollectionExports(tables: DBTables) {
)
.join('\n');
}

const sqlite = new SQLiteAsyncDialect();

async function recreateTables({ db, tables }: { db: SqliteDB; tables: DBTables }) {
const setupQueries: SQL[] = [];
for (const [name, table] of Object.entries(tables)) {
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
const createQuery = sql.raw(getCreateTableQuery(name, table));
const indexQueries = getCreateIndexQueries(name, table);
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
}
await db.batch([
db.run(sql`pragma defer_foreign_keys=true;`),
...setupQueries.map((q) => db.run(q)),
]);
}
2 changes: 0 additions & 2 deletions packages/db/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export { sql };
export type SqliteDB = LibSQLDatabase;
export type { Table } from './types.js';
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
// only used for in-memory db in test fixtures
export { recreateTables } from './queries.js';

export function hasPrimaryKey(column: DBColumn) {
return 'primaryKey' in column.schema && !!column.schema.primaryKey;
Expand Down
19 changes: 2 additions & 17 deletions packages/db/src/runtime/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type SQL, sql } from 'drizzle-orm';
import { type SQL } from 'drizzle-orm';
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core';
import { bold } from 'kleur/colors';
import {
Expand All @@ -12,33 +12,18 @@ import type {
ColumnType,
DBColumn,
DBTable,
DBTables,
DateColumn,
JsonColumn,
NumberColumn,
TextColumn,
} from '../core/types.js';
import { type SqliteDB, hasPrimaryKey } from './index.js';
import { hasPrimaryKey } from './index.js';
import { isSerializedSQL } from './types.js';

const sqlite = new SQLiteAsyncDialect();

export const SEED_DEV_FILE_NAME = ['seed.ts', 'seed.js', 'seed.mjs', 'seed.mts'];

export async function recreateTables({ db, tables }: { db: SqliteDB; tables: DBTables }) {
const setupQueries: SQL[] = [];
for (const [name, table] of Object.entries(tables)) {
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
const createQuery = sql.raw(getCreateTableQuery(name, table));
const indexQueries = getCreateIndexQueries(name, table);
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
}
await db.batch([
db.run(sql`pragma defer_foreign_keys=true;`),
...setupQueries.map((q) => db.run(q)),
]);
}

export function getDropTableIfExistsQuery(tableName: string) {
return `DROP TABLE IF EXISTS ${sqlite.escapeName(tableName)}`;
}
Expand Down

0 comments on commit 71e8987

Please sign in to comment.