-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify db-uniqueness constraint for ongoingDeliveryId
- Loading branch information
1 parent
9b89c59
commit 6c37759
Showing
5 changed files
with
100 additions
and
82 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
packages/attestation-service/migrations/20211109200901-attestation-v8.js
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,68 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
const transaction = await queryInterface.sequelize.transaction() | ||
|
||
try { | ||
await queryInterface.removeIndex( | ||
'Attestations', | ||
['ongoingDeliveryId'], | ||
{ fields: ['ongoingDeliveryId'], unique: true }, | ||
{ transaction } | ||
) | ||
await queryInterface.addIndex( | ||
'Attestations', | ||
['ongoingDeliveryId'], | ||
{ fields: ['ongoingDeliveryId'], unique: false }, | ||
{ transaction } | ||
) | ||
await transaction.commit() | ||
} catch (error) { | ||
await transaction.rollback() | ||
throw error | ||
} | ||
}, | ||
down: async (queryInterface, Sequelize) => { | ||
const transaction = await queryInterface.sequelize.transaction() | ||
try { | ||
await queryInterface.removeIndex( | ||
'Attestations', | ||
['ongoingDeliveryId'], | ||
{ fields: ['ongoingDeliveryId'], unique: false }, | ||
{ transaction } | ||
) | ||
await queryInterface.addIndex( | ||
'Attestations', | ||
['ongoingDeliveryId'], | ||
{ fields: ['ongoingDeliveryId'], unique: true }, | ||
{ transaction } | ||
) | ||
} catch (error) { | ||
await transaction.rollback() | ||
if ( | ||
error.errors.length == 1 && | ||
error.errors[0].path == 'ongoingDeliveryId' && | ||
error.errors[0].type == 'unique violation' | ||
) { | ||
try { | ||
console.warn('Duplicates in ongoingDeliveryId; nulling out column and retrying.') | ||
await queryInterface.removeColumn('Attestations', 'ongoingDeliveryId') | ||
await queryInterface.addColumn('Attestations', 'ongoingDeliveryId', { | ||
type: Sequelize.STRING, | ||
allowNull: true, | ||
}) | ||
await queryInterface.addIndex( | ||
'Attestations', | ||
['ongoingDeliveryId'], | ||
{ fields: ['ongoingDeliveryId'], unique: true }, | ||
{ transaction } | ||
) | ||
} catch (error) { | ||
await transaction.rollback() | ||
throw error | ||
} | ||
} | ||
} | ||
}, | ||
} |
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
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
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
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