Skip to content

Commit b0556bc

Browse files
committed
fix: clear storage after migration transaction
1 parent d3bc8a0 commit b0556bc

4 files changed

Lines changed: 27 additions & 10 deletions

File tree

lib/db.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mkdir } from 'node:fs/promises'
33
import path from 'node:path'
44
import { createSingletonPromise } from '@antfu/utils'
55
import SQLite from 'better-sqlite3'
6+
import { createHooks } from 'hookable'
67
import { Kysely, Migrator, MysqlDialect, PostgresDialect, SqliteDialect } from 'kysely'
78
import { createPool } from 'mysql2'
89
import pg from 'pg'
@@ -135,11 +136,14 @@ export const getDatabase = createSingletonPromise(async () => {
135136
})
136137

137138
logger.info('Migrating database...')
139+
const hooks = createHooks<{
140+
afterMigrate: () => Promise<void>
141+
}>()
138142
const migrator = new Migrator({
139143
db,
140144
provider: {
141145
async getMigrations() {
142-
return migrations(env.DB_DRIVER)
146+
return migrations(env.DB_DRIVER, hooks)
143147
},
144148
},
145149
})
@@ -149,6 +153,7 @@ export const getDatabase = createSingletonPromise(async () => {
149153
// eslint-disable-next-line unicorn/no-process-exit
150154
process.exit(1)
151155
}
156+
await hooks.callHook('afterMigrate')
152157
logger.debug('Migration results', results)
153158
logger.success('Database migrated')
154159

lib/migrations.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import type { Hookable, Hooks } from 'hookable'
12
import type { Migration } from 'kysely'
23
import type { Env } from './schemas'
34
import { Storage } from './storage'
45

5-
export function migrations(driver: Env['DB_DRIVER']) {
6+
export function migrations(
7+
driver: Env['DB_DRIVER'],
8+
hooks: Hookable<{
9+
afterMigrate: () => Promise<void>
10+
}>,
11+
) {
612
return {
713
$0_init: {
814
async up(db) {
@@ -81,14 +87,10 @@ export function migrations(driver: Env['DB_DRIVER']) {
8187
async up(db) {
8288
const scopeColumnType = driver === 'mysql' ? 'varchar(255)' : 'text'
8389

84-
// clear all existing entries
85-
const adapter = await Storage.getAdapterFromEnv()
86-
8790
await Promise.all([
8891
db.deleteFrom('cache_entries').execute(),
8992
db.deleteFrom('storage_locations').execute(),
9093
db.deleteFrom('uploads').execute(),
91-
adapter.clear(),
9294
])
9395

9496
await db.schema
@@ -118,16 +120,17 @@ export function migrations(driver: Env['DB_DRIVER']) {
118120
async up(db) {
119121
const repoIdColumnType = driver === 'mysql' ? 'varchar(255)' : 'text'
120122

121-
// clear all existing entries
122-
const adapter = await Storage.getAdapterFromEnv()
123-
124123
await Promise.all([
125124
db.deleteFrom('cache_entries').execute(),
126125
db.deleteFrom('storage_locations').execute(),
127126
db.deleteFrom('uploads').execute(),
128-
adapter.clear(),
129127
])
130128

129+
hooks.hook('afterMigrate', async () => {
130+
const adapter = await Storage.getAdapterFromEnv()
131+
await adapter.clear()
132+
})
133+
131134
await db.schema
132135
.alterTable('cache_entries')
133136
.addColumn('repoId', repoIdColumnType, (col) => col.notNull())

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"croner": "^10.0.1",
4444
"execa": "^9.6.1",
4545
"h3": "^1.15.5",
46+
"hookable": "^6.1.0",
4647
"jose": "^6.1.3",
4748
"kysely": "^0.28.11",
4849
"mysql2": "3.18.1",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)