Skip to content

Commit

Permalink
fix: units fts
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Aug 9, 2022
1 parent 9dd66f9 commit 227bc72
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 0 deletions.
227 changes: 227 additions & 0 deletions src/database/migrations/20220808192709-populate-units-fts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
'use strict';

export default {
async up(queryInterface) {
if (queryInterface.sequelize.getDialect() === 'sqlite') {
await queryInterface.dropTable('units_fts');
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE units_fts USING fts5(
warehouseUnitId,
issuanceId,
projectLocationId,
orgUid,
unitOwner,
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
vintageYear,
unitType,
marketplace,
marketplaceLink,
marketplaceIdentifier,
unitTags,
unitStatus,
unitStatusReason,
unitRegistryLink,
correspondingAdjustmentDeclaration,
correspondingAdjustmentStatus,
unitBlockStart,
unitBlockEnd,
unitCount,
timeStaged
);
`);
await queryInterface.sequelize.query(
`INSERT INTO units_fts SELECT
warehouseUnitId,
issuanceId,
projectLocationId,
orgUid,
unitOwner,
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
vintageYear,
unitType,
marketplace,
marketplaceLink,
marketplaceIdentifier,
unitTags,
unitStatus,
unitStatusReason,
unitRegistryLink,
correspondingAdjustmentDeclaration,
correspondingAdjustmentStatus,
unitBlockStart,
unitBlockEnd,
unitCount,
timeStaged
FROM units`,
);

await queryInterface.sequelize.query(`
CREATE TRIGGER unit_insert_fts AFTER INSERT ON units BEGIN
INSERT INTO units_fts(
warehouseUnitId,
issuanceId,
projectLocationId,
orgUid,
unitOwner,
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
vintageYear,
unitType,
marketplace,
marketplaceLink,
marketplaceIdentifier,
unitTags,
unitStatus,
unitStatusReason,
unitRegistryLink,
correspondingAdjustmentDeclaration,
correspondingAdjustmentStatus,
unitBlockStart,
unitBlockEnd,
unitCount
) VALUES (
new.warehouseUnitId,
new.issuanceId,
new.projectLocationId,
new.orgUid,
new.unitOwner,
new.countryJurisdictionOfOwner,
new.inCountryJurisdictionOfOwner,
new.serialNumberBlock,
new.vintageYear,
new.unitType,
new.marketplace,
new.marketplaceLink,
new.marketplaceIdentifier,
new.unitTags,
new.unitStatus,
new.unitStatusReason,
new.unitRegistryLink,
new.correspondingAdjustmentDeclaration,
new.correspondingAdjustmentStatus,
new.unitBlockStart,
new.unitBlockEnd,
new.unitCount
);
END;`);

await queryInterface.sequelize.query(`
CREATE TRIGGER unit_delete_fts AFTER DELETE ON units BEGIN
DELETE FROM units_fts WHERE warehouseUnitId = old.warehouseUnitId;
END;
`);

await queryInterface.sequelize.query(`
CREATE TRIGGER unit_update_fts AFTER UPDATE ON units BEGIN
DELETE FROM units_fts WHERE warehouseUnitId = old.warehouseUnitId;
INSERT INTO units_fts(
warehouseUnitId,
issuanceId,
projectLocationId,
orgUid,
unitOwner,
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
vintageYear,
unitType,
marketplace,
marketplaceLink,
marketplaceIdentifier,
unitTags,
unitStatus,
unitStatusReason,
unitRegistryLink,
correspondingAdjustmentDeclaration,
correspondingAdjustmentStatus,
unitBlockStart,
unitBlockEnd,
unitCount
) VALUES (
new.warehouseUnitId,
new.issuanceId,
new.projectLocationId,
new.orgUid,
new.unitOwner,
new.countryJurisdictionOfOwner,
new.inCountryJurisdictionOfOwner,
new.serialNumberBlock,
new.vintageYear,
new.unitType,
new.marketplace,
new.marketplaceLink,
new.marketplaceIdentifier,
new.unitTags,
new.unitStatus,
new.unitStatusReason,
new.unitRegistryLink,
new.correspondingAdjustmentDeclaration,
new.correspondingAdjustmentStatus,
new.unitBlockStart,
new.unitBlockEnd,
new.unitCount
);
END;
`);
}
},

async down(queryInterface) {
await queryInterface.dropTable('units_fts');
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE units_fts USING fts5(
warehouseProjectId,
orgUid,
currentRegistry,
projectId,
registryOfOrigin,
originProjectId,
program,
projectName,
projectLink,
projectDeveloper,
sector,
coveredByNDC,
projectType,
projectTags,
ndcInformation,
projectStatus,
projectStatusDate,
unitMetric,
methodology,
validationBody,
validationDate,
timeStaged
);
`);
await queryInterface.sequelize.query(
`INSERT INTO units_fts SELECT warehouseUnitId,
orgUid,
currentRegistry,
projectId,
registryOfOrigin,
originProjectId,
program,
projectName,
projectLink,
projectDeveloper,
sector,
coveredByNDC,
projectType,
projectTags,
ndcInformation,
projectStatus,
projectStatusDate,
unitMetric,
methodology,
validationBody,
validationDate,
timeStaged FROM units`,
);
},
};
5 changes: 5 additions & 0 deletions src/database/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import RepopulateVirtualTables from './20220515223227-re-populate-virtual-tables
import AddAuthorColumnToAuditTable from './20220708210357-adding-author-column-to-audit-table';
import CreateFileStore from './20220724212553-create-file-store';
import AddOptionalMethodology2FieldToProject from './20220721212845-add-optional-methodology2-field-to-project';
import PopulateUnitsFTS from './20220808192709-populate-units-fts';

export const migrations = [
{
Expand Down Expand Up @@ -139,4 +140,8 @@ export const migrations = [
migration: AddOptionalMethodology2FieldToProject,
name: '20220721212845-add-optional-methodology2-field-to-project',
},
{
migration: PopulateUnitsFTS,
name: '20220808192709-populate-units-fts',
},
];

0 comments on commit 227bc72

Please sign in to comment.