diff --git a/test/setupPaths.js b/test/setupPaths.js new file mode 100644 index 00000000..f579a663 --- /dev/null +++ b/test/setupPaths.js @@ -0,0 +1,7 @@ +// enhance require() to support project paths and typescript. +const path = require('path'); +const appModulePath = require('app-module-path'); +const root = path.dirname(__dirname); +appModulePath.addPath(path.join(root, "_build")); +appModulePath.addPath(path.join(root, "_build/core")); +appModulePath.addPath(path.join(root, "_build/ext")); diff --git a/test/upgradeDocument b/test/upgradeDocument new file mode 100755 index 00000000..be3f6cc3 --- /dev/null +++ b/test/upgradeDocument @@ -0,0 +1,4 @@ +#!/usr/bin/env node + +require('./setupPaths'); +require('test/upgradeDocumentImpl').main().catch(e => console.error(String(e))); diff --git a/test/upgradeDocumentImpl.ts b/test/upgradeDocumentImpl.ts new file mode 100644 index 00000000..499c28a0 --- /dev/null +++ b/test/upgradeDocumentImpl.ts @@ -0,0 +1,43 @@ +/** + * Upgrade one or more documents (both the DocStorage and schema migrations). + * + * Usage: + * test/upgradeDocument + */ +import {copyFile} from 'app/server/lib/docUtils'; +import {createDocTools} from 'test/server/docTools'; +import log from 'app/server/lib/log'; +import * as fs from "fs"; + +export async function main() { + const docPaths = process.argv.slice(2); + if (docPaths.length === 0) { + console.log(`Usage:\n test/upgradeDocument path/to/doc.grist ...\n`); + throw new Error("Document argument required"); + } + for (const docPath of docPaths) { + if (!docPath.endsWith('.grist')) { + throw new Error(`Document path should have .grist extension: ${docPath}`); + } + if (!fs.existsSync(docPath)) { + throw new Error(`Document path doesn't exist: ${docPath}`); + } + } + + const prevLogLevel = log.transports.file.level; + log.transports.file.level = 'warn'; + const docTools = createDocTools(); + await docTools.before(); + try { + for (const docPath of docPaths) { + console.log(`Upgrading ${docPath}`); + const activeDoc = await docTools.loadLocalDoc(docPath); + await activeDoc.waitForInitialization(); + await activeDoc.shutdown(); + await copyFile(docTools.getStorageManager().getPath(activeDoc.docName), docPath); + } + } finally { + await docTools.after(); + log.transports.file.level = prevLogLevel; + } +}