-
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
13 changed files
with
287 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.