From 85988b957b8da4bbdc8a78ee2f350276fcdf4a8b Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Thu, 13 Aug 2026 21:18:05 -0400 Subject: [PATCH] refactor(core): trim sqlite adapter paths (#42457) --- .../database/drizzle/sqlite-core/effect/db.ts | 53 ------- .../database/drizzle/up-migrations/sqlite.ts | 148 ------------------ 2 files changed, 201 deletions(-) diff --git a/packages/core/src/database/drizzle/sqlite-core/effect/db.ts b/packages/core/src/database/drizzle/sqlite-core/effect/db.ts index 523e5ce987..3bac07ea45 100644 --- a/packages/core/src/database/drizzle/sqlite-core/effect/db.ts +++ b/packages/core/src/database/drizzle/sqlite-core/effect/db.ts @@ -239,58 +239,5 @@ export class SQLiteEffectDatabase< ) => Effect.Effect = (tx, config) => this.session.transaction(tx, config) } -export type SQLiteEffectWithReplicas = Q & { $primary: Q; $replicas: Q[] } - -export const withReplicas = < - TEffectHKT extends QueryEffectHKTBase, - TRunResult, - TRelations extends AnyRelations, - Q extends SQLiteEffectDatabase, ->( - primary: Q, - replicas: [Q, ...Q[]], - getReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!, -): SQLiteEffectWithReplicas => { - const select: Q["select"] = (...args: []) => getReplica(replicas).select(...args) - const selectDistinct: Q["selectDistinct"] = (...args: []) => getReplica(replicas).selectDistinct(...args) - const $count: Q["$count"] = (...args: [any]) => getReplica(replicas).$count(...args) - const _with: Q["with"] = (...args: []) => getReplica(replicas).with(...args) - const $with = ((...args: [string] | [string, ColumnsSelection]) => - args.length === 1 - ? getReplica(replicas).$with(args[0]) - : getReplica(replicas).$with(args[0], args[1])) as Q["$with"] - - const update: Q["update"] = (...args: [any]) => primary.update(...args) - const insert: Q["insert"] = (...args: [any]) => primary.insert(...args) - const $delete: Q["delete"] = (...args: [any]) => primary.delete(...args) - const run: Q["run"] = (...args: [any]) => primary.run(...args) - const all: Q["all"] = (...args: [any]) => primary.all(...args) - const get: Q["get"] = (...args: [any]) => primary.get(...args) - const values: Q["values"] = (...args: [any]) => primary.values(...args) - const transaction: Q["transaction"] = (...args: [any]) => primary.transaction(...args) - - return { - ...primary, - update, - insert, - delete: $delete, - run, - all, - get, - values, - transaction, - $primary: primary, - $replicas: replicas, - select, - selectDistinct, - $count, - $with, - with: _with, - get query() { - return getReplica(replicas).query - }, - } -} - export type AnySQLiteEffectDatabase = SQLiteEffectDatabase export type AnySQLiteEffectSelectBase = SQLiteEffectSelectBase diff --git a/packages/core/src/database/drizzle/up-migrations/sqlite.ts b/packages/core/src/database/drizzle/up-migrations/sqlite.ts index 6cf99b0c3b..5356d6454d 100644 --- a/packages/core/src/database/drizzle/up-migrations/sqlite.ts +++ b/packages/core/src/database/drizzle/up-migrations/sqlite.ts @@ -1,22 +1,10 @@ /* oxlint-disable */ -import type { TablesRelationalConfig } from "drizzle-orm/_relations" import type { MigrationMeta } from "drizzle-orm/migrator" -import type { AnyRelations } from "drizzle-orm/relations" import { type SQL, sql } from "drizzle-orm/sql/sql" -import type { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core" -import type { SQLiteSession } from "drizzle-orm/sqlite-core/session" -import { GET_VERSION_FOR, MIGRATIONS_TABLE_VERSIONS, type UpgradeResult } from "./utils.js" /** @internal */ export type SQLiteMigrationTableRow = { id: number | null; hash: string; created_at: number } -type AsyncSQLiteDatabaseWithSession = BaseSQLiteDatabase<"async", unknown, Record> & { - session: { - all(query: SQL): Promise - } - transaction(transaction: (tx: { run(query: SQL): Promise }) => Promise): Promise -} - type SQLiteMigrationBackfillEntry = { name: string selector: @@ -115,139 +103,3 @@ export function buildSQLiteMigrationBackfillStatements( return statements } - -/** - * Detects the current version of the migrations table schema and upgrades it if needed. - * - * Version 0: Original schema (id, hash, created_at) - * Version 1: Extended schema (id, hash, created_at, name, applied_at) - */ -export function upgradeSyncIfNeeded( - migrationsTable: string, - session: SQLiteSession<"sync", unknown, Record, AnyRelations, TablesRelationalConfig>, - localMigrations: MigrationMeta[], -): UpgradeResult { - const tableExists = session.all(sql`SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ${migrationsTable}`) - - if (tableExists.length === 0) { - return { newDb: true } - } - - // Table exists, check table shape - const rows = session.all<{ column_name: string }>( - sql`SELECT name as column_name FROM pragma_table_info(${migrationsTable})`, - ) - - const version = GET_VERSION_FOR.sqlite(rows.map((r) => r.column_name)) - - for (let v = version; v < MIGRATIONS_TABLE_VERSIONS.sqlite; v++) { - const upgradeFn = upgradeSyncFunctions[v] - if (!upgradeFn) { - throw new Error(`No upgrade path from migration table version ${v} to ${v + 1}`) - } - upgradeFn(migrationsTable, session, localMigrations) - } - - return { newDb: false } -} - -const upgradeSyncFunctions: Record< - number, - ( - migrationsTable: string, - session: SQLiteSession<"sync", unknown, Record, AnyRelations, TablesRelationalConfig>, - localMigrations: MigrationMeta[], - ) => void -> = { - /** - * Upgrade from version 0 to version 1: - * 1. Read all existing DB migrations - * 2. Sort localMigrations ASC by millis and if the same - sort by name - * 3. Match each DB row to a local migration - * If multiple migrations share the same second, use hash matching as a tiebreaker - * Not implemented for now -> If hash matching fails, fall back to serial id ordering - * 5. Create extra column and backfill names for matched migrations - */ - 0: (migrationsTable, session, localMigrations) => { - const table = sql`${sql.identifier(migrationsTable)}` - const dbRows = session.all(sql`SELECT id, hash, created_at FROM ${table} ORDER BY id ASC`) - const statements = buildSQLiteMigrationBackfillStatements( - migrationsTable, - prepareSQLiteMigrationBackfill(dbRows, localMigrations), - ) - - session.transaction((tx) => { - for (const statement of statements) { - tx.run(statement) - } - }) - }, -} - -/** - * Detects the current version of the migrations table schema and upgrades it if needed. - * - * Version 0: Original schema (id, hash, created_at) - * Version 1: Extended schema (id, hash, created_at, name, applied_at) - */ -export async function upgradeAsyncIfNeeded( - migrationsTable: string, - db: AsyncSQLiteDatabaseWithSession, - localMigrations: MigrationMeta[], -): Promise { - // Check if the table exists at all - const tableExists = await db.session.all( - sql`SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ${migrationsTable}`, - ) - - if (tableExists.length === 0) { - return { newDb: true } - } - - const rows = await db.session.all<{ column_name: string }>( - sql`SELECT name as column_name FROM pragma_table_info(${migrationsTable})`, - ) - - const version = GET_VERSION_FOR.sqlite(rows.map((r) => r.column_name)) - - for (let v = version; v < MIGRATIONS_TABLE_VERSIONS.sqlite; v++) { - const upgradeFn = upgradeAsyncFunctions[v] - if (!upgradeFn) { - throw new Error(`No upgrade path from migration table version ${v} to ${v + 1}`) - } - await upgradeFn(migrationsTable, db, localMigrations) - } - - return { newDb: false } -} - -const upgradeAsyncFunctions: Record< - number, - (migrationsTable: string, db: AsyncSQLiteDatabaseWithSession, localMigrations: MigrationMeta[]) => Promise -> = { - /** - * Upgrade from version 0 to version 1: - * 1. Read all existing DB migrations - * 2. Sort localMigrations ASC by millis and if the same - sort by name - * 3. Match each DB row to a local migration - * If multiple migrations share the same second, use hash matching as a tiebreaker - * Not implemented for now -> If hash matching fails, fall back to serial id ordering - * 5. Create extra column and backfill names for matched migrations - */ - 0: async (migrationsTable, db, localMigrations) => { - const table = sql`${sql.identifier(migrationsTable)}` - const dbRows = await db.session.all( - sql`SELECT id, hash, created_at FROM ${table} ORDER BY id ASC`, - ) - const statements = buildSQLiteMigrationBackfillStatements( - migrationsTable, - prepareSQLiteMigrationBackfill(dbRows, localMigrations), - ) - - await db.transaction(async (tx) => { - for (const statement of statements) { - await tx.run(statement) - } - }) - }, -}