-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
services/app-api/src/dataMigrations/migrations/20231026124562_clean_empty_rates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { PrismaTransactionType } from '../../postgres/prismaTypes' | ||
|
||
export const migrateToDeleteContractOnlyRates = async ( | ||
client: PrismaTransactionType | ||
): Promise<Error | void> => { | ||
try { | ||
// find those empty submitted rates by their revision | ||
const badRateRevisions = await client.rateRevisionTable.findMany({ | ||
where: { | ||
rateType: undefined, | ||
submitInfo: { | ||
isNot: null, | ||
}, | ||
unlockInfo: { | ||
is: null, // has to be submitted revision that is not yet unlocked | ||
}, | ||
}, | ||
}) | ||
console.info( | ||
`Found ${badRateRevisions.length} rate revisions to delete` | ||
) | ||
|
||
await client.rateRevisionTable.deleteMany({ | ||
where: { | ||
id: { | ||
in: badRateRevisions.map((rev) => rev.id), | ||
}, | ||
}, | ||
}) | ||
|
||
// delete any empty rates | ||
await client.rateTable.deleteMany({ | ||
where: { | ||
revisions: { | ||
none: {}, | ||
}, | ||
}, | ||
}) | ||
} catch (error) { | ||
console.error(`Data migration ERROR: ${error}`) | ||
} | ||
} |