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

Pgwar-community-statements #364

Merged
merged 15 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
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
53 changes: 53 additions & 0 deletions database/migrations/20240613085447-pgwar-community-statements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240613085447-pgwar-community-statements-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240613085447-pgwar-community-statements-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240613180752-inf-t-resource-add-fk-class-not-null-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240613180752-inf-t-resource-add-fk-class-not-null-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
53 changes: 53 additions & 0 deletions database/migrations/20240618112815-pgwar-indexes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240618112815-pgwar-indexes-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240618112815-pgwar-indexes-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
114 changes: 32 additions & 82 deletions database/migrations/sqls/20240529120605-pgwar-entity-up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,105 +67,55 @@ END;
$$
LANGUAGE plpgsql;

-- Trigger function after_modify_info_proj_rel


-- Function to update pgwar from resource
----------------------------------------------
CREATE FUNCTION pgwar.after_modify_info_proj_rel()
RETURNS TRIGGER
CREATE FUNCTION pgwar.update_from_resource(NEW_RES information.resource)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
is_upsert boolean;
entity information.resource;
BEGIN
-- get the referenced information.resource
SELECT
* INTO entity
FROM
information.resource
WHERE
pk_entity = COALESCE(NEW.fk_entity, OLD.fk_entity);
-- if the referenced item is an entity
IF entity.pk_entity IS NOT NULL THEN
-- determine if this is an upsert action
-- if it is in at least one project ...
IF EXISTS(
SELECT
(NEW.is_in_project = TRUE
AND TG_OP != 'DELETE') INTO is_upsert;
-- if upsert ...
IF is_upsert = TRUE THEN
-- ... upsert the project entity
PERFORM
pgwar.upsert_entity_preview_fk_class(NEW.fk_entity, NEW.fk_project, entity.fk_class);
-- if allowed ...
IF (entity.community_visibility ->> 'toolbox')::bool = TRUE THEN
-- ... upsert the community entity
PERFORM
pgwar.upsert_entity_preview_fk_class(NEW.fk_entity, 0, entity.fk_class);
END IF;
ELSE
-- ... delete the project entity
DELETE FROM pgwar.entity_preview
WHERE pk_entity = COALESCE(NEW.fk_entity, OLD.fk_entity)
AND fk_project = COALESCE(NEW.fk_project, OLD.fk_project);
-- ... check if community entity has to be deleted
IF NOT EXISTS (
SELECT
pk_entity
FROM
projects.info_proj_rel
WHERE
fk_entity = COALESCE(NEW.fk_entity, OLD.fk_entity)
AND is_in_project = TRUE) THEN
-- ... delete the community entity
pk_entity
FROM
projects.info_proj_rel
WHERE
fk_entity = NEW_RES.pk_entity
AND is_in_project = TRUE) THEN
-- ... insert missing project entities or update existing, in case fk_class differs
PERFORM
pgwar.upsert_entity_preview_fk_class(fk_entity, fk_project, NEW_RES.fk_class)
FROM
projects.info_proj_rel
WHERE
fk_entity = NEW_RES.pk_entity
AND is_in_project = TRUE;
-- ... insert missing community entity or update existing, in case fk_class differs
PERFORM
pgwar.upsert_entity_preview_fk_class(NEW_RES.pk_entity, 0, NEW_RES.fk_class);
END IF;
-- if hidden for toolbox community ...
IF(NEW_RES.community_visibility ->> 'toolbox')::bool = FALSE THEN
-- ... delete potentially unallowed community entities
DELETE FROM pgwar.entity_preview
WHERE pk_entity = COALESCE(NEW.fk_entity, OLD.fk_entity)
AND fk_project = 0;
END IF;
WHERE fk_project = 0
AND pk_entity = NEW_RES.pk_entity;
END IF;
END IF;
RETURN NEW;
END;
$$;

CREATE TRIGGER after_modify_info_proj_rel
AFTER INSERT OR UPDATE OR DELETE ON projects.info_proj_rel
FOR EACH ROW
EXECUTE FUNCTION pgwar.after_modify_info_proj_rel();

-- Trigger function after_upsert_resource
----------------------------------------------
CREATE FUNCTION pgwar.after_upsert_resource()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
-- if it is in at least one project ...
IF EXISTS(
SELECT
pk_entity
FROM
projects.info_proj_rel
WHERE
fk_entity = COALESCE(NEW.pk_entity)
AND is_in_project = TRUE) THEN
-- ... insert missing project entities or update existing, in case fk_class differs
PERFORM
pgwar.upsert_entity_preview_fk_class(fk_entity, fk_project, NEW.fk_class)
FROM
projects.info_proj_rel
WHERE
fk_entity = NEW.pk_entity
AND is_in_project = TRUE;
-- ... insert missing community entity or update existing, in case fk_class differs
PERFORM
pgwar.upsert_entity_preview_fk_class(NEW.pk_entity, 0, NEW.fk_class);
END IF;
-- if hidden for toolbox community ...
IF(NEW.community_visibility ->> 'toolbox')::bool = FALSE THEN
-- ... delete potentially unallowed community entities
DELETE FROM pgwar.entity_preview
WHERE fk_project = 0
AND pk_entity = NEW.pk_entity;
END IF;
PERFORM pgwar.update_from_resource(NEW);

RETURN NEW;
END;
$$;
Expand Down
Loading