diff --git a/deploy/dev-instance/scripts/seed.sh b/deploy/dev-instance/scripts/seed.sh index 2524a33bd7..56c59b5d28 100755 --- a/deploy/dev-instance/scripts/seed.sh +++ b/deploy/dev-instance/scripts/seed.sh @@ -1,9 +1,10 @@ #!/usr/bin/env bash # Puts the sample document on the instance: "07/10 meeting agenda (sample)", # owned by alice, with 15 named versions by alice, bob, carol and dave. -# deploy.sh runs it on every deploy. It does nothing when the document is -# already there, and it takes about a minute at most: the history is built -# offline with its past timestamps, then stored in yhub. +# deploy.sh runs it on every deploy. It only adds what is missing, so it +# changes nothing when the document is complete, and it takes well under a +# minute: the history is built offline with its past timestamps, then stored +# in yhub. # # ./scripts/seed.sh [path to versions JSON] (default: seed-data/meetingVersions.json) set -euo pipefail @@ -34,9 +35,6 @@ docker run --rm \ mkdir -p /work && cp /seed/package.json /seed/package-lock.json /seed/*.mjs /work/ && cd /work && npm ci --prefer-offline --no-audit --no-fund --loglevel=error && node seed.mjs' -if [ ! -f "${out}/history.bin" ]; then - exit 0 # already there -fi docid="$(sed -E 's/.*"docid":"([^"]+)".*/\1/' "${out}/history.bin.json")" from_ms="$(sed -E 's/.*"accessesFrom":([0-9]+).*/\1/' "${out}/history.bin.json")" diff --git a/deploy/dev-instance/seed/import.mjs b/deploy/dev-instance/seed/import.mjs index 1ab3f528ef..9c1e72a9c6 100644 --- a/deploy/dev-instance/seed/import.mjs +++ b/deploy/dev-instance/seed/import.mjs @@ -2,7 +2,7 @@ // // It runs in the yhub image, with the @y/hub of the deployed server, and writes // the document the way Docs' fullMigrate does: one persisted row at clock 0, -// and the named versions. It refuses a document that yhub already holds. +// and the named versions. It only adds what yhub does not hold yet. // // Usage: node import.mjs import fs from 'node:fs'; @@ -14,10 +14,10 @@ const history = decodeAny(new Uint8Array(fs.readFileSync(process.argv[2]))); const docRef = { org: history.org, docid: history.docid, branch: 'main' }; const persistence = await createPersistence(process.env.POSTGRES, []); +// Only an empty document gets the content: a second clock-0 row would +// attribute the same content twice. const current = await persistence.retrieveDoc(docRef, {}); -if (current.lastClock !== '0') { - console.log(`import: yhub already holds ${docRef.docid}, nothing to do`); -} else { +if (current.lastClock === '0') { await persistence.store(docRef, { lastClock: '0', gcDoc: history.gcDoc, @@ -25,17 +25,25 @@ if (current.lastClock !== '0') { contentmap: history.contentmap, contentids: history.contentids, }); - for (const version of history.versions) { - await persistence.storeVersion(docRef, { - t: version.t, - name: version.name, - // what the version API stores for a version with no custom data - custom: encodeAny(null), - published: false, - at: version.t, - by: version.by, - }); + console.log(`import: stored the history of ${docRef.docid}`); +} else { + console.log(`import: yhub already holds the content of ${docRef.docid}`); +} +// The names are keyed on their time, and an existing one is kept. +let named = 0; +for (const version of history.versions) { + const stored = await persistence.storeVersion(docRef, { + t: version.t, + name: version.name, + // what the version API stores for a version with no custom data + custom: encodeAny(null), + published: false, + at: version.t, + by: version.by, + }); + if (stored != null) { + named++; } - console.log(`import: stored ${history.versions.length} versions of ${docRef.docid}`); } +console.log(`import: named ${named} new versions of ${history.versions.length}`); process.exit(0); diff --git a/deploy/dev-instance/seed/seed.mjs b/deploy/dev-instance/seed/seed.mjs index 48bc0ace58..5060e3d8e4 100644 --- a/deploy/dev-instance/seed/seed.mjs +++ b/deploy/dev-instance/seed/seed.mjs @@ -246,12 +246,11 @@ const found = await json( 'Listing documents', ); let doc = found.results.find((d) => d.title === TITLE); -if (doc && (await hasContent(owner, doc.id))) { - await ensureAccesses(owner, others, doc.id); - log(`already there: ${DOCS_URL}/docs/${doc.id}/`); - process.exit(0); -} -if (!doc) { +// An existing document still goes through the import, which only adds what is +// missing: a deploy that stopped after the content still gets its versions. +if (doc) { + log(`found ${DOCS_URL}/docs/${doc.id}/ (content: ${await hasContent(owner, doc.id)})`); +} else { doc = await json( await owner.api('/api/v1.0/documents/', { method: 'POST',