From e82245a6d31b4bedd7ef6da657a3eb9a902e6abe Mon Sep 17 00:00:00 2001 From: Joe Karow <58997957+JoeKarow@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:34:10 -0400 Subject: [PATCH] hide location addresses --- .../2024-03-11_hide-locations.ts | 62 +++++++++++++++++++ packages/db/prisma/data-migrations/index.ts | 1 + 2 files changed, 63 insertions(+) create mode 100644 packages/db/prisma/data-migrations/2024-03-11_hide-locations.ts diff --git a/packages/db/prisma/data-migrations/2024-03-11_hide-locations.ts b/packages/db/prisma/data-migrations/2024-03-11_hide-locations.ts new file mode 100644 index 0000000000..c2be004bdd --- /dev/null +++ b/packages/db/prisma/data-migrations/2024-03-11_hide-locations.ts @@ -0,0 +1,62 @@ +import { z } from 'zod' + +import { prisma } from '~db/client' +import { downloadFromDatastore, formatMessage } from '~db/prisma/common' +import { type MigrationJob } from '~db/prisma/dataMigrationRunner' +import { createLogger, type JobDef, jobPostRunner } from '~db/prisma/jobPreRun' +/** Define the job metadata here. */ +const jobDef: JobDef = { + jobId: '2024-03-11_hide-locations', + title: 'hide locations', + createdBy: 'Joe Karow', + /** Optional: Longer description for the job */ + description: undefined, +} + +const DeactivateIdsSchema = z.array(z.string()) +/** + * Job export - this variable MUST be UNIQUE + */ +export const job20240311_hide_locations = { + 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 + + log(`Downloading locations to hide from datastore`) + const hideLocationSlugs = await downloadFromDatastore( + 'migrations/2024-03-11_hide-locations/hideLocations.json', + log + ).then((data) => DeactivateIdsSchema.parse(data)) + const hideLocations = await prisma.orgLocation.updateMany({ + where: { + organization: { + slug: { in: hideLocationSlugs }, + }, + }, + data: { + notVisitable: true, + }, + }) + + log(`Updated ${hideLocations.count} location 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 diff --git a/packages/db/prisma/data-migrations/index.ts b/packages/db/prisma/data-migrations/index.ts index 715362aa79..a8ea5bfd0b 100644 --- a/packages/db/prisma/data-migrations/index.ts +++ b/packages/db/prisma/data-migrations/index.ts @@ -7,4 +7,5 @@ export * from './2024-02-19_attach-orphan-text' export * from './2024-02-20_appsheet-load/index' export * from './2024-02-23_add-missing-website' export * from './2024-03-08_update-alerts-and-org-urls/index' +export * from './2024-03-11_hide-locations' // codegen:end