|
| 1 | +import { v } from 'convex/values' |
| 2 | +import { internal } from './_generated/api' |
| 3 | +import type { Doc, Id } from './_generated/dataModel' |
| 4 | +import { action, internalMutation, internalQuery } from './_generated/server' |
| 5 | +import { assertRole, requireUserFromAction } from './lib/access' |
| 6 | + |
| 7 | +const DEFAULT_BATCH_SIZE = 50 |
| 8 | +const MAX_BATCH_SIZE = 200 |
| 9 | +const SYNC_STATE_KEY = 'souls' |
| 10 | + |
| 11 | +type BackupPageItem = |
| 12 | + | { |
| 13 | + kind: 'ok' |
| 14 | + soulId: Id<'souls'> |
| 15 | + versionId: Id<'soulVersions'> |
| 16 | + slug: string |
| 17 | + displayName: string |
| 18 | + version: string |
| 19 | + ownerHandle: string |
| 20 | + files: Doc<'soulVersions'>['files'] |
| 21 | + publishedAt: number |
| 22 | + } |
| 23 | + | { kind: 'missingLatestVersion'; soulId: Id<'souls'> } |
| 24 | + | { kind: 'missingVersionDoc'; soulId: Id<'souls'>; versionId: Id<'soulVersions'> } |
| 25 | + | { kind: 'missingOwner'; soulId: Id<'souls'>; ownerUserId: Id<'users'> } |
| 26 | + |
| 27 | +type BackupPageResult = { |
| 28 | + items: BackupPageItem[] |
| 29 | + cursor: string | null |
| 30 | + isDone: boolean |
| 31 | +} |
| 32 | + |
| 33 | +type BackupSyncState = { |
| 34 | + cursor: string | null |
| 35 | +} |
| 36 | + |
| 37 | +export type SyncGitHubSoulBackupsResult = { |
| 38 | + stats: { |
| 39 | + soulsScanned: number |
| 40 | + soulsSkipped: number |
| 41 | + soulsBackedUp: number |
| 42 | + soulsMissingVersion: number |
| 43 | + soulsMissingOwner: number |
| 44 | + errors: number |
| 45 | + } |
| 46 | + cursor: string | null |
| 47 | + isDone: boolean |
| 48 | +} |
| 49 | + |
| 50 | +export const getGitHubSoulBackupPageInternal = internalQuery({ |
| 51 | + args: { |
| 52 | + cursor: v.optional(v.string()), |
| 53 | + batchSize: v.optional(v.number()), |
| 54 | + }, |
| 55 | + handler: async (ctx, args): Promise<BackupPageResult> => { |
| 56 | + const batchSize = clampInt(args.batchSize ?? DEFAULT_BATCH_SIZE, 1, MAX_BATCH_SIZE) |
| 57 | + const { page, isDone, continueCursor } = await ctx.db |
| 58 | + .query('souls') |
| 59 | + .order('asc') |
| 60 | + .paginate({ cursor: args.cursor ?? null, numItems: batchSize }) |
| 61 | + |
| 62 | + const items: BackupPageItem[] = [] |
| 63 | + for (const soul of page) { |
| 64 | + if (soul.softDeletedAt) continue |
| 65 | + if (!soul.latestVersionId) { |
| 66 | + items.push({ kind: 'missingLatestVersion', soulId: soul._id }) |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + const version = await ctx.db.get(soul.latestVersionId) |
| 71 | + if (!version) { |
| 72 | + items.push({ |
| 73 | + kind: 'missingVersionDoc', |
| 74 | + soulId: soul._id, |
| 75 | + versionId: soul.latestVersionId, |
| 76 | + }) |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + const owner = await ctx.db.get(soul.ownerUserId) |
| 81 | + if (!owner || owner.deletedAt) { |
| 82 | + items.push({ kind: 'missingOwner', soulId: soul._id, ownerUserId: soul.ownerUserId }) |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + items.push({ |
| 87 | + kind: 'ok', |
| 88 | + soulId: soul._id, |
| 89 | + versionId: version._id, |
| 90 | + slug: soul.slug, |
| 91 | + displayName: soul.displayName, |
| 92 | + version: version.version, |
| 93 | + ownerHandle: owner.handle ?? owner._id, |
| 94 | + files: version.files, |
| 95 | + publishedAt: version.createdAt, |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + return { items, cursor: continueCursor, isDone } |
| 100 | + }, |
| 101 | +}) |
| 102 | + |
| 103 | +export const getGitHubSoulBackupSyncStateInternal = internalQuery({ |
| 104 | + args: {}, |
| 105 | + handler: async (ctx): Promise<BackupSyncState> => { |
| 106 | + const state = await ctx.db |
| 107 | + .query('githubBackupSyncState') |
| 108 | + .withIndex('by_key', (q) => q.eq('key', SYNC_STATE_KEY)) |
| 109 | + .unique() |
| 110 | + return { cursor: state?.cursor ?? null } |
| 111 | + }, |
| 112 | +}) |
| 113 | + |
| 114 | +export const setGitHubSoulBackupSyncStateInternal = internalMutation({ |
| 115 | + args: { |
| 116 | + cursor: v.optional(v.string()), |
| 117 | + }, |
| 118 | + handler: async (ctx, args) => { |
| 119 | + const now = Date.now() |
| 120 | + const state = await ctx.db |
| 121 | + .query('githubBackupSyncState') |
| 122 | + .withIndex('by_key', (q) => q.eq('key', SYNC_STATE_KEY)) |
| 123 | + .unique() |
| 124 | + |
| 125 | + if (!state) { |
| 126 | + await ctx.db.insert('githubBackupSyncState', { |
| 127 | + key: SYNC_STATE_KEY, |
| 128 | + cursor: args.cursor, |
| 129 | + updatedAt: now, |
| 130 | + }) |
| 131 | + return { ok: true as const } |
| 132 | + } |
| 133 | + |
| 134 | + await ctx.db.patch(state._id, { |
| 135 | + cursor: args.cursor, |
| 136 | + updatedAt: now, |
| 137 | + }) |
| 138 | + |
| 139 | + return { ok: true as const } |
| 140 | + }, |
| 141 | +}) |
| 142 | + |
| 143 | +export const syncGitHubSoulBackups: ReturnType<typeof action> = action({ |
| 144 | + args: { |
| 145 | + dryRun: v.optional(v.boolean()), |
| 146 | + batchSize: v.optional(v.number()), |
| 147 | + maxBatches: v.optional(v.number()), |
| 148 | + resetCursor: v.optional(v.boolean()), |
| 149 | + }, |
| 150 | + handler: async (ctx, args): Promise<SyncGitHubSoulBackupsResult> => { |
| 151 | + const { user } = await requireUserFromAction(ctx) |
| 152 | + assertRole(user, ['admin']) |
| 153 | + |
| 154 | + if (args.resetCursor && !args.dryRun) { |
| 155 | + await ctx.runMutation(internal.githubSoulBackups.setGitHubSoulBackupSyncStateInternal, { |
| 156 | + cursor: undefined, |
| 157 | + }) |
| 158 | + } |
| 159 | + |
| 160 | + return ctx.runAction(internal.githubSoulBackupsNode.syncGitHubSoulBackupsInternal, { |
| 161 | + dryRun: args.dryRun, |
| 162 | + batchSize: args.batchSize, |
| 163 | + maxBatches: args.maxBatches, |
| 164 | + }) as Promise<SyncGitHubSoulBackupsResult> |
| 165 | + }, |
| 166 | +}) |
| 167 | + |
| 168 | +function clampInt(value: number, min: number, max: number) { |
| 169 | + return Math.max(min, Math.min(max, Math.floor(value))) |
| 170 | +} |
0 commit comments