Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions ops/commands/pull-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import consola from 'consola'
import { execFileSync } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'
import { BACKUP_PREFIX, DATA_DIR, isDbFile } from '../lib/data-dir'
import {
BACKUP_PREFIX,
DATA_DIR,
isDbFile,
removeAllDbFiles,
} from '../lib/data-dir'

const VALID_APP_NAME = /^[a-z0-9-]+$/

Expand Down Expand Up @@ -145,10 +150,17 @@ export function pullDbCommand(options: PullDbOptions) {
// Ensure data directory exists
fs.mkdirSync(DATA_DIR, { recursive: true })

// Step 2: Remote backup + tar + pull + extract
// Step 2: Remove old DB files (including -wal/-shm) to prevent
// stale WAL journals from corrupting newly pulled databases
const removed = removeAllDbFiles()
if (removed > 0) {
consola.success(`Removed ${removed} old database file(s)`)
}

// Step 3: Remote backup + tar + pull + extract
const dbFiles = pullAllDbs(app)

// Step 3: Sanitize export settings
// Step 4: Sanitize export settings
if (!noSanitize) {
consola.start('Sanitizing export settings...')
for (const file of dbFiles) {
Expand Down
12 changes: 7 additions & 5 deletions ops/commands/restore-db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import consola from 'consola'
import fs from 'node:fs'
import path from 'node:path'
import { BACKUP_PREFIX, DATA_DIR, isDbFile } from '../lib/data-dir'
import {
BACKUP_PREFIX,
DATA_DIR,
isDbFile,
removeAllDbFiles,
} from '../lib/data-dir'

interface RestoreDbOptions {
name?: string
Expand Down Expand Up @@ -69,10 +74,7 @@ export async function restoreDbCommand(options: RestoreDbOptions) {
}

// Remove current db files
const currentDbFiles = fs.readdirSync(DATA_DIR).filter(isDbFile)
for (const f of currentDbFiles) {
fs.unlinkSync(path.join(DATA_DIR, f))
}
removeAllDbFiles()

// Copy backup files to data/
for (const f of filesToRestore) {
Expand Down
12 changes: 12 additions & 0 deletions ops/lib/data-dir.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'

export const DATA_DIR = 'data'
export const BACKUP_PREFIX = 'backup_'

Expand All @@ -8,3 +11,12 @@ export function isDbFile(filename: string): boolean {
filename.endsWith('.db-shm')
)
}

/** Remove all .db / .db-wal / .db-shm files from DATA_DIR */
export function removeAllDbFiles(): number {
const files = fs.readdirSync(DATA_DIR).filter(isDbFile)
for (const file of files) {
fs.unlinkSync(path.join(DATA_DIR, file))
}
return files.length
}