Skip to content

Commit

Permalink
migration working
Browse files Browse the repository at this point in the history
  • Loading branch information
macrael committed Apr 25, 2024
1 parent ce0515f commit 6da507c
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import type { PrismaTransactionType } from "../../postgres/prismaTypes"
import type { PrismaTransactionType } from '../../postgres/prismaTypes'

export async function migrate(
client: PrismaTransactionType,
contractIDs?: string[]
): Promise<Error | undefined> {

// get all the old contract x rate relationships and splat them into the new tables
try {
const contracts = await client.contractTable.findMany({
where:
contractIDs
where: contractIDs
? {
id: { in: contractIDs },
}
Expand Down Expand Up @@ -43,65 +41,69 @@ export async function migrate(
},
})


const unmigrateRevisions = []
for (const contract of contracts) {
for (const contractRev of contract.revisions) {

if (contractRev.relatedSubmisions.length > 0) {
continue
}
unmigrateRevisions.push(contractRev)
}
}

console.log('found Unmigrated Revs: ', unmigrateRevisions.length)


for (const contractRev of unmigrateRevisions) {
const allLinkedRates = contractRev.rateRevisions

const contractSubmissionTime = contractRev.submitInfo?.updatedAt

if (contractSubmissionTime) {
// we're in a submitted contract rev
console.log('rateLinkTimes', contractRev.rateRevisions.map(rr => rr.validAfter))

let firstRateLinkTime: Date | undefined = undefined
const concurrentlySubmittedRateLinks = allLinkedRates.filter((linkedRate) => {
if (linkedRate.isRemoval) {
return false
}

const rateSubmissionTime = linkedRate.validAfter

if (!firstRateLinkTime) {
firstRateLinkTime = rateSubmissionTime
return true
}
const concurrentlySubmittedRateLinks = allLinkedRates
.filter((linkedRate) => {
if (linkedRate.isRemoval) {
return false
}

console.log('diff', Math.abs(rateSubmissionTime.getTime() - firstRateLinkTime.getTime()))
const rateSubmissionTime = linkedRate.validAfter

if (!firstRateLinkTime) {
firstRateLinkTime = rateSubmissionTime
return true
}

if (Math.abs(rateSubmissionTime.getTime() - firstRateLinkTime.getTime()) < 1000) {
return true
} else {
return false
}
}).map(rr => rr.rateRevision)
if (
Math.abs(
rateSubmissionTime.getTime() -
firstRateLinkTime.getTime()
) < 1000
) {
return true
} else {
return false
}
})
.map((rr) => rr.rateRevision)

// we need to sort by rate created at to make this right, something was wrong with the hack, shocker
concurrentlySubmittedRateLinks.sort((a, b) => a.rate.createdAt.getTime() - b.rate.createdAt.getTime())

console.log('filtered out related rates', contractRev.contractID, concurrentlySubmittedRateLinks.length, allLinkedRates.length - concurrentlySubmittedRateLinks.length)

const concurrentRateIDs = concurrentlySubmittedRateLinks.map((r)=> r.rateID)
const rateIDSet: {[id: string]: boolean} = {}
concurrentlySubmittedRateLinks.sort(
(a, b) =>
a.rate.createdAt.getTime() - b.rate.createdAt.getTime()
)

const concurrentRateIDs = concurrentlySubmittedRateLinks.map(
(r) => r.rateID
)
const rateIDSet: { [id: string]: boolean } = {}
for (const rID of concurrentRateIDs) {
rateIDSet[rID] = true
}
if (concurrentRateIDs.length != Object.keys(rateIDSet).length) {
console.error('found a collision!', contractRev.id, concurrentRateIDs)
console.error(
'found a collision!',
contractRev.id,
concurrentRateIDs
)
throw new Error('found a collision')
}

Expand All @@ -111,30 +113,26 @@ export async function migrate(
}

// add contract+submitinfo to related contract submissions
const submittedRev = await client.contractRevisionTable.update({
await client.contractRevisionTable.update({
where: {
id: contractRev.id
},
id: contractRev.id,
},
data: {
relatedSubmisions: {
connect: {
id: contractSubmissionID
}
}
id: contractSubmissionID,
},
},
},
include: {
relatedSubmisions: true,
}
},
})

console.log('updateds sumitter ev', submittedRev)

let ratePosition = 1
for (const rateRev of concurrentlySubmittedRateLinks) {
const oldSubmitInfoID = rateRev.submitInfo?.id


console.log('setting rate to contract sub id: ', contractSubmissionID, contractRev.id, ratePosition)
// set the submitInfo on each rate to be the same ID as the contract’s
// add rates+submitInfo to related rate submissions
// add join entries with rate position for contract + update info + rate rev.
Expand All @@ -146,24 +144,25 @@ export async function migrate(
submitInfoID: contractSubmissionID,
relatedSubmissions: {
connect: {
id: contractSubmissionID
}
id: contractSubmissionID,
},
},
submissionPackages: {
create: {
submissionID: contractSubmissionID,
contractRevisionID: contractRev.id,
ratePosition: ratePosition,
}
}
}
},
},
},
})

if (!oldSubmitInfoID) {
throw new Error('Better have an old submit id, we had one above')
throw new Error(
'Better have an old submit id, we had one above'
)
}
if (oldSubmitInfoID !== contractSubmissionID) {
console.log('deleting', oldSubmitInfoID, rateRev.id, contractRev.id, contractRev.contractID)
// ignore updateInfos we've already deleted
await client.updateInfoTable.deleteMany({
where: {
Expand All @@ -172,11 +171,10 @@ export async function migrate(
})
}

ratePosition ++
ratePosition++
}
}
}

} catch (err) {
console.error('Prisma Error: ', err)
return err
Expand Down
Loading

0 comments on commit 6da507c

Please sign in to comment.