Skip to content

Commit 6771de6

Browse files
committed
Add v3 docs schema migration
1 parent b1ebb40 commit 6771de6

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ yarn-error.log
77
.DS_Store
88
FIXME_*
99
*.log
10+
/secrets/

scripts/migrations/migrateV3.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { db, JSDocFunction, JSDocPage, MarkdownPage } from '@date-fns/docs/db'
2+
import * as admin from 'firebase-admin'
3+
import { last } from 'lodash'
4+
import { StringifiedJSON } from 'typeroo/json'
5+
import { schema, Typesaurus } from 'typesaurus'
6+
7+
admin.initializeApp()
8+
9+
type Page =
10+
| MarkdownPage
11+
| (Omit<JSDocPage, 'doc'> & {
12+
doc: { json: string }
13+
})
14+
15+
const oldDb = schema(($) => ({
16+
pages: $.collection<Page>(),
17+
}))
18+
19+
type OldSchema = Typesaurus.Schema<typeof oldDb>
20+
21+
const maxPageSize = 100
22+
23+
processAll((pages) =>
24+
Promise.all(
25+
pages.map((page) => {
26+
if (page.data.type === 'markdown') return
27+
return db.pages.update(page.ref.id, {
28+
doc: page.data.doc.json as StringifiedJSON<JSDocFunction>,
29+
})
30+
})
31+
)
32+
)
33+
.then(() => console.log('Done'))
34+
.catch((error) => {
35+
console.error(error)
36+
process.exit(1)
37+
})
38+
39+
type ChunkCallback = (chunk: OldSchema['pages']['Doc'][]) => unknown
40+
41+
async function processAll(
42+
cb: ChunkCallback,
43+
cursor?: OldSchema['pages']['Doc']
44+
) {
45+
const { pageSize, nextCursor } = await processChunk(cb, cursor)
46+
if (nextCursor && pageSize === maxPageSize) await processAll(cb, nextCursor)
47+
}
48+
49+
async function processChunk(
50+
cb: ChunkCallback,
51+
cursor?: OldSchema['pages']['Doc']
52+
) {
53+
const chunk = await oldDb.pages.query(($) => [
54+
$.field($.docId()).order('asc', cursor && $.startAfter(cursor)),
55+
$.limit(maxPageSize),
56+
])
57+
await cb(chunk)
58+
return { pageSize: chunk.length, nextCursor: last(chunk) }
59+
}

src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const config: { [k: string]: Config } = {
3333
development: {
3434
jobsURL: 'https://jobs.date-fns.org',
3535
firebaseApp: {
36-
apiKey: 'AIzaSyBoDBiIbKeiu4-Uz4JzqH3X7pwbop2PfpU',
37-
projectId: 'date-fns-org',
36+
apiKey: 'AIzaSyArPabWWebnLWhEgITZbLjTA6I_BaDmF0E',
37+
projectId: 'date-fns-org-staging',
3838
},
3939
apiURL: '/api',
4040
},

0 commit comments

Comments
 (0)