-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve sqlite migratins for tests and dev 2
- Loading branch information
1 parent
2001ddf
commit 28c5dfc
Showing
2 changed files
with
39 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { escapeSQLiteIdentifier } from '@databases/escape-identifier' | ||
import sql from '@databases/sql' | ||
import path from 'path' | ||
import { fileURLToPath } from 'url' | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
/** @type {import('@databases/sql').FormatConfig} */ | ||
const sqliteFormat = { | ||
escapeIdentifier: (str) => escapeSQLiteIdentifier(str), | ||
formatValue: (value) => ({ placeholder: '?', value }), | ||
} | ||
|
||
const migrations = [sql.file(`${__dirname}/tables.sql`)] | ||
|
||
/** | ||
* Probably should batch queries and use https://www.atdatabases.org/docs/split-sql-query to split inlined queries | ||
* | ||
* @see https://docs.google.com/document/d/1QpUryGBWaGbAIjkw2URwpV6Btp5S-XQVkBJJs85dLRc/edit# | ||
* | ||
* @param {D1Database} db | ||
*/ | ||
export async function migrate(db) { | ||
try { | ||
for (const m of migrations) { | ||
await db.exec(m.format(sqliteFormat).text.replace(/\n/g, '')) | ||
} | ||
} catch (error) { | ||
const err = /** @type {Error} */ (error) | ||
// eslint-disable-next-line no-console | ||
console.log({ | ||
message: err.message, | ||
// @ts-ignore | ||
cause: err.cause?.message, | ||
}) | ||
} | ||
} |
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,2 @@ | ||
DROP TABLE | ||
IF EXISTS accounts; |