Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions deploy/dev-instance/scripts/seed.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")"

Expand Down
38 changes: 23 additions & 15 deletions deploy/dev-instance/seed/import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <history file>
import fs from 'node:fs';
Expand All @@ -14,28 +14,36 @@ 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,
nongcDoc: history.nongcDoc,
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);
11 changes: 5 additions & 6 deletions deploy/dev-instance/seed/seed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading