-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EMS-3633): data migration - declarations (#2819)
* feat(EMS-3633): data migration - declaration versioning * feat(EMS-3633): fix typos, add missing documentation * feat(EMS-3633): add missing migration field removal * feat(EMS-3633): fix sonarcloud/promise issue, update README.md * feat(EMS-3633): fix sonarcloud issue
- Loading branch information
Showing
32 changed files
with
811 additions
and
57 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
48 changes: 48 additions & 0 deletions
48
.../version-1-to-version-2/create-new-application-relationships/declaration-version-field.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,48 @@ | ||
import { Connection } from 'mysql2/promise'; | ||
import getAllDeclarations from '../get-all-declarations'; | ||
import getAllDeclarationVersions from '../get-all-declaration-versions'; | ||
import executeSqlQuery from '../execute-sql-query'; | ||
|
||
/** | ||
* updateDeclarationVersionField | ||
* Update "version" columns in "declaration" entries | ||
* 1) Map over each "declaration". | ||
* 2) Generate "version" values (version ID relationship) | ||
* 3) Update the values in the Declaration table. | ||
* @param {Connection} connection: SQL database connection | ||
* @returns {Promise<Array<ApplicationPrivateMarket>>} Updated declarations | ||
*/ | ||
const updateDeclarationVersionField = async (connection: Connection) => { | ||
const loggingMessage = 'Creating declarationVersion entries with declaration relationships'; | ||
|
||
console.info(`✅ ${loggingMessage}`); | ||
|
||
try { | ||
const declarations = await getAllDeclarations(connection); | ||
const declarationVersions = await getAllDeclarationVersions(connection); | ||
|
||
const declarationPromises = declarations.map(async (declaration: object, index: number) => { | ||
const version = declarationVersions[index]; | ||
|
||
const query = ` | ||
UPDATE Declaration SET version='${version.id}' WHERE id='${declaration.id}' | ||
`; | ||
|
||
const updated = await executeSqlQuery({ | ||
connection, | ||
query, | ||
loggingMessage: `Updating version column in declartion table for declaration ${declaration.id}`, | ||
}); | ||
|
||
return updated; | ||
}); | ||
|
||
return Promise.all(declarationPromises); | ||
} catch (err) { | ||
console.error(`🚨 error ${loggingMessage} %O`, err); | ||
|
||
throw new Error(`🚨 error ${loggingMessage} ${err}`); | ||
} | ||
}; | ||
|
||
export default updateDeclarationVersionField; |
27 changes: 27 additions & 0 deletions
27
...-version-2/create-new-application-relationships/declaration-version-relationship/index.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,27 @@ | ||
import { Connection } from 'mysql2/promise'; | ||
import submittedApplications from './submitted-applications'; | ||
import nonSubmittedApplications from './non-submitted-applications'; | ||
|
||
/** | ||
* createDeclarationVersionRelationship | ||
* Create new "declaration version" entires with version numbers and "declaration" relationships. | ||
* @param {Connection} connection: SQL database connection | ||
* @returns {Promise<Array<DeclarationVersion>>} Declaration version entries | ||
*/ | ||
const createDeclarationVersionRelationship = async (connection: Connection) => { | ||
const loggingMessage = 'Creating declarationVersion entries with declaration relationships for non-submitted applications'; | ||
|
||
console.info(`✅ ${loggingMessage}`); | ||
|
||
try { | ||
const promises = Promise.all([await submittedApplications(connection), await nonSubmittedApplications(connection)]); | ||
|
||
return promises; | ||
} catch (err) { | ||
console.error(`🚨 error ${loggingMessage} %O`, err); | ||
|
||
throw new Error(`🚨 error ${loggingMessage} ${err}`); | ||
} | ||
}; | ||
|
||
export default createDeclarationVersionRelationship; |
67 changes: 67 additions & 0 deletions
67
...-application-relationships/declaration-version-relationship/non-submitted-applications.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,67 @@ | ||
import { Connection } from 'mysql2/promise'; | ||
import { DECLARATIONS } from '../../../../constants'; | ||
import getAllNonSubmittedApplications from '../../get-all-non-submitted-applications'; | ||
import createCuid from '../../create-cuid'; | ||
import executeSqlQuery from '../../execute-sql-query'; | ||
import { Application } from '../../../../types'; | ||
|
||
const { | ||
ANTI_BRIBERY, | ||
ANTI_BRIBERY_CODE_OF_CONDUCT, | ||
ANTI_BRIBERY_EXPORTING_WITH_CODE_OF_CONDUCT, | ||
CONFIDENTIALITY, | ||
CONFIRMATION_AND_ACKNOWLEDGEMENTS, | ||
} = DECLARATIONS.LATEST_DECLARATIONS; | ||
|
||
/** | ||
* createDeclarationVersionRelationship | ||
* Create new "declaration version" entires with version numbers and "declaration" relationships, | ||
* for applications that do NOT have a SUBMITTED status. | ||
* Applications that have NOT been submitted, prior to V2, should have the latest declaration versions. | ||
* 1) Map over each application | ||
* 2) Create new database values with a CUID and version numbers | ||
* 3) Add entries to the DeclarationVersion table | ||
* @param {Connection} connection: SQL database connection | ||
* @returns {Promise<Array<DeclarationVersion>>} Declaration version entries | ||
*/ | ||
const createDeclarationVersionRelationshipNotSubmittedApplications = async (connection: Connection) => { | ||
const loggingMessage = 'Creating declarationVersion entries with declaration relationships for non-submitted applications'; | ||
|
||
console.info(`✅ ${loggingMessage}`); | ||
|
||
const nonSubmittedApplications = await getAllNonSubmittedApplications(connection); | ||
|
||
if (!nonSubmittedApplications.length) { | ||
console.info('ℹ️ No non-submitted applications available - no need to create declarationVersion entries'); | ||
|
||
return false; | ||
} | ||
|
||
try { | ||
const declarationPromises = nonSubmittedApplications.map(async (application: Application) => { | ||
const { declaration: declarationId } = application; | ||
|
||
const idValues = `'${createCuid()}', '${declarationId}',`; | ||
|
||
const versionValues = `'${ANTI_BRIBERY}', '${CONFIDENTIALITY}', '${CONFIRMATION_AND_ACKNOWLEDGEMENTS}', '${ANTI_BRIBERY_CODE_OF_CONDUCT}', '${ANTI_BRIBERY_EXPORTING_WITH_CODE_OF_CONDUCT}'`; | ||
|
||
const theValues = `(${idValues} ${versionValues})`; | ||
|
||
const query = await executeSqlQuery({ | ||
connection, | ||
query: `INSERT INTO DeclarationVersion (id, declaration, agreeToAntiBribery, agreeToConfidentiality, agreeToConfirmationAndAcknowledgements, hasAntiBriberyCodeOfConduct, willExportWithAntiBriberyCodeOfConduct) VALUES ${theValues}`, | ||
loggingMessage: `Creating DeclarationVersion entry for application ${application.id}`, | ||
}); | ||
|
||
return query; | ||
}); | ||
|
||
return Promise.all(declarationPromises); | ||
} catch (err) { | ||
console.error(`🚨 error ${loggingMessage} %O`, err); | ||
|
||
throw new Error(`🚨 error ${loggingMessage} ${err}`); | ||
} | ||
}; | ||
|
||
export default createDeclarationVersionRelationshipNotSubmittedApplications; |
68 changes: 68 additions & 0 deletions
68
...-new-application-relationships/declaration-version-relationship/submitted-applications.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,68 @@ | ||
import { Connection } from 'mysql2/promise'; | ||
import { DECLARATIONS } from '../../../../constants'; | ||
import getAllSubmittedApplications from '../../get-all-submitted-applications'; | ||
import createCuid from '../../create-cuid'; | ||
import executeSqlQuery from '../../execute-sql-query'; | ||
import { Application } from '../../../../types'; | ||
|
||
const { | ||
ANTI_BRIBERY, | ||
ANTI_BRIBERY_CODE_OF_CONDUCT, | ||
ANTI_BRIBERY_EXPORTING_WITH_CODE_OF_CONDUCT, | ||
CONFIDENTIALITY, | ||
CONFIRMATION_AND_ACKNOWLEDGEMENTS, | ||
HOW_YOUR_DATA_WILL_BE_USED, | ||
} = DECLARATIONS.V1_DECLARATIONS; | ||
|
||
/** | ||
* createDeclarationVersionRelationshipSubmittedApplications | ||
* Create new "declaration version" entires with version numbers and "declaration" relationships, | ||
* for applications with a SUBMITTED status. | ||
* Applications that have already been submitted, prior to V2, should have V1 declaration versions. | ||
* 1) Map over each application | ||
* 2) Create new database values with a CUID and version numbers | ||
* 3) Add entries to the DeclarationVersion table | ||
* @param {Connection} connection: SQL database connection | ||
* @returns {Promise<Array<DeclarationVersion>>} Declaration version entries | ||
*/ | ||
const createDeclarationVersionRelationshipSubmittedApplications = async (connection: Connection) => { | ||
const loggingMessage = 'Creating declarationVersion entries with declaration relationships for submitted applications'; | ||
|
||
console.info(`✅ ${loggingMessage}`); | ||
|
||
const submittedApplications = await getAllSubmittedApplications(connection); | ||
|
||
if (!submittedApplications.length) { | ||
console.info('ℹ️ No submitted applications available - no need to create declarationVersion entries'); | ||
|
||
return false; | ||
} | ||
|
||
try { | ||
const declarationPromises = submittedApplications.map(async (application: Application) => { | ||
const { declaration: declarationId } = application; | ||
|
||
const idValues = `'${createCuid()}', '${declarationId}',`; | ||
|
||
const versionValues = `'${HOW_YOUR_DATA_WILL_BE_USED}', '${ANTI_BRIBERY}', '${CONFIDENTIALITY}', '${CONFIRMATION_AND_ACKNOWLEDGEMENTS}', '${ANTI_BRIBERY_CODE_OF_CONDUCT}', '${ANTI_BRIBERY_EXPORTING_WITH_CODE_OF_CONDUCT}'`; | ||
|
||
const theValues = `(${idValues} ${versionValues})`; | ||
|
||
const query = await executeSqlQuery({ | ||
connection, | ||
query: `INSERT INTO DeclarationVersion (id, declaration, agreeHowDataWillBeUsed, agreeToAntiBribery, agreeToConfidentiality, agreeToConfirmationAndAcknowledgements, hasAntiBriberyCodeOfConduct, willExportWithAntiBriberyCodeOfConduct) VALUES ${theValues}`, | ||
loggingMessage: `Creating DeclarationVersion entry for application ${application.id}`, | ||
}); | ||
|
||
return query; | ||
}); | ||
|
||
return Promise.all(declarationPromises); | ||
} catch (err) { | ||
console.error(`🚨 error ${loggingMessage} %O`, err); | ||
|
||
throw new Error(`🚨 error ${loggingMessage} ${err}`); | ||
} | ||
}; | ||
|
||
export default createDeclarationVersionRelationshipSubmittedApplications; |
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
34 changes: 34 additions & 0 deletions
34
...i/data-migration/version-1-to-version-2/create-tables/create-declaration-version-table.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,34 @@ | ||
import { Connection } from 'mysql2/promise'; | ||
import executeSqlQuery from '../execute-sql-query'; | ||
|
||
/** | ||
* createDeclarationVersionTable | ||
* Create new "declaration version" database table. | ||
* @param {Connection} connection: SQL database connection | ||
* @returns {Promise<Array<object>>} executeSqlQuery response | ||
*/ | ||
const createDeclarationVersionTable = (connection: Connection) => { | ||
const loggingMessage = 'Creating TABLE - declaration version'; | ||
|
||
const query = ` | ||
CREATE TABLE DeclarationVersion ( | ||
id varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL, | ||
agreeHowDataWillBeUsed varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', | ||
agreeToAntiBribery varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', | ||
agreeToConfidentiality varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', | ||
agreeToConfirmationAndAcknowledgements varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', | ||
hasAntiBriberyCodeOfConduct varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', | ||
willExportWithAntiBriberyCodeOfConduct varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', | ||
declaration varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL, | ||
PRIMARY KEY (id), | ||
KEY DeclarationVersion_declaration_idx (declaration), | ||
CONSTRAINT DeclarationVersion_declaration_fkey FOREIGN KEY (declaration) REFERENCES Declaration (id) ON DELETE | ||
SET | ||
NULL ON UPDATE CASCADE | ||
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci; | ||
`; | ||
|
||
return executeSqlQuery({ connection, query, loggingMessage }); | ||
}; | ||
|
||
export default createDeclarationVersionTable; |
Oops, something went wrong.