This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix backup feature to use correct fs method (#8922)
* 🐛 Fix backup feature to use correct fs method * 💅 Fix format * Update framework/src/node/node.ts Co-authored-by: Boban Milošević <milosevic.boban@gmail.com> --------- Co-authored-by: Boban Milošević <milosevic.boban@gmail.com>
- Loading branch information
Showing
3 changed files
with
92 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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,25 @@ | ||
/* | ||
* Copyright © 2023 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { Database } from '@liskhq/lisk-db'; | ||
|
||
export const backupDatabase = async (dataPath: string, db: Database) => { | ||
const backupPath = path.resolve(dataPath, 'backup'); | ||
// if backup already exist, it should remove the directory and create a new checkpoint | ||
if (fs.existsSync(backupPath)) { | ||
fs.rmSync(backupPath, { recursive: true, force: true }); | ||
} | ||
await db.checkpoint(backupPath); | ||
}; |
This file contains 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,60 @@ | ||
/* | ||
* Copyright © 2023 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
|
||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { Database } from '@liskhq/lisk-db'; | ||
import { getRandomBytes } from '@liskhq/lisk-cryptography'; | ||
import { backupDatabase } from '../../../../src/node/utils/backup'; | ||
|
||
describe('backup', () => { | ||
const getDataPath = (name: string) => path.join(os.tmpdir(), Date.now().toString(), name); | ||
|
||
it('should create backup', async () => { | ||
const dbName = 'db-1'; | ||
const dataPath = getDataPath(dbName); | ||
const db = new Database(path.join(dataPath, dbName)); | ||
const key = getRandomBytes(10); | ||
await db.set(key, getRandomBytes(20)); | ||
|
||
await backupDatabase(dataPath, db); | ||
|
||
expect(fs.existsSync(path.join(dataPath, 'backup'))).toBeTrue(); | ||
db.close(); | ||
}); | ||
|
||
it('should remove old backup and create new one if exist', async () => { | ||
const dbName = 'db-2'; | ||
const dataPath = getDataPath(dbName); | ||
const db = new Database(path.join(dataPath, dbName)); | ||
const key = getRandomBytes(10); | ||
await db.set(key, getRandomBytes(20)); | ||
|
||
await backupDatabase(dataPath, db); | ||
const key2 = getRandomBytes(10); | ||
await db.set(key2, getRandomBytes(20)); | ||
|
||
expect(fs.existsSync(path.join(dataPath, 'backup'))).toBeTrue(); | ||
|
||
await backupDatabase(dataPath, db); | ||
|
||
expect(fs.existsSync(path.join(dataPath, 'backup'))).toBeTrue(); | ||
db.close(); | ||
const backupDB = new Database(path.join(dataPath, 'backup')); | ||
|
||
await expect(backupDB.has(key2)).resolves.toBeTrue(); | ||
backupDB.close(); | ||
}); | ||
}); |