-
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
16 changed files
with
312 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,45 @@ | ||
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 | ||
} | ||
|
||
/** | ||
* @param {BetterSQLite3Database} db | ||
* @param {string} migrationsTableName | ||
* @returns {number} | ||
*/ | ||
export const countDrizzleMigrations = (db, migrationsTableName) => | ||
db.transaction((tx) => { | ||
const existsQuery = sql` | ||
SELECT EXISTS ( | ||
SELECT 1 | ||
FROM sqlite_master | ||
WHERE type IS 'table' | ||
AND name IS ${migrationsTableName} | ||
) 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(migrationsTableName)} | ||
` | ||
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,10 @@ | ||
import test from 'node:test' | ||
import assert from 'node:assert/strict' | ||
|
||
test('migrations pick up values that were previously not understood', async () => { | ||
// TODO(evanhahn) Write this test | ||
// Receive an observation with a new field, `foo` | ||
// Get the bytes, add it to the core, see that it's in SQLite without a `foo` column | ||
// Do a migration where `foo` is added | ||
// Reload the project and see that `foo` is now there | ||
}) |
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,3 @@ | ||
CREATE TABLE `foo` ( | ||
`bar` text | ||
); |
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 @@ | ||
{ | ||
"version": "5", | ||
"dialect": "sqlite", | ||
"id": "edd7be22-bd12-4abd-877b-2bf62df76631", | ||
"prevId": "00000000-0000-0000-0000-000000000000", | ||
"tables": { | ||
"foo": { | ||
"name": "foo", | ||
"columns": { | ||
"bar": { | ||
"name": "bar", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": false, | ||
"autoincrement": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {} | ||
} | ||
}, | ||
"enums": {}, | ||
"_meta": { | ||
"schemas": {}, | ||
"tables": {}, | ||
"columns": {} | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"version": "5", | ||
"dialect": "sqlite", | ||
"entries": [ | ||
{ | ||
"idx": 0, | ||
"version": "5", | ||
"when": 1716500546941, | ||
"tag": "0000_burly_rockslide", | ||
"breakpoints": true | ||
} | ||
] | ||
} |
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,3 @@ | ||
import { text, sqliteTable } from 'drizzle-orm/sqlite-core' | ||
|
||
export const foo = sqliteTable('foo', { bar: text('bar') }) |
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,3 @@ | ||
CREATE TABLE `foo` ( | ||
`bar` text | ||
); |
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,3 @@ | ||
CREATE TABLE `baz` ( | ||
`qux` text | ||
); |
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 @@ | ||
{ | ||
"version": "5", | ||
"dialect": "sqlite", | ||
"id": "edd7be22-bd12-4abd-877b-2bf62df76631", | ||
"prevId": "00000000-0000-0000-0000-000000000000", | ||
"tables": { | ||
"foo": { | ||
"name": "foo", | ||
"columns": { | ||
"bar": { | ||
"name": "bar", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": false, | ||
"autoincrement": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {} | ||
} | ||
}, | ||
"enums": {}, | ||
"_meta": { | ||
"schemas": {}, | ||
"tables": {}, | ||
"columns": {} | ||
} | ||
} |
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,46 @@ | ||
{ | ||
"version": "5", | ||
"dialect": "sqlite", | ||
"id": "9730c082-6ea7-4355-99b6-dd9eb12f9b82", | ||
"prevId": "edd7be22-bd12-4abd-877b-2bf62df76631", | ||
"tables": { | ||
"baz": { | ||
"name": "baz", | ||
"columns": { | ||
"qux": { | ||
"name": "qux", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": false, | ||
"autoincrement": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {} | ||
}, | ||
"foo": { | ||
"name": "foo", | ||
"columns": { | ||
"bar": { | ||
"name": "bar", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": false, | ||
"autoincrement": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {} | ||
} | ||
}, | ||
"enums": {}, | ||
"_meta": { | ||
"schemas": {}, | ||
"tables": {}, | ||
"columns": {} | ||
} | ||
} |
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,20 @@ | ||
{ | ||
"version": "5", | ||
"dialect": "sqlite", | ||
"entries": [ | ||
{ | ||
"idx": 0, | ||
"version": "5", | ||
"when": 1716500546941, | ||
"tag": "0000_burly_rockslide", | ||
"breakpoints": true | ||
}, | ||
{ | ||
"idx": 1, | ||
"version": "5", | ||
"when": 1716501178989, | ||
"tag": "0001_furry_king_cobra", | ||
"breakpoints": true | ||
} | ||
] | ||
} |
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,5 @@ | ||
import { text, sqliteTable } from 'drizzle-orm/sqlite-core' | ||
|
||
export const foo = sqliteTable('foo', { bar: text('bar') }) | ||
|
||
export const baz = sqliteTable('baz', { qux: text('qux') }) |
Oops, something went wrong.