Skip to content

Commit b3d2fea

Browse files
authored
Merge pull request #38 from opencrvs/multiple-registrations-and-skip
Fix registration number and add skip functionality
2 parents 59170ba + 9964538 commit b3d2fea

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

.github/workflows/migrate-prod.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ on:
2020
required: true
2121
type: string
2222
default: 'farajaland.opencrvs.org'
23+
skip:
24+
description: 'Number of records to skip'
25+
required: false
26+
type: number
27+
default: 0
2328

2429
jobs:
2530
migrate:
@@ -53,6 +58,7 @@ jobs:
5358
env:
5459
OPENCRVS_EVENT: ${{ github.event.inputs.event }}
5560
OPENCRVS_DOMAIN: ${{ github.event.inputs.domain }}
61+
RECORD_SKIP: ${{ github.event.inputs.skip }}
5662
OPENCRVS_CLIENT_ID: ${{ secrets.OPENCRVS_CLIENT_ID }}
5763
OPENCRVS_CLIENT_SECRET: ${{ secrets.OPENCRVS_CLIENT_SECRET }}
5864
run: |

v1-to-v2-data-migration/helpers/transform.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,33 @@ function postProcess(
525525

526526
const approvedCorrections = []
527527

528-
const rev = document.actions.slice().reverse()
528+
let rev = document.actions.slice().reverse()
529+
let firstRegisterAction = null
530+
const actionsToRemove: string[] = []
529531

530532
for (const action of rev) {
533+
if (action.type === 'REGISTER') {
534+
if (action.registrationNumber) {
535+
if (firstRegisterAction) {
536+
console.warn(
537+
`Multiple REGISTER actions found for document ${document.id}`,
538+
)
539+
action.registrationNumber = undefined
540+
action.status = 'Requested'
541+
542+
firstRegisterAction.originalActionId = action.id
543+
} else {
544+
firstRegisterAction = action
545+
}
546+
} else {
547+
if (!firstRegisterAction || firstRegisterAction.originalActionId) {
548+
actionsToRemove.push(action.id)
549+
} else {
550+
firstRegisterAction.originalActionId = action.id
551+
}
552+
}
553+
}
554+
531555
if (action.type === 'APPROVE_CORRECTION') {
532556
approvedCorrections.push(action.requestId)
533557
}
@@ -555,6 +579,10 @@ function postProcess(
555579
}
556580
}
557581

582+
if (actionsToRemove.length > 0) {
583+
rev = rev.filter((action) => !actionsToRemove.includes(action.id))
584+
}
585+
558586
document.actions = rev.reverse()
559587

560588
return document

v1-to-v2-data-migration/helpers/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export interface Action {
265265
registrationNumber?: string
266266
assignedTo?: string
267267
requestId?: string
268+
originalActionId?: string
268269
}
269270

270271
// Document output types

v1-to-v2-data-migration/helpers/vars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-nocheck - Using Deno-specific environment variables
22
export const DOMAIN = Deno?.env?.get('OPENCRVS_DOMAIN') || 'localhost'
33
export const EVENT = Deno?.env?.get('OPENCRVS_EVENT') || 'birth'
4+
export const RECORD_SKIP = Number(Deno?.env?.get('RECORD_SKIP')) || 0
45
export const CLIENT_ID = Deno?.env?.get('OPENCRVS_CLIENT_ID')
56
export const CLIENT_SECRET = Deno?.env?.get('OPENCRVS_CLIENT_SECRET')
67
export const ADMIN_USERNAME =

v1-to-v2-data-migration/migrate.ipynb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@
192192
"} from './helpers/gqlHandlers.ts'\n",
193193
"import { transform } from './helpers/transform.ts'\n",
194194
"import { batch, getIndexErrors } from './helpers/utils.ts'\n",
195-
"\n",
196-
"const saved = []\n",
195+
"import { RECORD_SKIP } from './helpers/vars.ts'\n",
197196
"\n",
198197
"const migrateBirth = async (entryIds) => {\n",
199198
" const transformed = []\n",
@@ -231,12 +230,16 @@
231230
"}\n",
232231
"\n",
233232
"if (EVENT === 'birth') {\n",
233+
" const skippedPages = RECORD_SKIP ? Math.floor(RECORD_SKIP / 1000) : 0\n",
234234
" const pageSize = 1000\n",
235235
" const batchSize = 100\n",
236236
" let itemsRemaining = 0\n",
237-
" let page = 1\n",
238-
" let totalProcessed = 0\n",
237+
" let page = 1 + skippedPages\n",
238+
" let totalProcessed = skippedPages * pageSize\n",
239239
"\n",
240+
" if (RECORD_SKIP) {\n",
241+
" console.log(`Skipping first ${skippedPages * pageSize} records`)\n",
242+
" }\n",
240243
" do {\n",
241244
" const birthRegistrations = await fetchAllBirthRegistrations(\n",
242245
" sysToken,\n",
@@ -250,6 +253,7 @@
250253
"\n",
251254
" const { results, totalItems } = birthRegistrations.data.searchEvents\n",
252255
" const birthIds = results.map((x) => x.id)\n",
256+
"\n",
253257
" console.log(\n",
254258
" `Processing next page of ${birthIds.length} of ${totalItems} total records`\n",
255259
" )\n",
@@ -340,11 +344,16 @@
340344
"}\n",
341345
"\n",
342346
"if (EVENT === 'death') {\n",
347+
" const skippedPages = RECORD_SKIP ? Math.floor(RECORD_SKIP / 1000) : 0\n",
343348
" const pageSize = 1000\n",
344349
" const batchSize = 100\n",
345350
" let itemsRemaining = 0\n",
346-
" let page = 1\n",
347-
" let totalProcessed = 0\n",
351+
" let page = 1 + skippedPages\n",
352+
" let totalProcessed = skippedPages * pageSize\n",
353+
"\n",
354+
" if (RECORD_SKIP) {\n",
355+
" console.log(`Skipping first ${skippedPages * pageSize} records`)\n",
356+
" }\n",
348357
"\n",
349358
" do {\n",
350359
" const deathRegistrations = await fetchAllDeathRegistrations(\n",

0 commit comments

Comments
 (0)