From 3b2e3919090fbdbc8f5decc9401caababfd5af1d Mon Sep 17 00:00:00 2001 From: Joe Karow <58997957+JoeKarow@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:39:06 -0500 Subject: [PATCH] schema updates --- .../db/generated/attributeSupplementSchema.ts | 5 - .../index.ts | 148 ++++++++++++++++++ packages/db/prisma/data-migrations/index.ts | 1 + 3 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 packages/db/prisma/data-migrations/2024-02-14_attribute-supplement-schemas/index.ts diff --git a/packages/db/generated/attributeSupplementSchema.ts b/packages/db/generated/attributeSupplementSchema.ts index fc6480b32a..0f13462e24 100644 --- a/packages/db/generated/attributeSupplementSchema.ts +++ b/packages/db/generated/attributeSupplementSchema.ts @@ -6,15 +6,10 @@ export const attributeSupplementSchema = { access_value: z.union([z.string(), z.null()]).optional(), instructions: z.string(), }), - currency: z.any(), - incompatible: z.any(), incompatibleData: z.array(z.record(z.any())), number: z.object({ num: z.number() }), - 'num-max': z.any(), numMax: z.object({ max: z.number() }), - 'num-min': z.any(), numMin: z.object({ min: z.number() }), - 'num-min-max': z.any(), numMinMaxOrRange: z.union([ z.object({ min: z.number() }), z.object({ max: z.number() }), diff --git a/packages/db/prisma/data-migrations/2024-02-14_attribute-supplement-schemas/index.ts b/packages/db/prisma/data-migrations/2024-02-14_attribute-supplement-schemas/index.ts new file mode 100644 index 0000000000..e06e0a6d6d --- /dev/null +++ b/packages/db/prisma/data-migrations/2024-02-14_attribute-supplement-schemas/index.ts @@ -0,0 +1,148 @@ +import { prisma, type Prisma } from '~db/client' +import { formatMessage } from '~db/prisma/common' +import { type MigrationJob } from '~db/prisma/dataMigrationRunner' +import { createLogger, type JobDef, jobPostRunner } from '~db/prisma/jobPreRun' +import { type FieldAttributes, FieldType } from '~db/zod_util/attributeSupplement' + +/** Define the job metadata here. */ +const jobDef: JobDef = { + jobId: '2024-02-14_attribute-supplement-schemas', + title: 'attribute supplement schemas', + createdBy: 'Joe Karow', + /** Optional: Longer description for the job */ + description: undefined, +} +/** + * Job export - this variable MUST be UNIQUE + */ +export const job20240214_attribute_supplement_schemas = { + title: `[${jobDef.jobId}] ${jobDef.title}`, + task: async (_ctx, task) => { + /** Create logging instance */ + createLogger(task, jobDef.jobId) + const log = (...args: Parameters) => (task.output = formatMessage(...args)) + /** + * Start defining your data migration from here. + * + * To log output, use `task.output = 'Message to log'` + * + * This will be written to `stdout` and to a log file in `/prisma/migration-logs/` + */ + + // Do stuff + const deleted = await prisma.attributeSupplementDataSchema.deleteMany({ + where: { + id: { + in: [ + 'asds_01GW2HHH9NFEXHG9RHBTM9NRFR', + 'asds_01GW2HHH9PKJ6H9WFSNZSVK2G4', + 'asds_01GW2HHH9P7J5A1CBGN6B5QCG7', + 'asds_01GW2HHH9PN6MJ4ZS7D17G1YTK', + 'asds_01GW2HHH9PSSYV7TKFA6DY68P4', + ], + }, + }, + }) + + log(`Deleted ${deleted.count} records.`) + + const updateData: SchemaUpdate[] = [ + { + data: { + definition: [ + { key: 'min', label: 'Min', name: 'min', type: FieldType.number }, + { key: 'max', label: 'Max', name: 'max', type: FieldType.number }, + ], + // tag: 'numMinMaxOrRange', + }, + where: { + id: 'asds_01GYX872BWWCGTZREHDT2AFF9D', + }, + }, + { + data: { + definition: [ + { key: 'min', label: 'Min', name: 'min', type: FieldType.number }, + { key: 'max', label: 'Max', name: 'max', type: FieldType.number }, + ], + // tag: 'numRange', + }, + where: { + id: 'asds_01GYX872BYZQ6CC344S1SWTJ97', + }, + }, + { + data: { + definition: { key: 'min', label: 'Min', name: 'min', type: FieldType.number }, + // tag: 'numMin', + }, + where: { + id: 'asds_01GYX872BZE4TN1MJHMTGVAYZ0', + }, + }, + { + data: { + definition: { key: 'max', label: 'Max', name: 'max', type: FieldType.number }, + // tag: 'numMax', + }, + where: { + id: 'asds_01GYX872BZNT0F6WH50XJQWM9G', + }, + }, + { + data: { + definition: { key: 'num', label: 'Number', name: 'num', type: FieldType.number }, + // tag: 'number', + }, + where: { + id: 'asds_01GYX872BZKJPVH6VHC0ABFH8A', + }, + }, + { + data: { + definition: { + key: 'incompatible', + label: 'Incompatible', + name: 'incompatible', + type: FieldType.text, + }, + // tag: 'incompatibleData', + }, + where: { + id: 'asds_01GYX872BZSMTHYM4HYYTCENZM', + }, + }, + { + data: { + definition: { key: 'other', label: 'Other', name: 'other', type: FieldType.text }, + // tag: 'otherDescribe', + }, + where: { + id: 'asds_01GYX872BZ7V6VQ3NE6KSVVRKH', + }, + }, + ] + const updates = await prisma.$transaction( + updateData.map((args) => + prisma.attributeSupplementDataSchema.update( + args as unknown as Prisma.AttributeSupplementDataSchemaUpdateArgs + ) + ) + ) + log(`Updated ${updates.length} records.`) + /** + * DO NOT REMOVE BELOW + * + * This writes a record to the DB to register that this migration has run successfully. + */ + // await jobPostRunner(jobDef) + }, + def: jobDef, +} satisfies MigrationJob + +type SchemaUpdate = { + where: { id: string } + data: { + definition: FieldAttributes | FieldAttributes[] + } +} diff --git a/packages/db/prisma/data-migrations/index.ts b/packages/db/prisma/data-migrations/index.ts index 31638c79d7..f16237b5e7 100644 --- a/packages/db/prisma/data-migrations/index.ts +++ b/packages/db/prisma/data-migrations/index.ts @@ -3,4 +3,5 @@ export * from './2024-01-31_fix-attr-supp-json/index' export * from './2024-01-31_target-population-attrib' export * from './2024-02-01_add-missing-attributes/index' export * from './2024-02-02_deactivate-incompatible-attribs' +export * from './2024-02-14_attribute-supplement-schemas/index' // codegen:end