Skip to content

Commit

Permalink
Merge pull request #2931 from SeedCompany/edgedb/project-context
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF authored Nov 8, 2023
2 parents 720671a + e2df0a2 commit ac6332b
Show file tree
Hide file tree
Showing 24 changed files with 609 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dbschema/ceremony.esdl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Engagement {
abstract type Ceremony extending Resource {
abstract type Ceremony extending Child {
required planned: bool {
default := false;
};
Expand Down
13 changes: 13 additions & 0 deletions dbschema/common.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@ module default {
global currentUserId: uuid;

scalar type ReportPeriod extending enum<Monthly, Quarterly>;

# Helper function to workaround native support for sort ignoring accents
# https://stackoverflow.com/a/11007216
# https://github.com/edgedb/edgedb/issues/386
function str_sortable(value: str) -> str
using (
str_lower(
re_replace('Ñ', 'N',
str_trim(re_replace('[ [\\]|,\\-$]+', ' ', value, flags := 'g')),
flags := 'g'
)
)
);
}
37 changes: 27 additions & 10 deletions dbschema/engagement.esdl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module default {
abstract type Engagement extending Project::Resource {
abstract type Engagement extending Project::Child {
required status: Engagement::Status {
default := Engagement::Status.InDevelopment;
}
Expand Down Expand Up @@ -77,25 +77,37 @@ module default {
createdAt := datetime_of_statement(),
engagement := __new__,
project := __new__.project,
projectContext := __new__.projectContext,
}
);

trigger recalculateProjectSensOnInsert after insert for each do (
update (
select __new__.project
# Filter out projects without change, so modifiedAt isn't bumped
filter .sensitivity != max(.languages.sensitivity) ?? Sensitivity.High
select TranslationProject
filter __new__ in .languages
# Filter out projects without change, so modifiedAt isn't bumped
and .ownSensitivity != max(.languages.ownSensitivity) ?? Sensitivity.High
)
set { sensitivity := max(.languages.sensitivity) ?? Sensitivity.High }
set { ownSensitivity := max(.languages.ownSensitivity) ?? Sensitivity.High }
);
trigger recalculateProjectSensOnDelete after delete for each do (
with removedLang := __old__.language
update (
select __old__.project
# Filter out projects without change, so modifiedAt isn't bumped
filter .sensitivity != max((.languages except removedLang).sensitivity) ?? Sensitivity.High
select TranslationProject
filter __old__ in .languages
# Filter out projects without change, so modifiedAt isn't bumped
and .ownSensitivity != max((.languages except removedLang).ownSensitivity) ?? Sensitivity.High
)
set { sensitivity := max((.languages except removedLang).sensitivity) ?? Sensitivity.High }
set { ownSensitivity := max((.languages except removedLang).ownSensitivity) ?? Sensitivity.High }
);

trigger addProjectToContextOfLanguage after insert for each do (
update __new__.language.projectContext
set { projects += __new__.project }
);
trigger removeProjectFromContextOfLanguage after delete for each do (
update __old__.language.projectContext
set { projects -= __old__.project }
);
}

Expand All @@ -118,6 +130,7 @@ module default {
createdAt := datetime_of_statement(),
engagement := __new__,
project := __new__.project,
projectContext := __new__.projectContext,
}
);
}
Expand Down Expand Up @@ -150,7 +163,11 @@ module Engagement {
NotRenewed,
>;

abstract type Resource extending Project::Resource {
abstract type Child extending Project::Child {
annotation description := "\
A type that is a child of an engagement. \
It will always have a reference to a single engagement & project that it is under.";

required engagement: default::Engagement {
readonly := true;
on target delete delete source;
Expand Down
49 changes: 35 additions & 14 deletions dbschema/language.esdl
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
module default {
type Language extending Resource, Mixin::Pinnable, Mixin::Taggable {
type Language extending Resource, Project::ContextAware, Mixin::Pinnable, Mixin::Taggable {
required name: str;
index on (str_sortable(.name));

required displayName: str {
default := .name;
}
displayNamePronunciation: str;

required sensitivity: Sensitivity {
overloaded required ownSensitivity: Sensitivity {
annotation description := "The sensitivity of the language. This is a source / user settable.";
default := Sensitivity.High;
}
property effectiveSensitivity := max(.projects.sensitivity) ?? .sensitivity;

trigger recalculateProjectSens after update for each do (
update (
select __new__.projects
# Filter out projects without change, so modifiedAt isn't bumped
filter .sensitivity != max(.languages.sensitivity) ?? Sensitivity.High
select TranslationProject
filter __new__ in .languages
# Filter out projects without change, so modifiedAt isn't bumped
and .ownSensitivity != max(.languages.ownSensitivity) ?? Sensitivity.High
)
set { sensitivity := max(.languages.sensitivity) ?? Sensitivity.High }
set { ownSensitivity := max(.languages.ownSensitivity) ?? Sensitivity.High }
);

required ethnologue: Ethnologue::Language {
default := (insert Ethnologue::Language);
on source delete delete target;
}
required single link ethnologue := assert_exists(assert_single(
.<language[is Ethnologue::Language]
));
trigger connectEthnologue after insert for each do (
insert Ethnologue::Language {
language := __new__,
ownSensitivity := __new__.ownSensitivity,
projectContext := __new__.projectContext
}
);
trigger matchEthnologueToOwnSens after update for each do (
update __new__.ethnologue
filter .ownSensitivity != __new__.ownSensitivity
set { ownSensitivity := __new__.ownSensitivity }
);

required isDialect: bool {
default := false;
Expand Down Expand Up @@ -66,9 +79,12 @@ module default {
# Similar to previous version but avoids https://github.com/edgedb/edgedb/issues/5846
select LanguageEngagement filter __source__ = .language
);
multi link projects := .engagements.project;

property isMember := exists .projects.isMember;
overloaded link projectContext: Project::Context {
default := (insert Project::Context);
}

index on ((.name, .ownSensitivity, .leastOfThese, .isSignLanguage, .isDialect));
}

scalar type population extending int32 {
Expand All @@ -77,7 +93,12 @@ module default {
}

module Ethnologue {
type Language {
type Language extending Project::ContextAware {
required language: default::Language {
on target delete delete source;
constraint exclusive;
};

code: code {
constraint exclusive;
};
Expand Down
42 changes: 42 additions & 0 deletions dbschema/migrations/00005.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
CREATE MIGRATION m1mhq7b2c53yr7fxf7x5sgjogvrkg4hjj2nhpdivxja66sjpc5amza
ONTO m1unudoy7u4j3s2waa5jadkb43qedra7r6eab3ndzk22fhf4p5bfwq
{
CREATE FUNCTION default::str_sortable(value: std::str) -> std::str USING (std::str_lower(std::re_replace('Ñ', 'N', std::str_trim(std::re_replace(r'[ [\]|,\-$]+', ' ', value, flags := 'g')), flags := 'g')));
ALTER TYPE default::Language {
CREATE INDEX ON (default::str_sortable(.name));
CREATE INDEX ON (.sensitivity);
CREATE INDEX ON ((.name, .sensitivity, .leastOfThese, .isSignLanguage, .isDialect));
ALTER TRIGGER recalculateProjectSens USING (UPDATE
(SELECT
default::TranslationProject
FILTER
((__new__ IN .languages) AND (.sensitivity != (std::max(.languages.sensitivity) ?? default::Sensitivity.High)))
)
SET {
sensitivity := (std::max(.languages.sensitivity) ?? default::Sensitivity.High)
});
};
ALTER TYPE default::LanguageEngagement {
ALTER TRIGGER recalculateProjectSensOnDelete USING (WITH
removedLang :=
__old__.language
UPDATE
(SELECT
default::TranslationProject
FILTER
((__old__ IN .languages) AND (.sensitivity != (std::max(((.languages EXCEPT removedLang)).sensitivity) ?? default::Sensitivity.High)))
)
SET {
sensitivity := (std::max(((.languages EXCEPT removedLang)).sensitivity) ?? default::Sensitivity.High)
});
ALTER TRIGGER recalculateProjectSensOnInsert USING (UPDATE
(SELECT
default::TranslationProject
FILTER
((__new__ IN .languages) AND (.sensitivity != (std::max(.languages.sensitivity) ?? default::Sensitivity.High)))
)
SET {
sensitivity := (std::max(.languages.sensitivity) ?? default::Sensitivity.High)
});
};
};
16 changes: 16 additions & 0 deletions dbschema/migrations/00006.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE MIGRATION m1hjnudahjdbrqvoi7ikqsalk5uv4gyfk5rbzcnonq2qoauw4ee2ea
ONTO m1mhq7b2c53yr7fxf7x5sgjogvrkg4hjj2nhpdivxja66sjpc5amza
{
ALTER TYPE default::InternshipEngagement {
ALTER LINK project {
RESET OPTIONALITY;
RESET TYPE;
};
};
ALTER TYPE default::LanguageEngagement {
ALTER LINK project {
RESET OPTIONALITY;
RESET TYPE;
};
};
};
11 changes: 11 additions & 0 deletions dbschema/migrations/00007.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE MIGRATION m17wcugznjhc34awd734qo7mxst3vzwhc54xjrqawsjtlcuhbafdga
ONTO m1hjnudahjdbrqvoi7ikqsalk5uv4gyfk5rbzcnonq2qoauw4ee2ea
{
ALTER TYPE Project::Resource {
DROP PROPERTY sensitivity;
};
ALTER TYPE default::Language {
DROP INDEX ON (.sensitivity);
DROP PROPERTY effectiveSensitivity;
};
};
9 changes: 9 additions & 0 deletions dbschema/migrations/00008.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE MIGRATION m1rtq3fw45vdkon7lgrpisvdak2g663tcv7bekfed2bfxgkj23lu4q
ONTO m17wcugznjhc34awd734qo7mxst3vzwhc54xjrqawsjtlcuhbafdga
{
ALTER TYPE default::Language {
ALTER PROPERTY sensitivity {
RENAME TO ownSensitivity;
};
};
};
9 changes: 9 additions & 0 deletions dbschema/migrations/00009.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE MIGRATION m1kgkikeo6qaen7zzrnuhvhfiqak32ap254imd6or7naaatelnf24q
ONTO m1rtq3fw45vdkon7lgrpisvdak2g663tcv7bekfed2bfxgkj23lu4q
{
ALTER TYPE default::Project {
ALTER PROPERTY sensitivity {
RENAME TO ownSensitivity;
};
};
};
46 changes: 46 additions & 0 deletions dbschema/migrations/00010.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
CREATE MIGRATION m1bl5usbsd57f2obaikame74dp7p3oitsmulfitpvducp2k4oetwea
ONTO m1kgkikeo6qaen7zzrnuhvhfiqak32ap254imd6or7naaatelnf24q
{
ALTER TYPE Project::Resource {
DROP PROPERTY isMember;
};
CREATE TYPE Project::Context;
CREATE ABSTRACT TYPE Project::HasContext {
CREATE REQUIRED LINK projectContext: Project::Context {
SET default := (INSERT
Project::Context
);
};
CREATE PROPERTY ownSensitivity: default::Sensitivity;
};
ALTER TYPE default::Language {
DROP PROPERTY isMember;
};
ALTER TYPE default::Project {
DROP PROPERTY isMember;
EXTENDING Project::HasContext LAST;
};
ALTER TYPE Project::Context {
CREATE MULTI LINK projects: default::Project {
ON TARGET DELETE ALLOW;
};
};
ALTER TYPE Project::HasContext {
CREATE REQUIRED SINGLE PROPERTY isMember := (EXISTS (.projectContext.projects.membership));
};
ALTER TYPE default::Project {
ALTER PROPERTY ownSensitivity {
RESET OPTIONALITY;
RESET TYPE;
};
};
ALTER TYPE Project::Resource {
CREATE PROPERTY isMember := (.project.isMember);
};
ALTER TYPE Project::HasContext {
CREATE REQUIRED SINGLE PROPERTY sensitivity := ((std::max(.projectContext.projects.ownSensitivity) ?? (.ownSensitivity ?? default::Sensitivity.High)));
};
ALTER TYPE default::Language {
CREATE PROPERTY isMember := (EXISTS (.projects.isMember));
};
};
31 changes: 31 additions & 0 deletions dbschema/migrations/00011.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CREATE MIGRATION m1je52dmicyhild43tygqtxydgf4mp5nymhqyibrzv2a2yg5kbf4yq
ONTO m1bl5usbsd57f2obaikame74dp7p3oitsmulfitpvducp2k4oetwea
{
ALTER TYPE Ethnologue::Language {
CREATE REQUIRED LINK language: default::Language {
ON TARGET DELETE DELETE SOURCE;
SET REQUIRED USING (SELECT
default::Language FILTER
(.ethnologue = __source__)
LIMIT
1
);
CREATE CONSTRAINT std::exclusive;
};
};
ALTER TYPE default::Language {
ALTER LINK ethnologue {
RESET default;
USING (std::assert_exists(std::assert_single(.<language[IS Ethnologue::Language])));
RESET ON SOURCE DELETE;
SET SINGLE;
};
CREATE TRIGGER connectEthnologue
AFTER INSERT
FOR EACH DO (INSERT
Ethnologue::Language
{
language := __new__
});
};
};
Loading

0 comments on commit ac6332b

Please sign in to comment.