-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added PostgreSql migration of Shesha DB logic objects (procedur…
…es, triggers, views) feat: changed Hangfire storage configuration in the starter and functional tests project, they use current configured DB
- Loading branch information
ivan
committed
Jun 23, 2023
1 parent
4436583
commit 19b3ac5
Showing
68 changed files
with
1,175 additions
and
72 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
...src/Shesha.Framework/Migrations/PostgreSql/InitialScripts/Core_AddEntityHistoryEvents.sql
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,91 @@ | ||
CREATE OR REPLACE PROCEDURE public."Core_AddEntityHistoryEvents"( | ||
IN "changeTime" timestamp without time zone, | ||
IN reason text, | ||
IN "tenantId" integer, | ||
IN "userId" integer, | ||
IN changes "Core_EntityHistoryItem"[]) | ||
LANGUAGE 'plpgsql' | ||
AS $BODY$ | ||
DECLARE | ||
count BIGINT; | ||
ecsId INT; | ||
BEGIN | ||
|
||
SELECT COUNT(*) INTO count FROM unnest(changes) ; | ||
|
||
RAISE NOTICE '%', count; | ||
|
||
IF count > 0 THEN | ||
"changeTime" := COALESCE("changeTime", CURRENT_TIMESTAMP); | ||
|
||
-- insert EntityChangeSet | ||
INSERT INTO "AbpEntityChangeSets" ("BrowserInfo", "CreationTime", "Reason", "TenantId", "UserId") | ||
VALUES ('Direct DB change', "changeTime", "reason", "tenantId", "userId") | ||
RETURNING "Id" INTO ecsId; | ||
|
||
-- insert into entityChanges table | ||
CREATE TEMPORARY TABLE entityChanges ( | ||
"ChangeType" SMALLINT, | ||
"EntityId" VARCHAR(48), | ||
"EntityTypeFullName" VARCHAR(192) | ||
); | ||
|
||
INSERT INTO entityChanges ("ChangeType", "EntityId", "EntityTypeFullName") | ||
SELECT DISTINCT (unnest(changes))."ChangeType", (unnest(changes))."EntityId", (unnest(changes))."EntityTypeFullName"; | ||
|
||
-- Insert EntityChanges | ||
INSERT INTO "AbpEntityChanges" ("ChangeTime", "ChangeType", "EntityChangeSetId", "EntityId", "EntityTypeFullName", "TenantId") | ||
SELECT "changeTime", COALESCE(ec."ChangeType", 1), ecsId, ec."EntityId", ec."EntityTypeFullName", "tenantId" | ||
FROM entityChanges ec; | ||
|
||
-- Insert EntityPropertyChanges | ||
INSERT INTO "AbpEntityPropertyChanges" ("EntityChangeId", "NewValue", "OriginalValue", "PropertyName", "PropertyTypeFullName", "TenantId") | ||
SELECT | ||
ec."Id", | ||
c."NewValue", | ||
c."OldValue", | ||
c."PropertyName", | ||
c."PropertyTypeFullName", | ||
"tenantId" | ||
FROM unnest(changes) c | ||
JOIN "AbpEntityChanges" ec ON ec."EntityChangeSetId" = ecsId | ||
AND ec."ChangeType" = COALESCE(c."ChangeType", 1) | ||
AND ec."EntityId" = c."EntityId" | ||
AND ec."EntityTypeFullName" = c."EntityTypeFullName" | ||
WHERE TRIM(COALESCE(c."PropertyName", '')) <> ''; | ||
|
||
-- Insert Descriptions for EntityPropertyChanges | ||
INSERT INTO "Core_EntityHistoryEvents" ("Id", "EntityPropertyChangeId", "EventType", "Description") | ||
SELECT | ||
UUID_GENERATE_V4(), | ||
pc."Id", | ||
'PROPERTY_CHANGE_FRIENDLY_TEXT', | ||
c."Description" | ||
FROM unnest(changes) c | ||
JOIN "AbpEntityChanges" ec ON ec."EntityChangeSetId" = ecsId | ||
AND ec."ChangeType" = COALESCE(c."ChangeType", 1) | ||
AND ec."EntityId" = c."EntityId" | ||
AND ec."EntityTypeFullName" = c."EntityTypeFullName" | ||
JOIN "AbpEntityPropertyChanges" pc ON pc."EntityChangeId" = ec."Id" | ||
AND pc."PropertyName" = c."PropertyName" | ||
AND pc."PropertyTypeFullName" = c."PropertyTypeFullName" | ||
WHERE TRIM(COALESCE(c."PropertyName", '')) <> '' | ||
AND TRIM(COALESCE(c."Description", '')) <> ''; | ||
|
||
-- Insert Descriptions for EntityChanges | ||
INSERT INTO "Core_EntityHistoryEvents" ("Id", "EntityChangeId", "EventType", "Description") | ||
SELECT | ||
UUID_GENERATE_V4(), | ||
ec."Id", | ||
'ENTITY_EVENT', | ||
c."Description" | ||
FROM unnest(changes) c | ||
JOIN "AbpEntityChanges" ec ON ec."EntityChangeSetId" = ecsId | ||
AND ec."ChangeType" = COALESCE(c."ChangeType", 1) | ||
AND ec."EntityId" = c."EntityId" | ||
AND ec."EntityTypeFullName" = c."EntityTypeFullName" | ||
WHERE c."PropertyName" IS NULL | ||
AND TRIM(COALESCE(c."Description", '')) <> ''; | ||
END IF; | ||
END; | ||
$BODY$; |
36 changes: 36 additions & 0 deletions
36
...hesha.Framework/Migrations/PostgreSql/InitialScripts/Core_AddSingleEntityHistoryEvent.sql
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,36 @@ | ||
CREATE OR REPLACE PROCEDURE public."Core_AddSingleEntityHistoryEvent"( | ||
IN "changeTime" timestamp without time zone, | ||
IN reason character varying, | ||
IN "tenantId" integer, | ||
IN "userId" integer, | ||
IN "changeType" smallint, | ||
IN "entityId" character varying, | ||
IN "entityTypeFullName" character varying, | ||
IN "propertyName" character varying, | ||
IN "propertyTypeFullName" character varying, | ||
IN "newValue" character varying, | ||
IN "oldValue" character varying, | ||
IN description character varying) | ||
LANGUAGE 'plpgsql' | ||
AS $BODY$ | ||
declare | ||
changes "Core_EntityHistoryItem"; -- Typed table we created. | ||
begin | ||
|
||
INSERT INTO "Core_EntityHistoryItems" ("ChangeType", "EntityId", "EntityTypeFullName", "PropertyName", "PropertyTypeFullName", "NewValue", "OldValue", "Description") | ||
values ("changeType", "entityId", "entityTypeFullName", "propertyName", "propertyTypeFullName", "newValue", "oldValue", "description"); | ||
|
||
|
||
|
||
call "Core_AddEntityHistoryEvents" | ||
( | ||
"changeTime", | ||
"reason", | ||
"tenantId", | ||
"userId", | ||
array[row("changeType", "entityId", "entityTypeFullName", "propertyName", "propertyTypeFullName", "newValue", "oldValue", "description")]::"Core_EntityHistoryItem"[] | ||
); | ||
|
||
|
||
end | ||
$BODY$; |
11 changes: 11 additions & 0 deletions
11
...core/src/Shesha.Framework/Migrations/PostgreSql/InitialScripts/Core_EntityHistoryItem.sql
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,11 @@ | ||
CREATE TYPE public."Core_EntityHistoryItem" AS | ||
( | ||
"ChangeType" smallint, | ||
"EntityId" character varying(48), | ||
"EntityTypeFullName" character varying(192), | ||
"PropertyName" character varying(96), | ||
"PropertyTypeFullName" character varying(256), | ||
"NewValue" character varying(512), | ||
"OldValue" character varying(512), | ||
"Description" character varying(512) | ||
); |
20 changes: 20 additions & 0 deletions
20
...sha.Framework/Migrations/PostgreSql/InitialScripts/log_Core_Persons_UpdateContacts_AU.sql
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,20 @@ | ||
CREATE OR REPLACE FUNCTION public."log_Core_Persons_UpdateContacts_AU"() | ||
RETURNS trigger | ||
LANGUAGE 'plpgsql' | ||
COST 100 | ||
VOLATILE NOT LEAKPROOF | ||
AS $BODY$ | ||
BEGIN | ||
IF OLD."MobileNumber1" <> NEW."MobileNumber1" OR OLD."EmailAddress1" <> NEW."EmailAddress1" THEN | ||
update | ||
"AbpUsers" | ||
set | ||
"PhoneNumber" = NEW."MobileNumber1", | ||
"EmailAddress" = NEW."EmailAddress1" | ||
where | ||
"AbpUsers"."Id" = OLD."UserId"; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$BODY$; |
39 changes: 39 additions & 0 deletions
39
...work/Migrations/PostgreSql/InitialScripts/log_Frwk_ConfigurationItems_UpdateIsLast_AD.sql
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,39 @@ | ||
CREATE OR REPLACE FUNCTION public."log_Frwk_ConfigurationItems_UpdateIsLast_AD"() | ||
RETURNS trigger | ||
LANGUAGE 'plpgsql' | ||
COST 100 | ||
VOLATILE NOT LEAKPROOF | ||
AS $BODY$ | ||
declare | ||
v_cnt numeric; | ||
BEGIN | ||
|
||
v_cnt := 0; | ||
|
||
IF EXISTS (SELECT 1 FROM deleted WHERE "IsLast" = true) THEN | ||
UPDATE "Frwk_ConfigurationItems" item | ||
SET "IsLast" = true | ||
FROM deleted | ||
where item."IsLast" = false | ||
AND (deleted."ModuleId" = item."ModuleId" OR (deleted."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND deleted."Name" = item."Name" | ||
AND deleted."ItemType" = item."ItemType" | ||
AND item."Id" = ( | ||
select "Id" | ||
from "Frwk_ConfigurationItems" lastVersions | ||
where lastVersions."IsDeleted" = false | ||
and (lastVersions."ModuleId" = item."ModuleId" or lastVersions."ModuleId" is null and item."ModuleId" is null) | ||
and lastVersions."Name" = item."Name" | ||
and lastVersions."ItemType" = item."ItemType" | ||
order by lastVersions."VersionNo" desc | ||
limit 1 | ||
); | ||
|
||
GET DIAGNOSTICS v_cnt = ROW_COUNT; | ||
Raise notice 'set IsLast = true: %', (v_cnt); | ||
|
||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$BODY$; |
78 changes: 78 additions & 0 deletions
78
...work/Migrations/PostgreSql/InitialScripts/log_Frwk_ConfigurationItems_UpdateIsLast_AI.sql
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,78 @@ | ||
CREATE OR REPLACE FUNCTION public."log_Frwk_ConfigurationItems_UpdateIsLast_AI"() | ||
RETURNS trigger | ||
LANGUAGE 'plpgsql' | ||
COST 100 | ||
VOLATILE NOT LEAKPROOF | ||
AS $BODY$ | ||
declare | ||
v_cnt numeric; | ||
toupdate numeric; | ||
BEGIN | ||
Raise notice 'Value: %', (select "ModuleId" from inserted); | ||
|
||
v_cnt := 0; | ||
|
||
UPDATE "Frwk_ConfigurationItems" item | ||
SET "IsLast" = false | ||
FROM inserted | ||
WHERE | ||
item."IsLast" = true | ||
AND (inserted."ModuleId" = item."ModuleId" OR (inserted."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND inserted."Name" = item."Name" | ||
AND inserted."ItemType" = item."ItemType" | ||
AND item."Id" <> ( | ||
SELECT "Id" | ||
FROM "Frwk_ConfigurationItems" lastVersions | ||
WHERE lastVersions."IsDeleted" = false | ||
AND (lastVersions."ModuleId" = item."ModuleId" OR (lastVersions."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND lastVersions."Name" = item."Name" | ||
AND lastVersions."ItemType" = item."ItemType" | ||
ORDER BY lastVersions."VersionNo" DESC | ||
LIMIT 1 | ||
); | ||
|
||
GET DIAGNOSTICS v_cnt = ROW_COUNT; | ||
Raise notice 'set IsLast = false: %', (v_cnt); | ||
select count(1) into toupdate | ||
FROM "Frwk_ConfigurationItems" item | ||
inner JOIN inserted ON (inserted."ModuleId" = item."ModuleId" OR (inserted."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND inserted."Name" = item."Name" | ||
AND inserted."ItemType" = item."ItemType" | ||
WHERE item."IsLast" = false | ||
AND item."Id" = ( | ||
SELECT "Id" | ||
FROM "Frwk_ConfigurationItems" lastVersions | ||
WHERE lastVersions."IsDeleted" = false | ||
AND (lastVersions."ModuleId" = item."ModuleId" OR (lastVersions."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND lastVersions."Name" = item."Name" | ||
AND lastVersions."ItemType" = item."ItemType" | ||
ORDER BY lastVersions."VersionNo" DESC | ||
LIMIT 1 | ||
); | ||
Raise notice 'toupdate: %', (toupdate); | ||
v_cnt := 0; | ||
|
||
UPDATE "Frwk_ConfigurationItems" item | ||
SET "IsLast" = true | ||
FROM inserted | ||
WHERE item."IsLast" = false | ||
AND (inserted."ModuleId" = item."ModuleId" OR (inserted."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND inserted."Name" = item."Name" | ||
AND inserted."ItemType" = item."ItemType" | ||
AND item."Id" = ( | ||
SELECT "Id" | ||
FROM "Frwk_ConfigurationItems" lastVersions | ||
WHERE lastVersions."IsDeleted" = false | ||
AND (lastVersions."ModuleId" = item."ModuleId" OR (lastVersions."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND lastVersions."Name" = item."Name" | ||
AND lastVersions."ItemType" = item."ItemType" | ||
ORDER BY lastVersions."VersionNo" DESC | ||
LIMIT 1 | ||
); | ||
|
||
GET DIAGNOSTICS v_cnt = ROW_COUNT; | ||
Raise notice 'set IsLast = true: %', (v_cnt); | ||
|
||
RETURN null; | ||
END; | ||
$BODY$; |
85 changes: 85 additions & 0 deletions
85
...work/Migrations/PostgreSql/InitialScripts/log_Frwk_ConfigurationItems_UpdateIsLast_AU.sql
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,85 @@ | ||
CREATE OR REPLACE FUNCTION public."log_Frwk_ConfigurationItems_UpdateIsLast_AU"() | ||
RETURNS trigger | ||
LANGUAGE 'plpgsql' | ||
COST 100 | ||
VOLATILE NOT LEAKPROOF | ||
AS $BODY$ | ||
declare | ||
v_cnt numeric; | ||
toupdate numeric; | ||
BEGIN | ||
|
||
v_cnt := 0; | ||
|
||
IF TG_OP = 'UPDATE' THEN | ||
IF EXISTS ( SELECT 1 FROM updated | ||
inner join "Frwk_ConfigurationItems" conf on conf."Id"=updated."Id" | ||
WHERE (updated."VersionNo" IS DISTINCT FROM conf."VersionNo") OR (updated."IsDeleted" IS DISTINCT FROM conf."IsDeleted") | ||
) THEN | ||
|
||
UPDATE "Frwk_ConfigurationItems" item | ||
SET "IsLast" = FALSE | ||
FROM updated | ||
WHERE item."IsLast" = TRUE | ||
AND (updated."ModuleId" = item."ModuleId" OR (updated."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND updated."Name" = item."Name" | ||
AND updated."ItemType" = item."ItemType" | ||
AND item."Id" <> ( | ||
SELECT "Id" | ||
FROM "Frwk_ConfigurationItems" lastVersions | ||
WHERE lastVersions."IsDeleted" = FALSE | ||
AND (lastVersions."ModuleId" = item."ModuleId" OR (lastVersions."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND lastVersions."Name" = item."Name" | ||
AND lastVersions."ItemType" = item."ItemType" | ||
ORDER BY lastVersions."VersionNo" DESC | ||
LIMIT 1 | ||
); | ||
|
||
GET DIAGNOSTICS v_cnt = ROW_COUNT; | ||
Raise notice 'set IsLast = false: %', (v_cnt); | ||
select count(1) into toupdate | ||
FROM "Frwk_ConfigurationItems" item | ||
inner JOIN updated ON (updated."ModuleId" = item."ModuleId" OR (updated."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND updated."Name" = item."Name" | ||
AND updated."ItemType" = item."ItemType" | ||
WHERE item."IsLast" = false | ||
AND item."Id" = ( | ||
SELECT "Id" | ||
FROM "Frwk_ConfigurationItems" lastVersions | ||
WHERE lastVersions."IsDeleted" = false | ||
AND (lastVersions."ModuleId" = item."ModuleId" OR (lastVersions."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND lastVersions."Name" = item."Name" | ||
AND lastVersions."ItemType" = item."ItemType" | ||
ORDER BY lastVersions."VersionNo" DESC | ||
LIMIT 1 | ||
); | ||
Raise notice 'toupdate: %', (toupdate); | ||
v_cnt := 0; | ||
|
||
UPDATE "Frwk_ConfigurationItems" item | ||
SET "IsLast" = TRUE | ||
FROM updated | ||
WHERE item."IsLast" = FALSE | ||
AND (updated."ModuleId" = item."ModuleId" OR (updated."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND updated."Name" = item."Name" | ||
AND updated."ItemType" = item."ItemType" | ||
AND item."Id" = ( | ||
SELECT "Id" | ||
FROM "Frwk_ConfigurationItems" lastVersions | ||
WHERE lastVersions."IsDeleted" = FALSE | ||
AND (lastVersions."ModuleId" = item."ModuleId" OR (lastVersions."ModuleId" IS NULL AND item."ModuleId" IS NULL)) | ||
AND lastVersions."Name" = item."Name" | ||
AND lastVersions."ItemType" = item."ItemType" | ||
ORDER BY lastVersions."VersionNo" DESC | ||
LIMIT 1 | ||
); | ||
|
||
GET DIAGNOSTICS v_cnt = ROW_COUNT; | ||
Raise notice 'set IsLast = true: %', (v_cnt); | ||
|
||
END IF; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$BODY$; |
Oops, something went wrong.