-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rebuild SQLite when migrations occur
- Loading branch information
Showing
5 changed files
with
132 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { sql } from 'drizzle-orm' | ||
import { assert } from '../utils.js' | ||
/** @import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3' */ | ||
|
||
/** | ||
* @param {unknown} queryResult | ||
* @returns {number} | ||
*/ | ||
const getNumberResult = (queryResult) => { | ||
assert( | ||
queryResult && | ||
typeof queryResult === 'object' && | ||
'result' in queryResult && | ||
typeof queryResult.result === 'number', | ||
'expected query to return proper result' | ||
) | ||
return queryResult.result | ||
} | ||
|
||
/** | ||
* Get the number of rows in a table using `SELECT COUNT(*)`. | ||
* Returns 0 if the table doesn't exist. | ||
* | ||
* @param {BetterSQLite3Database} db | ||
* @param {string} tableName | ||
* @returns {number} | ||
*/ | ||
export const tableCountIfExists = (db, tableName) => | ||
db.transaction((tx) => { | ||
const existsQuery = sql` | ||
SELECT EXISTS ( | ||
SELECT 1 | ||
FROM sqlite_master | ||
WHERE type IS 'table' | ||
AND name IS ${tableName} | ||
) AS result | ||
` | ||
const existsResult = tx.get(existsQuery) | ||
const exists = getNumberResult(existsResult) | ||
if (!exists) return 0 | ||
|
||
const countQuery = sql` | ||
SELECT COUNT(*) AS result | ||
FROM ${sql.identifier(tableName)} | ||
` | ||
const countResult = tx.get(countQuery) | ||
return getNumberResult(countResult) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Database from 'better-sqlite3' | ||
import { drizzle } from 'drizzle-orm/better-sqlite3' | ||
import assert from 'node:assert/strict' | ||
import test, { describe } from 'node:test' | ||
import { tableCountIfExists } from '../../src/lib/drizzle-helpers.js' | ||
|
||
describe('table count if exists', () => { | ||
const db = new Database(':memory:') | ||
|
||
db.exec('CREATE TABLE empty (ignored)') | ||
|
||
db.exec('CREATE TABLE filled (n INT)') | ||
db.exec('INSERT INTO filled (n) VALUES (9)') | ||
db.exec('INSERT INTO filled (n) VALUES (8)') | ||
db.exec('INSERT INTO filled (n) VALUES (7)') | ||
|
||
const driz = drizzle(db) | ||
|
||
test("when table doesn't exist", () => { | ||
assert.equal(tableCountIfExists(driz, 'doesnt_exist'), 0) | ||
}) | ||
|
||
test('when table is empty', () => { | ||
assert.equal(tableCountIfExists(driz, 'empty'), 0) | ||
}) | ||
|
||
test('when table has rows', () => { | ||
assert.equal(tableCountIfExists(driz, 'filled'), 3) | ||
}) | ||
}) |