Skip to content

Commit 045d602

Browse files
authored
feat: database version (#3148)
1 parent e97c787 commit 045d602

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

src/module.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ import { getLocalDatabase, refineDatabaseConfig, resolveDatabaseAdapter } from '
3636
export * from './utils'
3737
export type * from './types'
3838

39+
/**
40+
* Database version is used to identify schema changes
41+
* and drop the info table when the version is not supported
42+
*/
43+
const databaseVersion = 'v3.2.0'
44+
3945
export default defineNuxtModule<ModuleOptions>({
4046
meta: {
4147
name: 'Content',
@@ -142,6 +148,7 @@ export default defineNuxtModule<ModuleOptions>({
142148
wsUrl: '',
143149
}
144150
nuxt.options.runtimeConfig.content = {
151+
databaseVersion,
145152
version,
146153
database: options.database,
147154
localDatabase: options._localDatabase!,
@@ -303,7 +310,7 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
303310
collectionQueries.push(...list.flatMap(([, sql]) => sql!))
304311
}
305312

306-
const version = collectionChecksum[collection.name] = hash(collectionQueries)
313+
const version = collectionChecksum[collection.name] = `${databaseVersion}--${hash(collectionQueries)}`
307314

308315
collectionDump[collection.name] = [
309316
// we have to start the series of queries
@@ -317,7 +324,7 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
317324
...collectionQueries,
318325

319326
// and finally when we are finished, we update the info table to say that the init is done
320-
`UPDATE ${infoCollection.tableName} SET ready = true WHERE id = 'checksum_${collection.name}'`,
327+
`UPDATE ${infoCollection.tableName} SET ready = true WHERE id = 'checksum_${collection.name}';`,
321328
]
322329
}
323330

src/presets/nuxthub.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export default definePreset({
2626
// Write SQL dump to database queries when not in dev mode
2727
await mkdir(resolve(nitroConfig.rootDir, '.data/hub/database/queries'), { recursive: true })
2828
let i = 1
29-
let dump = ''
29+
// Drop info table and prepare for new dump
30+
let dump = 'DROP TABLE IF EXISTS _content_info;\n'
3031
const dumpFiles: Array<{ file: string, content: string }> = []
3132
Object.values(options.manifest.dump).forEach((value) => {
3233
value.forEach((line) => {

src/runtime/internal/database.server.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ async function _checkAndImportDatabaseIntegrity(event: H3Event, collection: stri
6565

6666
const before = await db.first<{ version: string, ready: boolean }>(`select * from ${tables.info} where id = ?`, [`checksum_${collection}`]).catch((): null => null)
6767

68+
if (before?.version && !String(before.version)?.startsWith(`${config.databaseVersion}--`)) {
69+
// database version is not supported, drop the info table
70+
await db.exec(`DROP TABLE IF EXISTS ${tables.info}`)
71+
before.version = ''
72+
}
73+
6874
if (before?.version) {
6975
if (before.version === integrityVersion) {
7076
if (before.ready) {
71-
// table is already initialized and ready, use it
77+
// table is already initialized and ready, use it
7278
return true
7379
}
7480

src/types/module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export interface ModuleOptions {
184184
export interface RuntimeConfig {
185185
content: {
186186
version: string
187+
databaseVersion: string
187188
database: D1DatabaseConfig | SqliteDatabaseConfig | PostgreSQLDatabaseConfig
188189
localDatabase: SqliteDatabaseConfig
189190
integrityCheck: boolean

0 commit comments

Comments
 (0)