-
Notifications
You must be signed in to change notification settings - Fork 2
improve: speed up pull-db and add restore-db command #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+229
−121
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
35cb767
improve: speed up pull-db and add restore-db command
coji 7b287d6
refactor: extract shared constants and simplify ops commands
coji 2516472
fix: show help when ops is run without a command
coji fc5a888
fix: address CodeRabbit review feedback
coji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import consola from 'consola' | ||
| import fs from 'node:fs' | ||
| import path from 'node:path' | ||
| import { BACKUP_PREFIX, DATA_DIR, isDbFile } from '../lib/data-dir' | ||
|
|
||
| interface RestoreDbOptions { | ||
| name?: string | ||
| } | ||
|
|
||
| function listBackups(): string[] { | ||
| if (!fs.existsSync(DATA_DIR)) return [] | ||
| return fs | ||
| .readdirSync(DATA_DIR, { withFileTypes: true }) | ||
| .filter((d) => d.isDirectory() && d.name.startsWith(BACKUP_PREFIX)) | ||
| .map((d) => d.name) | ||
| .sort() | ||
| .reverse() | ||
| } | ||
|
|
||
| export async function restoreDbCommand(options: RestoreDbOptions) { | ||
| const backups = listBackups() | ||
| if (backups.length === 0) { | ||
| consola.error('No backups found in data/') | ||
| process.exit(1) | ||
| } | ||
|
|
||
| const target = options.name | ||
|
|
||
| if (!target) { | ||
| consola.info('Available backups (newest first):') | ||
| for (const b of backups) { | ||
| const files = fs | ||
| .readdirSync(path.join(DATA_DIR, b)) | ||
| .filter((f) => f.endsWith('.db')) | ||
| consola.log(` ${b} (${files.length} database(s))`) | ||
| } | ||
| consola.info('\nUsage: pnpm ops restore-db --name <backup_name>') | ||
| return | ||
| } | ||
|
|
||
| const backupDir = path.join(DATA_DIR, target) | ||
| const stat = fs.statSync(backupDir, { throwIfNoEntry: false }) | ||
| if (!stat?.isDirectory()) { | ||
| consola.error(`Backup not found: ${target}`) | ||
| consola.info('Run without --name to list available backups') | ||
| process.exit(1) | ||
| } | ||
|
|
||
| const filesToRestore = fs.readdirSync(backupDir).filter(isDbFile) | ||
|
|
||
| if (filesToRestore.length === 0) { | ||
| consola.error(`No database files in backup: ${target}`) | ||
| process.exit(1) | ||
| } | ||
|
|
||
| const dbCount = filesToRestore.filter((f) => f.endsWith('.db')).length | ||
| consola.warn( | ||
| `This will replace all current database files with ${dbCount} database(s) from ${target}.`, | ||
| ) | ||
| consola.warn( | ||
| 'Ensure no batch jobs or dev server are accessing the databases.', | ||
| ) | ||
| const confirmed = await consola.prompt('Continue with restore?', { | ||
| type: 'confirm', | ||
| }) | ||
| if (!confirmed) { | ||
| consola.info('Restore cancelled') | ||
| return | ||
| } | ||
|
|
||
| // Remove current db files | ||
| const currentDbFiles = fs.readdirSync(DATA_DIR).filter(isDbFile) | ||
| for (const f of currentDbFiles) { | ||
| fs.unlinkSync(path.join(DATA_DIR, f)) | ||
| } | ||
|
|
||
| // Copy backup files to data/ | ||
| for (const f of filesToRestore) { | ||
| fs.copyFileSync(path.join(backupDir, f), path.join(DATA_DIR, f)) | ||
| } | ||
|
|
||
| consola.success(`Restored ${dbCount} database(s) from ${target}`) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export const DATA_DIR = 'data' | ||
| export const BACKUP_PREFIX = 'backup_' | ||
|
|
||
| export function isDbFile(filename: string): boolean { | ||
| return ( | ||
| filename.endsWith('.db') || | ||
| filename.endsWith('.db-wal') || | ||
| filename.endsWith('.db-shm') | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.