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/D3502pull/221/head
parent
b8486dcdba
commit
ec8ab598cb
@ -0,0 +1,239 @@
|
||||
import { synchronizeProducts } from 'app/gen-server/entity/Product';
|
||||
import { HomeDBManager } from 'app/gen-server/lib/HomeDBManager';
|
||||
import { applyPatch } from 'app/gen-server/lib/TypeORMPatches';
|
||||
import { getMigrations, getOrCreateConnection, undoLastMigration, updateDb } from 'app/server/lib/dbUtils';
|
||||
import { getDatabaseUrl } from 'app/server/lib/serverUtils';
|
||||
import { Gristifier } from 'app/server/utils/gristify';
|
||||
import { pruneActionHistory } from 'app/server/utils/pruneActionHistory';
|
||||
import * as commander from 'commander';
|
||||
import { Connection, getConnectionOptions } from 'typeorm';
|
||||
|
||||
/**
|
||||
* Main entrypoint for a cli toolbox for configuring aspects of Grist
|
||||
* and Grist documents.
|
||||
*/
|
||||
async function main() {
|
||||
// Tweak TypeORM support of SQLite a little bit to support transactions.
|
||||
applyPatch();
|
||||
const program = getProgram();
|
||||
await program.parseAsync(process.argv);
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main().then(() => process.exit(0)).catch(e => {
|
||||
// tslint:disable-next-line:no-console
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Grist companion client program as a commander object.
|
||||
* To actually run it, call parseAsync(argv), optionally after
|
||||
* adding any other commands that may be available.
|
||||
*/
|
||||
export function getProgram(): commander.Command {
|
||||
const program = commander.program;
|
||||
program
|
||||
.name('grist-toolbox') // haven't really settled on a name yet.
|
||||
// want to reserve "grist" for electron app?
|
||||
.description('a toolbox of handy Grist-related utilities');
|
||||
|
||||
addDbCommand(program, {nested: true});
|
||||
addHistoryCommand(program, {nested: true});
|
||||
addSiteCommand(program, {nested: true});
|
||||
addSqliteCommand(program);
|
||||
return program;
|
||||
}
|
||||
|
||||
// Add commands related to document history:
|
||||
// history prune <docId> [N]
|
||||
export function addHistoryCommand(program: commander.Command, options: CommandOptions) {
|
||||
const sub = section(program, {
|
||||
sectionName: 'history',
|
||||
sectionDescription: 'fiddle with history of a Grist document',
|
||||
...options,
|
||||
});
|
||||
sub('prune <docId>')
|
||||
.description('remove all but last N actions from doc')
|
||||
.argument('[N]', 'number of actions to keep', parseIntForCommander, 1)
|
||||
.action(pruneActionHistory);
|
||||
}
|
||||
|
||||
// Add commands related to sites:
|
||||
// site create <domain> <owner-email>
|
||||
export function addSiteCommand(program: commander.Command,
|
||||
options: CommandOptions) {
|
||||
const sub = section(program, {
|
||||
sectionName: 'site',
|
||||
sectionDescription: 'set up sites',
|
||||
...options
|
||||
});
|
||||
sub('create <domain> <owner-email>')
|
||||
.description('create a site')
|
||||
.action(async (domain, email) => {
|
||||
console.log("create a site");
|
||||
const profile = {email, name: email};
|
||||
const db = await getHomeDBManager();
|
||||
const user = await db.getUserByLogin(email, {profile});
|
||||
if (!user) {
|
||||
// This should not happen.
|
||||
throw new Error('failed to create user');
|
||||
}
|
||||
await db.addOrg(user, {
|
||||
name: domain,
|
||||
domain,
|
||||
}, {
|
||||
setUserAsOwner: false,
|
||||
useNewPlan: true,
|
||||
planType: 'teamFree'
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Add commands related to home/landing database:
|
||||
// db migrate
|
||||
// db revert
|
||||
// db check
|
||||
// db url
|
||||
export function addDbCommand(program: commander.Command,
|
||||
options: CommandOptions,
|
||||
reuseConnection?: Connection) {
|
||||
function withConnection(op: (connection: Connection) => Promise<number>) {
|
||||
return async () => {
|
||||
if (!process.env.TYPEORM_LOGGING) {
|
||||
process.env.TYPEORM_LOGGING = 'true';
|
||||
}
|
||||
const connection = reuseConnection || await getOrCreateConnection();
|
||||
const exitCode = await op(connection);
|
||||
if (exitCode !== 0) {
|
||||
program.error('db command failed', {exitCode});
|
||||
}
|
||||
};
|
||||
}
|
||||
const sub = section(program, {
|
||||
sectionName: 'db',
|
||||
sectionDescription: 'maintain the database of users, sites, workspaces, and docs',
|
||||
...options,
|
||||
});
|
||||
|
||||
sub('migrate')
|
||||
.description('run all pending migrations on database')
|
||||
.action(withConnection(async (connection) => {
|
||||
await updateDb(connection);
|
||||
return 0;
|
||||
}));
|
||||
|
||||
sub('revert')
|
||||
.description('revert last migration on database')
|
||||
.action(withConnection(async (connection) => {
|
||||
await undoLastMigration(connection);
|
||||
return 0;
|
||||
}));
|
||||
|
||||
sub('check')
|
||||
.description('check that there are no pending migrations on database')
|
||||
.action(withConnection(dbCheck));
|
||||
|
||||
sub('url')
|
||||
.description('construct a url for the database (for psql, catsql etc)')
|
||||
.action(withConnection(async () => {
|
||||
console.log(getDatabaseUrl(await getConnectionOptions(), true));
|
||||
return 0;
|
||||
}));
|
||||
}
|
||||
|
||||
// Add command related to sqlite:
|
||||
// sqlite gristify <sqlite-file>
|
||||
// sqlite clean <sqlite-file>
|
||||
export function addSqliteCommand(program: commander.Command) {
|
||||
const sub = program.command('sqlite')
|
||||
.description('commands for accessing sqlite files');
|
||||
|
||||
sub.command('gristify <sqlite-file>')
|
||||
.description('add grist metadata to an sqlite file')
|
||||
.option('--add-sort', 'add a manualSort column, important for adding/removing rows')
|
||||
.action((filename, options) => new Gristifier(filename).gristify(options));
|
||||
|
||||
sub.command('clean <sqlite-file>')
|
||||
.description('remove grist metadata from an sqlite file')
|
||||
.action(filename => new Gristifier(filename).degristify());
|
||||
}
|
||||
|
||||
// Report the status of the database. Migrations appied, migrations pending,
|
||||
// product information applied, product changes pending.
|
||||
export async function dbCheck(connection: Connection) {
|
||||
const migrations = await getMigrations(connection);
|
||||
const changingProducts = await synchronizeProducts(connection, false);
|
||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||
const log = process.env.TYPEORM_LOGGING === 'true' ? console.log : (...args: any[]) => null;
|
||||
const options = await getConnectionOptions();
|
||||
log("database url:", getDatabaseUrl(options, false));
|
||||
log("migration files:", options.migrations);
|
||||
log("migration directory:", (options.cli && options.cli.migrationsDir) || 'unspecified');
|
||||
log("migrations applied to db:", migrations.migrationsInDb);
|
||||
log("migrations listed in code:", migrations.migrationsInCode);
|
||||
let exitCode: number = 0;
|
||||
if (migrations.pendingMigrations.length) {
|
||||
log(`Migration(s) need to be applied: ${migrations.pendingMigrations}`);
|
||||
exitCode = 1;
|
||||
} else {
|
||||
log("No migrations need to be applied");
|
||||
}
|
||||
log("");
|
||||
if (changingProducts.length) {
|
||||
log("Products need updating:", changingProducts);
|
||||
log(` (to revert a product change, run an older version of the code)`);
|
||||
log(` (db:revert will not undo product changes)`);
|
||||
exitCode = 1;
|
||||
} else {
|
||||
log(`Products unchanged`);
|
||||
}
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
// Get an interface to the home db.
|
||||
export async function getHomeDBManager() {
|
||||
const dbManager = new HomeDBManager();
|
||||
await dbManager.connect();
|
||||
await dbManager.initializeSpecialIds();
|
||||
return dbManager;
|
||||
}
|
||||
|
||||
// Get a function for adding a command to a section of related commands.
|
||||
// There is a "nested" option that uses commander's nested command feature.
|
||||
// Older cli code may use an older unnested style.
|
||||
function section(program: commander.Command, options: {
|
||||
sectionName: string,
|
||||
sectionDescription: string,
|
||||
nested: boolean
|
||||
}) {
|
||||
// If unnested, we'll return a function that adds commands directly to the
|
||||
// program (section description is ignored in this case). If nested, we make
|
||||
// a command to represent the section, and return a function that adds to that.
|
||||
const sub = options.nested ?
|
||||
program.command(options.sectionName).description(options.sectionDescription) :
|
||||
program;
|
||||
return (name: string) => {
|
||||
if (options.nested) {
|
||||
return sub.command(name);
|
||||
} else {
|
||||
return sub.command(`${options.sectionName}:${name}`);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Options for command style.
|
||||
export interface CommandOptions {
|
||||
nested: boolean,
|
||||
sectionName?: string,
|
||||
}
|
||||
|
||||
// This is based on the recommended way to parse integers for commander.
|
||||
export function parseIntForCommander(value: string, prev: number) {
|
||||
const pvalue = parseInt(value, 10);
|
||||
if (isNaN(pvalue)) {
|
||||
throw new Error('Not a number.');
|
||||
}
|
||||
return pvalue;
|
||||
}
|
@ -0,0 +1,256 @@
|
||||
import { ColInfoWithId } from 'app/common/DocActions';
|
||||
import { ActiveDoc } from 'app/server/lib/ActiveDoc';
|
||||
import { DocManager } from 'app/server/lib/DocManager';
|
||||
import { makeExceptionalDocSession, OptDocSession } from 'app/server/lib/DocSession';
|
||||
import { createDummyGristServer } from 'app/server/lib/GristServer';
|
||||
import { TrivialDocStorageManager } from 'app/server/lib/IDocStorageManager';
|
||||
import { DBMetadata, quoteIdent, SQLiteDB } from 'app/server/lib/SQLiteDB';
|
||||
|
||||
/**
|
||||
* A utility class for modifying a SQLite file to be viewed/edited with Grist.
|
||||
*/
|
||||
export class Gristifier {
|
||||
public constructor(private _filename: string) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Grist metadata tables to a SQLite file. After this action,
|
||||
* the file can be opened as a Grist document, with partial functionality.
|
||||
* Level of functionality will depend on the nature of the tables in the
|
||||
* SQLite file.
|
||||
*
|
||||
* The `user_version` slot of SQLite will be modified by this operation,
|
||||
* losing whatever was in it previously.
|
||||
*
|
||||
* A "manualSort" column may be added to tables by specifying `addSort`,
|
||||
* to support a notion of order that exists in spreadsheets.
|
||||
*
|
||||
* Grist is very finicky about primary keys, and tables that don't match
|
||||
* its expectations cannot be viewed or edited directly at the moment.
|
||||
* Instead, views are added supporting selects, updates, inserts, and
|
||||
* deletes. Structure changes (e.g. adding/removing columns) are not
|
||||
* supported unfortunately.
|
||||
*
|
||||
* This is very much an experiment, with plenty of limits and
|
||||
* sharp edges. In general it isn't possible to treat an arbitrary
|
||||
* SQLite file as a Grist document, but in particular cases it can
|
||||
* work and be very useful.
|
||||
*/
|
||||
public async gristify(options: {addSort?: boolean}) {
|
||||
// Remove any existing Grist material from the file.
|
||||
await this.degristify();
|
||||
|
||||
// Enumerate user tables and columns.
|
||||
const inventory = await this._getUserTables();
|
||||
|
||||
// Grist keeps a schema number in the SQLite "user_version" slot,
|
||||
// so we need to zap it. This is the one destructive operation
|
||||
// involved in gristification.
|
||||
// TODO: consider moving schema information somewhere more neutral.
|
||||
await this._zapUserVersion();
|
||||
|
||||
// Open the file as an empty Grist document, creating Grist metadata
|
||||
// tables.
|
||||
const docManager = new DocManager(
|
||||
new TrivialDocStorageManager(), null, null, createDummyGristServer()
|
||||
);
|
||||
const activeDoc = new ActiveDoc(docManager, this._filename);
|
||||
const docSession = makeExceptionalDocSession('system');
|
||||
await activeDoc.createEmptyDoc(docSession, {useExisting: true});
|
||||
await activeDoc.waitForInitialization();
|
||||
|
||||
// Now "create" user tables and columns with Grist. The creation
|
||||
// will be fictitious since the tables and columns already exist -
|
||||
// they just don't have metadata describing them to Grist.
|
||||
const outcomes: TableOutcome[] = [];
|
||||
for (const [tableId, table] of Object.entries(inventory)) {
|
||||
const columnDefs = this._collectColumnDefinitions(table);
|
||||
if (!('id' in columnDefs)) {
|
||||
// Can't handle this table in Grist directly at the moment, but
|
||||
// we can do something via a view.
|
||||
await this._createView(docSession, activeDoc, tableId, Object.keys(table), columnDefs);
|
||||
outcomes.push({tableId, viewed: true, reason: 'id complications'});
|
||||
} else {
|
||||
await this._registerTable(docSession, activeDoc, tableId, columnDefs);
|
||||
if (options.addSort) {
|
||||
await this._addManualSort(activeDoc, tableId);
|
||||
outcomes.push({tableId, addManualSort: true});
|
||||
} else {
|
||||
outcomes.push({tableId});
|
||||
}
|
||||
}
|
||||
}
|
||||
await activeDoc.shutdown();
|
||||
|
||||
// Give a final readout of what happened for every table, since the
|
||||
// conversion process is quite noisy.
|
||||
for (const outcome of outcomes) {
|
||||
console.log(JSON.stringify(outcome));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all Grist metadata tables. Warning: attachments are considered metadata.
|
||||
*/
|
||||
public async degristify() {
|
||||
const db = await SQLiteDB.openDBRaw(this._filename);
|
||||
const tables = await db.all(
|
||||
`SELECT name FROM sqlite_master WHERE type='table' ` +
|
||||
` AND name LIKE '_grist%'`
|
||||
);
|
||||
for (const table of tables) {
|
||||
console.log(`Removing ${table.name}`);
|
||||
await db.exec(`DROP TABLE ${quoteIdent(table.name)}`);
|
||||
}
|
||||
const views = await db.all(
|
||||
`SELECT name FROM sqlite_master WHERE type='view' ` +
|
||||
` AND name LIKE 'GristView%'`
|
||||
);
|
||||
for (const view of views) {
|
||||
console.log(`Removing ${view.name}`);
|
||||
await db.exec(`DROP VIEW ${quoteIdent(view.name)}`);
|
||||
}
|
||||
await db.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make definitions for the table's columns. This is very crude, it handles
|
||||
* integers and leaves everything else as "Any".
|
||||
*/
|
||||
private _collectColumnDefinitions(table: DBMetadata[string]) {
|
||||
const defs: Record<string, ColInfoWithId> = {};
|
||||
for (const [colId, info] of Object.entries(table)) {
|
||||
if (colId.startsWith('manualSort')) { continue; }
|
||||
const type = info.toLowerCase();
|
||||
const c: ColInfoWithId = {
|
||||
id: colId,
|
||||
type: 'Any',
|
||||
isFormula: false,
|
||||
formula: '',
|
||||
};
|
||||
// see https://www.sqlite.org/datatype3.html#determination_of_column_affinity
|
||||
if (type.includes('int')) {
|
||||
c.type = 'Int';
|
||||
}
|
||||
if (colId === 'id') {
|
||||
if (c.type !== 'Int') {
|
||||
// Grist can only support integer id columns.
|
||||
// For now, just rename this column out of the way to id2, and use
|
||||
// a view to map SQLite's built-in ROWID to the id column.
|
||||
// TODO: could collide with a column called "id2".
|
||||
c.id = 'id2';
|
||||
}
|
||||
}
|
||||
defs[c.id] = c;
|
||||
}
|
||||
return defs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Support tables that don't have an integer column called "id" through views.
|
||||
* It would be better to enhance Grist to support a wider variety of scenarios,
|
||||
* but this is helpful for now.
|
||||
*/
|
||||
private async _createView(docSession: OptDocSession, activeDoc: ActiveDoc, tableId: string,
|
||||
cols: string[], columnDefs: Record<string, ColInfoWithId>) {
|
||||
const newName = `GristView_${tableId}`;
|
||||
function quote(name: string) {
|
||||
return quoteIdent(name === 'id' ? 'id2' : name);
|
||||
}
|
||||
function quoteForSelect(name: string) {
|
||||
if (name === 'id') { return 'id as id2'; }
|
||||
return quoteIdent(name);
|
||||
}
|
||||
|
||||
// View table tableId via a view GristView_tableId, with id and manualSort supplied
|
||||
// from ROWID. SQLite tables may not have a ROWID, but this is relatively rare.
|
||||
await activeDoc.docStorage.exec(`CREATE VIEW ${quoteIdent(newName)} AS SELECT ` +
|
||||
['ROWID AS id', 'ROWID AS manualSort', ...cols.map(quoteForSelect)].join(', ') +
|
||||
` FROM ${quoteIdent(tableId)}`);
|
||||
|
||||
// Make an INSTEAD OF UPDATE trigger, so that if someone tries to update the view,
|
||||
// we instead update the underlying table. Updates of manualSort or id are just ignored.
|
||||
// The trigger is a little awkward to write since we need to compare OLD and NEW
|
||||
// to see what changed - updating unchanged material could needlessly run afoul of
|
||||
// constraints.
|
||||
const updateTrigger = `CREATE TRIGGER ${quoteIdent('trigger_update_' + newName)} ` +
|
||||
`INSTEAD OF UPDATE ON ${quoteIdent(newName)} BEGIN ` +
|
||||
cols.map(col =>
|
||||
`UPDATE ${quoteIdent(tableId)} SET ` +
|
||||
`${quoteIdent(col)} = NEW.${quote(col)} ` +
|
||||
` WHERE OLD.${quote(col)} <> NEW.${quote(col)} ` +
|
||||
` AND ${quoteIdent(tableId)}.ROWID = NEW.ROWID`
|
||||
).join('; ') +
|
||||
`; END`;
|
||||
await activeDoc.docStorage.exec(updateTrigger);
|
||||
|
||||
// Make an INSTEAD OF INSERT trigger.
|
||||
const insertTrigger = `create trigger ${quoteIdent('trigger_insert_' + newName)} ` +
|
||||
`INSTEAD OF INSERT ON ${quoteIdent(newName)} BEGIN ` +
|
||||
`INSERT INTO ${quoteIdent(tableId)}` +
|
||||
'(' + cols.map(quoteIdent).join(',') + ') VALUES(' +
|
||||
cols.map(col => `NEW.${quote(col)}`).join(', ') +
|
||||
`); END`;
|
||||
await activeDoc.docStorage.exec(insertTrigger);
|
||||
|
||||
// Make an INSTEAD OF DELETE trigger.
|
||||
const deleteTrigger = `create trigger ${quoteIdent('trigger_delete_' + newName)} ` +
|
||||
`INSTEAD OF DELETE ON ${quoteIdent(newName)} BEGIN ` +
|
||||
`DELETE FROM ${quoteIdent(tableId)} WHERE ${quoteIdent(tableId)}.ROWID = OLD.ROWID` +
|
||||
`; END`;
|
||||
await activeDoc.docStorage.exec(deleteTrigger);
|
||||
|
||||
const result = await this._registerTable(docSession, activeDoc, newName, columnDefs);
|
||||
|
||||
// Now, tweak the Grist metadata to make the table name the expected one
|
||||
// (the table id as far as Grist is concerned must remain that of the view)
|
||||
const id = result.retValues[0].id;
|
||||
await activeDoc.docStorage.run('update _grist_Views_section set title = ? ' +
|
||||
'where id in (select rawViewSectionRef from _grist_Tables where id = ?)',
|
||||
[tableId, id]);
|
||||
await activeDoc.docStorage.run('update _grist_Views set name = ? ' +
|
||||
'where id in (select primaryViewId from _grist_Tables where id = ?)',
|
||||
[tableId, id]);
|
||||
}
|
||||
|
||||
private async _getUserTables(): Promise<DBMetadata> {
|
||||
// Enumerate existing tables and columns.
|
||||
const db = await SQLiteDB.openDBRaw(this._filename);
|
||||
const inventory = await db.collectMetadata();
|
||||
await db.close();
|
||||
// We are not interested in the special "sqlite_sequence" table.
|
||||
delete inventory.sqlite_sequence;
|
||||
return inventory;
|
||||
}
|
||||
|
||||
private async _zapUserVersion(): Promise<void> {
|
||||
const db = await SQLiteDB.openDBRaw(this._filename);
|
||||
await db.exec(`PRAGMA user_version = 0`);
|
||||
await db.close();
|
||||
}
|
||||
|
||||
private async _addManualSort(activeDoc: ActiveDoc, tableId: string) {
|
||||
const db = activeDoc.docStorage;
|
||||
await db.exec(`ALTER TABLE ${quoteIdent(tableId)} ADD COLUMN manualSort INTEGER`).catch(e => null);
|
||||
await db.exec(`UPDATE ${quoteIdent(tableId)} SET manualSort = id`);
|
||||
}
|
||||
|
||||
private async _registerTable(docSession: OptDocSession, activeDoc: ActiveDoc,
|
||||
tableId: string, args: Record<string, ColInfoWithId>) {
|
||||
delete args.id;
|
||||
activeDoc.onlyAllowMetaDataActionsOnDb(true);
|
||||
const result = await activeDoc.applyUserActions(docSession, [
|
||||
['AddTable', tableId, Object.values(args)],
|
||||
]);
|
||||
activeDoc.onlyAllowMetaDataActionsOnDb(false);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
interface TableOutcome {
|
||||
tableId: string;
|
||||
skipped?: boolean;
|
||||
viewed?: boolean;
|
||||
addManualSort?: boolean;
|
||||
reason?: string;
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
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);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue