fix: db compaction

This commit is contained in:
Ben Allfree 2024-11-09 15:58:51 +00:00
parent b7d9badd7c
commit a0df601f96

View File

@ -1,18 +1,34 @@
import { exec } from 'child_process'
import { execSync } from 'child_process'
import { globSync } from 'glob'
import { DATA_ROOT } from '../../../../core'
import { logger } from '../../../common/Logger'
export const compact = async () => {
const { info } = logger()
const { info, error } = logger()
const files = [`data`, `logs`].flatMap((db) =>
globSync(`${DATA_ROOT()}/*/pb_data/${db}.db{-shm,-wal}`),
)
const files = [
...new Set(
[`data`, `logs`].flatMap((db) =>
globSync(`${DATA_ROOT()}/*/pb_data/${db}.db{-shm,-wal}`).map((f) =>
f.replace(/-(?:shm|wal)$/, ''),
),
),
),
]
files.map(async (file) => {
info(`Compacting ${file}`)
exec(`sqlite3 ${file} ".tables"`)
info(`Compacting ${files.length} files`, { files })
files.forEach((file) => {
const { info, error } = logger().child(file)
const cmd = `sqlite3 ${file} ".tables"`
info(cmd)
try {
execSync(cmd)
info(`Finished compacting`)
} catch (e) {
error(`Error compacting`, e)
}
})
// console.log('Compaction complete')
info(`Compaction complete`)
}