Skip to content

Commit

Permalink
chore: improve sqlite migratins for tests and dev 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Oct 27, 2022
1 parent 2001ddf commit 28c5dfc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/access-api/sql/migrate.js
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,
})
}
}
2 changes: 2 additions & 0 deletions packages/access-api/sql/reset.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE
IF EXISTS accounts;

0 comments on commit 28c5dfc

Please sign in to comment.