Skip to content

Commit

Permalink
feat: use uuid as primary key for all global tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael.Taylor committed Jan 7, 2022
1 parent 9926255 commit 8b5ffdd
Show file tree
Hide file tree
Showing 28 changed files with 140 additions and 387 deletions.
1 change: 0 additions & 1 deletion migrations/20211212200953-fulltext-search.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
if (queryInterface.sequelize.getDialect() === 'sqlite') {
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE projects_fts USING fts5(
id,
orgUid,
warehouseProjectId,
projectId,
Expand Down
4 changes: 0 additions & 4 deletions migrations/20211219182106-sqlite-triggers-projects.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
await queryInterface.sequelize.query(`
CREATE TRIGGER project_insert_fts AFTER INSERT ON projects BEGIN
INSERT INTO projects_fts(
id,
orgUid,
warehouseProjectId,
currentRegistry,
Expand All @@ -29,7 +28,6 @@ module.exports = {
validationDate,
projectTag
) VALUES (
new.id,
new.orgUid,
new.warehouseProjectId,
new.currentRegistry,
Expand Down Expand Up @@ -64,7 +62,6 @@ module.exports = {
CREATE TRIGGER project_update_fts AFTER UPDATE ON projects BEGIN
DELETE FROM projects_fts WHERE id = old.id;
INSERT INTO projects_fts(
id,
orgUid,
warehouseProjectId,
currentRegistry,
Expand All @@ -87,7 +84,6 @@ module.exports = {
validationDate,
projectTag
) VALUES (
new.id,
new.orgUid,
new.warehouseProjectId,
new.currentRegistry,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "mocha tests/**/*.spec.js --reporter spec --exit",
"release": "./node_modules/.bin/standard-version && git push --tags",
"postinstall": "npm run requirements-check",
"resetdb": "rm ./data.sqlite3 && npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all"
"resetdb": "rm ./data.sqlite3 && npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all --debug"
},
"dependencies": {
"body-parser": "^1.19.0",
Expand Down
8 changes: 4 additions & 4 deletions seeders/20211209205139-add-units.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
const UnitStub = require('../src/models/units/units.stub.json');
const JunctionStub = [
{
unitId: 1,
qualificationId: 1,
unitId: '5c960ac1-a180-45a4-9850-be177e26d2fb',
qualificationId: '702cafbb-c624-4273-9cdc-c617ad5675df',
},
{
unitId: 1,
qualificationId: 2,
unitId: '5c960ac1-a180-45a4-9850-be177e26d2fb',
qualificationId: '76903895-840e-406c-b2a0-f90244acf02d',
},
];
module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions src/models/co-benefits/co-benefits.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class CoBenefit extends Model {
static associate() {
CoBenefit.belongsTo(Project, {
onDelete: 'CASCADE',
targetKey: 'warehouseProjectId',
foreignKey: 'projectId',
});
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/models/co-benefits/co-benefits.stub.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
[
{
"id": "9506cf84-6f9e-437a-a099-9672e4737668",
"benefit": "TEST_COBENEFIT_1",
"projectId": 1
"projectId": "81e05bfa-e93f-458f-b907-96bf170e52cd"
},
{
"id": "f124ebef-6436-4cd4-abdd-c55e10f4d1a0",
"benefit": "TEST_COBENEFIT_2",
"projectId": 1
"projectId": "81e05bfa-e93f-458f-b907-96bf170e52cd"
},
{
"id": "729c71bf-cc6e-4424-842f-54c2a490952a",
"benefit": "TEST_COBENEFIT_3",
"projectId": 1
"projectId": "81e05bfa-e93f-458f-b907-96bf170e52cd"
},
{
"id": "2995a129-1fad-4251-9942-7c6db2fbff74",
"benefit": "TEST_COBENEFIT_4",
"projectId": 1
"projectId": "81e05bfa-e93f-458f-b907-96bf170e52cd"
},
{
"id": "83913f9e-9876-4c4d-ac4c-10dd992d7d7f",
"benefit": "TEST_COBENEFIT_5",
"projectId": 1
"projectId": "81e05bfa-e93f-458f-b907-96bf170e52cd"
}
]
6 changes: 4 additions & 2 deletions src/models/co-benefits/co-benifets.modeltypes.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { uuid: uuidv4 } = require('uuidv4');
const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.INTEGER,
type: Sequelize.STRING,
unique: true,
defaultValue: () => uuidv4(),
primaryKey: true,
autoIncrement: true,
},
benefit: Sequelize.STRING,
projectId: {
Expand Down
2 changes: 2 additions & 0 deletions src/models/locations/locations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ProjectLocation extends Model {
static associate() {
ProjectLocation.belongsTo(Project, {
onDelete: 'CASCADE',
targetKey: 'warehouseProjectId',
foreignKey: 'projectId',
});
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/models/locations/locations.modeltypes.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { uuid: uuidv4 } = require('uuidv4');
const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.INTEGER,
type: Sequelize.STRING,
unique: true,
defaultValue: () => uuidv4(),
primaryKey: true,
autoIncrement: true,
},
countryRegion: Sequelize.STRING,
country: Sequelize.STRING,
Expand Down
5 changes: 5 additions & 0 deletions src/models/locations/locations.stub.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
[
{
"id": "89adcb9a-dc10-41aa-8606-c1ce1545dcbd",
"countryRegion": "United States",
"projectId": 1
},
{
"id": "b41d0f89-2f71-4bde-b6d0-5cf77802c3fc",
"countryRegion": "United States",
"projectId": 1
},
{
"id": "8f23fd77-d19f-4083-b7be-949288e88a36",
"countryRegion": "China",
"projectId": 2
},
{
"id": "f28965c3-96b8-40ef-809e-894975d16c4d",
"countryRegion": "Russia",
"projectId": 2
},
{
"id": "40ae83b1-b497-4f63-9b7d-874505b67662",
"countryRegion": "United States",
"projectId": 2
}
Expand Down
8 changes: 4 additions & 4 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class Project extends Model {

static async findAllMySQLFts(searchStr, orgUid, pagination, columns = []) {
const { offset, limit } = pagination;

let fields = '*';
if (columns.length) {
fields = columns.join(', ');
}

let sql = `
SELECT ${fields} FROM projects WHERE MATCH (
warehouseProjectId,
Expand Down Expand Up @@ -111,12 +111,12 @@ class Project extends Model {

static async findAllSqliteFts(searchStr, orgUid, pagination, columns = []) {
const { offset, limit } = pagination;

let fields = '*';
if (columns.length) {
fields = columns.join(', ');
}

searchStr = searchStr = searchStr.replaceAll('-', '+');

let sql = `SELECT ${fields} FROM projects_fts WHERE projects_fts MATCH :search`;
Expand Down
8 changes: 3 additions & 5 deletions src/models/projects/projects.modeltypes.cjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const { uuid: uuidv4 } = require('uuidv4');
const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
warehouseProjectId: {
type: Sequelize.STRING,
unique: true,
defaultValue: () => uuidv4(),
primaryKey: true,
},
// The orgUid is the singeltonId of the
// organizations tables on the datalayer
Expand Down
18 changes: 9 additions & 9 deletions src/models/projects/projects.stub.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"warehouseprojectId": "81e05bfa-e93f-458f-b907-96bf170e52cd",
"warehouseProjectId": "81e05bfa-e93f-458f-b907-96bf170e52cd",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand All @@ -28,7 +28,7 @@
},
{
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"warehouseprojectId": "897891e2-cc66-4867-8da2-c17d69d018cb",
"warehouseProjectId": "897891e2-cc66-4867-8da2-c17d69d018cb",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand All @@ -55,7 +55,7 @@
},
{
"orgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"warehouseprojectId": "8a02a620-8be8-44f9-ba90-eff00ccc3a70",
"warehouseProjectId": "8a02a620-8be8-44f9-ba90-eff00ccc3a70",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand All @@ -82,7 +82,7 @@
},
{
"orgUid": "fbffae6b-0203-4ac0-a08b-1551b730783b",
"warehouseprojectId": "11954678-f7a5-47d2-94f8-f4f3138a529c",
"warehouseProjectId": "11954678-f7a5-47d2-94f8-f4f3138a529c",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand All @@ -109,7 +109,7 @@
},
{
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"warehouseprojectId": "634f3d01-ca5c-4c0c-80d4-9dec6e0ec712",
"warehouseProjectId": "634f3d01-ca5c-4c0c-80d4-9dec6e0ec712",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand All @@ -136,7 +136,7 @@
},
{
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"warehouseprojectId": "0d918e3e-9203-480f-b2e2-0217f437d5c1",
"warehouseProjectId": "0d918e3e-9203-480f-b2e2-0217f437d5c1",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand All @@ -163,7 +163,7 @@
},
{
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"warehouseprojectId": "1943d442-d243-456a-b2d6-cbb47b01fcb9",
"warehouseProjectId": "1943d442-d243-456a-b2d6-cbb47b01fcb9",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand All @@ -190,7 +190,7 @@
},
{
"orgUid": "70150fde-57f6-44a6-9486-1fef49528475",
"warehouseprojectId": "c2a92ac1-8d63-4346-b747-dae736b0556c",
"warehouseProjectId": "c2a92ac1-8d63-4346-b747-dae736b0556c",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand All @@ -217,7 +217,7 @@
},
{
"orgUid": "70150fde-57f6-44a6-9486-1fef49528475",
"warehouseprojectId": "da494c65-17ec-4694-85df-e44de2c77cf5",
"warehouseProjectId": "da494c65-17ec-4694-85df-e44de2c77cf5",
"currentRegistry": "Feedfire",
"registryOfOrigin": "Skinte",
"originprojectId": "c5067a60-e329-4510-991c-b302fd00ec8d",
Expand Down
5 changes: 4 additions & 1 deletion src/models/qualifications/qualifications.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class Qualification extends Model {
// When all qualifications are removed from
// all projects and units, remove completly,
// otherwise just dissaciate
Qualification.belongsTo(Project);
Qualification.belongsTo(Project, {
targetKey: 'warehouseProjectId',
foreignKey: 'projectId',
});
//Qualification.hasMany(Unit);
// https://gist.github.com/elliette/20ddc4e827efd9d62bc98752e7a62610#some-important-addendums
Qualification.belongsToMany(Unit, {
Expand Down
6 changes: 4 additions & 2 deletions src/models/qualifications/qualifications.modeltypes.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { uuid: uuidv4 } = require('uuidv4');
const Sequelize = require('sequelize');

module.exports = {
id: {
autoIncrement: true,
type: Sequelize.STRING,
unique: true,
defaultValue: () => uuidv4(),
primaryKey: true,
type: Sequelize.INTEGER,
},
qualificationId: Sequelize.INTEGER,
qualificationLink: Sequelize.STRING,
Expand Down
12 changes: 5 additions & 7 deletions src/models/qualifications/qualifications.stub.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[
{
"id": "1",
"projectId": 2,
"id": "702cafbb-c624-4273-9cdc-c617ad5675df",
"type": "basic type",
"label": "qualification 1",
"creditingPeriodStartDate": "2011-10-05T14:48:00.000Z",
Expand All @@ -10,32 +9,31 @@
"qualificationLink": "https://qualification.link/1"
},
{
"id": "2",
"projectId": 1,
"id": "76903895-840e-406c-b2a0-f90244acf02d",
"type": "basic type",
"label": "qualification 2",
"creditingPeriodStartDate": "2011-10-05T14:48:00.000Z",
"creditingPeriodEndDate": "2022-10-05T14:48:00.000Z",
"qualificationLink": "https://qualification.link/2"
},
{
"id": "3",
"id": "2c9d41fc-1f58-427d-b028-fc83bf45c74f",
"type": "basic type",
"label": "qualification 3",
"creditingPeriodStartDate": "2010-10-05T14:48:00.000Z",
"creditingPeriodEndDate": "2022-10-05T14:48:00.000Z",
"qualificationLink": "https://qualification.link/3"
},
{
"id": "4",
"id": "2421e951-0a68-4f9f-b760-01528c605237",
"type": "basic type",
"label": "qualification 4",
"creditingPeriodStartDate": "2011-10-05T14:48:00.000Z",
"creditingPeriodEndDate": "2022-10-05T14:48:00.000Z",
"qualificationLink": "https://qualification.link/4"
},
{
"id": "5",
"id": "df7c1ff8-f669-4028-bcbd-3abedf7b1a49",
"type": "basic type",
"label": "qualification 5",
"creditingPeriodStartDate": "2010-10-05T14:48:00.000Z",
Expand Down
2 changes: 2 additions & 0 deletions src/models/ratings/ratings.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Rating extends Model {
static associate() {
Rating.belongsTo(Project, {
onDelete: 'CASCADE',
targetKey: 'warehouseProjectId',
foreignKey: 'projectId',
});
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/models/ratings/ratings.modeltypes.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { uuid: uuidv4 } = require('uuidv4');
const Sequelize = require('sequelize');

module.exports = {
id: {
type: Sequelize.INTEGER,
type: Sequelize.STRING,
unique: true,
defaultValue: () => uuidv4(),
primaryKey: true,
autoIncrement: true,
},
type: Sequelize.STRING,
rating: Sequelize.NUMBER,
Expand Down
Loading

0 comments on commit 8b5ffdd

Please sign in to comment.