Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add safety checks on migration script #431

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions backend/core/src/migration/1695143897902-add-jurisdictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class addJurisdictions1695143897902 implements MigrationInterface {
const jurisValues = Object.values(newJuris)
await queryRunner.query(
`INSERT INTO jurisdictions (${jurisKeys})
VALUES (${jurisValues.map((_, index) => `$${index+1}`).toString()})`,
VALUES (${jurisValues.map((_, index) => `$${index + 1}`).toString()})`,
jurisValues
)
})
Expand All @@ -39,12 +39,14 @@ export class addJurisdictions1695143897902 implements MigrationInterface {
)

existingListings.forEach(async (listing) => {
const matchingJuris = existingJurisdictions.find((juris) => juris.name === listing.county).id
await queryRunner.query(
`UPDATE listings
SET jurisdiction_id='${matchingJuris}'
WHERE id='${listing.id}'`
)
const matchingJuris = existingJurisdictions.find((juris) => juris.name === listing.county)
if (matchingJuris) {
await queryRunner.query(
`UPDATE listings
SET jurisdiction_id='${matchingJuris.id}'
WHERE id='${listing.id}'`
)
}
})

const existingUsersAndRoles = await queryRunner.query(
Expand All @@ -66,24 +68,26 @@ export class addJurisdictions1695143897902 implements MigrationInterface {
)
}
})
// Add all jurisdictions tied to listings of partner accounts
// Add all jurisdictions tied to listings of partner accounts
} else if (user.is_partner) {
const userListings = await queryRunner.query(
`SELECT listings_id from listings_leasing_agents_user_accounts where user_accounts_id = '${user.user_id}'`
)
userListings.forEach(async (listing) => {
const matchingListing = existingListings.find(
(list) => list.id === listing.listings_id
const matchingListing = existingListings.find((list) => list.id === listing.listings_id)
const matchingJuris = existingJurisdictions.find(
(juris) => juris.name === matchingListing.county
)
const matchingJuris = existingJurisdictions.find((juris) => juris.name === matchingListing.county).id
if (matchingJuris) {
await queryRunner.query(
`INSERT INTO user_accounts_jurisdictions_jurisdictions
(user_accounts_id, jurisdictions_id)
VALUES ($1, $2)
ON CONFLICT (user_accounts_id, jurisdictions_id) DO NOTHING
`,
[user.user_id, matchingJuris]
[user.user_id, matchingJuris.id]
)
}
})
}
})
Expand Down
Loading