gristlabs_grist-core/app/server/utils/pruneActionHistory.ts

64 lines
2.2 KiB
TypeScript
Raw Normal View History

(core) add a `yarn run cli` tool, and add a `sqlite gristify` option Summary: This adds rudimentary support for opening certain SQLite files in Grist. If you have a file such as `landing.db` in Grist, you can convert it to Grist format by doing (either in monorepo or grist-core): ``` yarn run cli -h yarn run cli sqlite -h yarn run cli sqlite gristify landing.db ``` The file is now openable by Grist. To actually do so with the regular Grist server, you'll need to either import it, or convert some doc you don't care about in the `samples/` directory to be a soft link to it (and then force a reload). This implementation is a rudimentary experiment. Here are some awkwardnesses: * Only tables that happen to have a column called `id`, and where the column happens to be an integer, can be opened directly with Grist as it is today. That could be generalized, but it looked more than a Gristathon's worth of work, so I instead used SQLite views. * Grist will handle tables that start with an uncapitalized letter a bit erratically. You can successfully add columns, for example, but removing them will cause sadness - Grist will rename the table in a confused way. * I didn't attempt to deal with column names with spaces etc (though views could deal with those). * I haven't tried to do any fancy type mapping. * Columns with constraints can make adding new rows impossible in Grist, since Grist requires that a row can be added with just a single cell set. Test Plan: added small test Reviewers: georgegevoian Reviewed By: georgegevoian Differential Revision: https://phab.getgrist.com/D3502
2022-07-14 09:32:06 +00:00
import * as gutil from 'app/common/gutil';
import {ActionHistoryImpl} from 'app/server/lib/ActionHistoryImpl';
import {DocStorage} from 'app/server/lib/DocStorage';
import {DocStorageManager} from 'app/server/lib/DocStorageManager';
import * as docUtils from 'app/server/lib/docUtils';
import log from 'app/server/lib/log';
/**
* A utility script for cleaning up the action log.
*
* @param {String} docPath - The path to the document from the current directory including
* the document name.
* @param {Int} keepN - The number of recent actions to keep. Must be at least 1. Defaults to 1
* if not provided.
*/
export async function pruneActionHistory(docPath: string, keepN: number) {
if (!docPath || !gutil.endsWith(docPath, '.grist')) {
throw new Error('Invalid document: Document should be a valid .grist file');
}
const storageManager = new DocStorageManager(".", ".");
const docStorage = new DocStorage(storageManager, docPath);
const backupPath = gutil.removeSuffix(docPath, '.grist') + "-backup.grist";
// If the backup already exists, abort. Otherwise, create a backup copy and continue.
const exists = await docUtils.pathExists(backupPath);
if (exists) { throw new Error('Backup file already exists, aborting pruneActionHistory'); }
await docUtils.copyFile(docPath, backupPath);
await docStorage.openFile();
try {
const history = new ActionHistoryImpl(docStorage);
await history.initialize();
await history.deleteActions(keepN);
} finally {
await docStorage.shutdown();
}
}
/**
* Variant that accepts and parses command line arguments.
*/
export async function pruneActionHistoryFromConsole(argv: string[]): Promise<number> {
if (argv.length === 0) {
log.error("Please supply document name, and optionally the number of actions to preserve (default=1)");
return 1;
}
const docPath = argv[0];
const keepN = parseInt(argv[1], 10) || 1;
try {
await pruneActionHistory(docPath, keepN);
} catch (e) {
log.error(e);
return 1;
}
return 0;
}
if (require.main === module) {
pruneActionHistoryFromConsole(process.argv.slice(2)).catch((e) => {
log.error("pruneActionHistory failed: %s", e);
process.exit(1);
});
}