tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
handle migrating all records
awarm.space
2 months ago
9ab87603
f2440132
+48
-23
1 changed file
expand all
collapse all
unified
split
app
api
inngest
functions
migrate_user_to_standard.ts
+48
-23
app/api/inngest/functions/migrate_user_to_standard.ts
···
128
128
}
129
129
}
130
130
131
131
-
// Step 4: Get and migrate documents for these publications
131
131
+
// Step 4: Get ALL user's pub.leaflet.document records (both in publications and standalone)
132
132
const oldDocuments = await step.run("fetch-old-documents", async () => {
133
133
-
const oldPubUris = Object.keys(publicationUriMap);
134
134
-
if (oldPubUris.length === 0) return [];
135
135
-
136
133
const { data, error } = await supabaseServerClient
137
137
-
.from("documents_in_publications")
138
138
-
.select("document, publication, documents(uri, data)")
139
139
-
.in("publication", oldPubUris);
134
134
+
.from("documents")
135
135
+
.select("uri, data")
136
136
+
.like("uri", `at://${did}/pub.leaflet.document/%`);
140
137
141
138
if (error) throw new Error(`Failed to fetch documents: ${error.message}`);
142
139
return data || [];
143
140
});
144
141
142
142
+
// Also fetch publication associations for documents
143
143
+
const documentPublicationMap = await step.run("fetch-document-publications", async () => {
144
144
+
const docUris = oldDocuments.map(d => d.uri);
145
145
+
if (docUris.length === 0) return {};
146
146
+
147
147
+
const { data, error } = await supabaseServerClient
148
148
+
.from("documents_in_publications")
149
149
+
.select("document, publication")
150
150
+
.in("document", docUris);
151
151
+
152
152
+
if (error) throw new Error(`Failed to fetch document publications: ${error.message}`);
153
153
+
154
154
+
// Create a map of document URI -> publication URI
155
155
+
const map: Record<string, string> = {};
156
156
+
for (const row of data || []) {
157
157
+
map[row.document] = row.publication;
158
158
+
}
159
159
+
return map;
160
160
+
});
161
161
+
145
162
const documentUriMap: Record<string, string> = {}; // old URI -> new URI
146
163
147
147
-
for (const docRow of oldDocuments) {
148
148
-
if (!docRow.documents) continue;
149
149
-
const doc = docRow.documents as { uri: string; data: Json };
164
164
+
for (const doc of oldDocuments) {
150
165
const aturi = new AtUri(doc.uri);
151
166
152
167
// Skip if already a site.standard.document
···
163
178
continue;
164
179
}
165
180
166
166
-
// Get the new publication URI
167
167
-
const newPubUri = publicationUriMap[docRow.publication];
168
168
-
if (!newPubUri) {
169
169
-
stats.errors.push(`Document ${doc.uri}: No migrated publication found`);
170
170
-
continue;
181
181
+
// Determine the site field:
182
182
+
// - If document is in a publication, use the new publication URI (if migrated) or old URI
183
183
+
// - If standalone, use the HTTPS URL format
184
184
+
const oldPubUri = documentPublicationMap[doc.uri];
185
185
+
let siteValue: string;
186
186
+
187
187
+
if (oldPubUri) {
188
188
+
// Document is in a publication - use new URI if migrated, otherwise keep old
189
189
+
siteValue = publicationUriMap[oldPubUri] || oldPubUri;
190
190
+
} else {
191
191
+
// Standalone document - use HTTPS URL format
192
192
+
siteValue = `https://leaflet.pub/p/${did}`;
171
193
}
172
194
173
195
// Build site.standard.document record
174
196
const newRecord: SiteStandardDocument.Record = {
175
197
$type: "site.standard.document",
176
198
title: normalized.title || "Untitled",
177
177
-
site: newPubUri,
199
199
+
site: siteValue,
178
200
publishedAt: normalized.publishedAt || new Date().toISOString(),
179
201
description: normalized.description,
180
202
content: normalized.content,
···
212
234
return { success: false as const, error: dbError.message };
213
235
}
214
236
215
215
-
// Add to documents_in_publications with new URIs
216
216
-
await supabaseServerClient
217
217
-
.from("documents_in_publications")
218
218
-
.upsert({
219
219
-
publication: newPubUri,
220
220
-
document: newUri,
221
221
-
});
237
237
+
// If document was in a publication, add to documents_in_publications with new URIs
238
238
+
if (oldPubUri) {
239
239
+
const newPubUri = publicationUriMap[oldPubUri] || oldPubUri;
240
240
+
await supabaseServerClient
241
241
+
.from("documents_in_publications")
242
242
+
.upsert({
243
243
+
publication: newPubUri,
244
244
+
document: newUri,
245
245
+
});
246
246
+
}
222
247
223
248
return { success: true as const };
224
249
});