Skip to content

Commit

Permalink
Updated database to store ActivityAffectedObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
tonydamage committed Aug 7, 2023
1 parent 64e5b5f commit 973a7d8
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 276 deletions.
60 changes: 31 additions & 29 deletions config/sql/native-new/postgres-new-upgrade.sql
Original file line number Diff line number Diff line change
Expand Up @@ -325,38 +325,12 @@ $aa$);

-- Task Affected Indexing (Changes to types)
call apply_change(16, $aa$
ALTER TYPE ContainerType ADD VALUE IF NOT EXISTS 'AFFECTED_RESOURCE_OBJECTS' AFTER 'ACCESS_CERTIFICATION_WORK_ITEM';
ALTER TYPE ContainerType ADD VALUE IF NOT EXISTS 'AFFECTED_OBJECTS' AFTER 'AFFECTED_RESOURCE_OBJECTS';
ALTER TYPE ContainerType ADD VALUE IF NOT EXISTS 'AFFECTED_OBJECTS' AFTER 'ACCESS_CERTIFICATION_WORK_ITEM';
$aa$);

-- Task Affected Indexing (tables)

call apply_change(17, $aa$
CREATE TABLE m_task_affected_resource_objects (
ownerOid UUID NOT NULL REFERENCES m_object_oid(oid) ON DELETE CASCADE,
containerType ContainerType GENERATED ALWAYS AS ('AFFECTED_RESOURCE_OBJECTS') STORED
CHECK (containerType = 'AFFECTED_RESOURCE_OBJECTS'),
objectClassId INTEGER REFERENCES m_uri(id),
resourceRefTargetOid UUID,
resourceRefTargetType ObjectType,
resourceRefRelationId INTEGER REFERENCES m_uri(id),
intent TEXT,
kind ShadowKindType,
PRIMARY KEY (ownerOid, cid)
) INHERITS(m_container);

CREATE TABLE m_task_affected_objects (
ownerOid UUID NOT NULL REFERENCES m_object_oid(oid) ON DELETE CASCADE,
containerType ContainerType GENERATED ALWAYS AS ('AFFECTED_OBJECTS') STORED
CHECK (containerType = 'AFFECTED_OBJECTS'),
type ObjectType,
archetypeRefTargetOid UUID,
archetypeRefTargetType ObjectType,
archetypeRefRelationId INTEGER REFERENCES m_uri(id),
PRIMARY KEY (ownerOid, cid)
) INHERITS(m_container);
-- Task Affected Indexing (tables), empty now, replaced with change 19

$aa$);
call apply_change(17, $$ SELECT 1 $$, true);


-- Resource/super/resourceRef Indexing (tables)
Expand All @@ -366,6 +340,34 @@ ADD COLUMN superRefTargetOid UUID,
ADD COLUMN superRefTargetType ObjectType,
ADD COLUMN superRefRelationId INTEGER REFERENCES m_uri(id);
$aa$)

-- Fixed upgrade for task indexing
-- Drop tables should only affect development machines
call apply_change(19, $aa$
DROP TABLE IF EXISTS m_task_affected_resource_objects;
DROP TABLE IF EXISTS m_task_affected_objects;

CREATE TABLE m_task_affected_objects (
ownerOid UUID NOT NULL REFERENCES m_object_oid(oid) ON DELETE CASCADE,
containerType ContainerType GENERATED ALWAYS AS ('AFFECTED_OBJECTS') STORED
CHECK (containerType = 'AFFECTED_OBJECTS'),
activityId INTEGER REFERENCES m_uri(id),
type ObjectType,
archetypeRefTargetOid UUID,
archetypeRefTargetType ObjectType,
archetypeRefRelationId INTEGER REFERENCES m_uri(id),
objectClassId INTEGER REFERENCES m_uri(id),
resourceRefTargetOid UUID,
resourceRefTargetType ObjectType,
resourceRefRelationId INTEGER REFERENCES m_uri(id),
intent TEXT,
kind ShadowKindType,
PRIMARY KEY (ownerOid, cid)
) INHERITS(m_container);

$aa$)


---
-- WRITE CHANGES ABOVE ^^
-- IMPORTANT: update apply_change number at the end of postgres-new.sql
Expand Down
39 changes: 17 additions & 22 deletions config/sql/native-new/postgres-new.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CREATE EXTENSION IF NOT EXISTS fuzzystrmatch; -- fuzzy string match (levenshtein
CREATE TYPE ContainerType AS ENUM (
'ACCESS_CERTIFICATION_CASE',
'ACCESS_CERTIFICATION_WORK_ITEM',
'AFFECTED_OBJECTS',
'ASSIGNMENT',
'CASE_WORK_ITEM',
'FOCUS_IDENTITY',
Expand Down Expand Up @@ -1312,28 +1313,22 @@ CREATE INDEX m_task_fullTextInfo_idx ON m_task USING gin(fullTextInfo gin_trgm_o
CREATE INDEX m_task_createTimestamp_idx ON m_task (createTimestamp);
CREATE INDEX m_task_modifyTimestamp_idx ON m_task (modifyTimestamp);

CREATE TABLE m_task_affected_resource_objects (
ownerOid UUID NOT NULL REFERENCES m_object_oid(oid) ON DELETE CASCADE,
containerType ContainerType GENERATED ALWAYS AS ('AFFECTED_RESOURCE_OBJECTS') STORED
CHECK (containerType = 'AFFECTED_RESOURCE_OBJECTS'),
objectClassId INTEGER REFERENCES m_uri(id),
resourceRefTargetOid UUID,
resourceRefTargetType ObjectType,
resourceRefRelationId INTEGER REFERENCES m_uri(id),
intent TEXT,
kind ShadowKindType,
PRIMARY KEY (ownerOid, cid)
) INHERITS(m_container);

CREATE TABLE m_task_affected_objects (
ownerOid UUID NOT NULL REFERENCES m_object_oid(oid) ON DELETE CASCADE,
containerType ContainerType GENERATED ALWAYS AS ('AFFECTED_OBJECTS') STORED
CHECK (containerType = 'AFFECTED_OBJECTS'),
type ObjectType,
archetypeRefTargetOid UUID,
archetypeRefTargetType ObjectType,
archetypeRefRelationId INTEGER REFERENCES m_uri(id),
PRIMARY KEY (ownerOid, cid)
ownerOid UUID NOT NULL REFERENCES m_object_oid(oid) ON DELETE CASCADE,
containerType ContainerType GENERATED ALWAYS AS ('AFFECTED_OBJECTS') STORED
CHECK (containerType = 'AFFECTED_OBJECTS'),
activityId INTEGER REFERENCES m_uri(id),
type ObjectType,
archetypeRefTargetOid UUID,
archetypeRefTargetType ObjectType,
archetypeRefRelationId INTEGER REFERENCES m_uri(id),
objectClassId INTEGER REFERENCES m_uri(id),
resourceRefTargetOid UUID,
resourceRefTargetType ObjectType,
resourceRefRelationId INTEGER REFERENCES m_uri(id),
intent TEXT,
kind ShadowKindType,
PRIMARY KEY (ownerOid, cid)
) INHERITS(m_container);

-- endregion
Expand Down Expand Up @@ -2115,4 +2110,4 @@ END $$;
-- This is important to avoid applying any change more than once.
-- Also update SqaleUtils.CURRENT_SCHEMA_CHANGE_NUMBER
-- repo/repo-sqale/src/main/java/com/evolveum/midpoint/repo/sqale/SqaleUtils.java
call apply_change(18, $$ SELECT 1 $$, true);
call apply_change(19, $$ SELECT 1 $$, true);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javax.sql.DataSource;

import com.evolveum.midpoint.repo.sqale.qmodel.task.QAffectedObjectsMapping;
import com.evolveum.midpoint.repo.sqale.qmodel.task.QAffectedResourceObjectsMapping;

import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -196,7 +195,6 @@ public SqaleRepoContext sqlRepoContext(
.register(SimulationResultType.COMPLEX_TYPE, QSimulationResultMapping.initSimulationResultMapping(repositoryContext))
.register(SimulationResultProcessedObjectType.COMPLEX_TYPE, QProcessedObjectMapping.initProcessedResultMapping(repositoryContext))
.register(MarkType.COMPLEX_TYPE, QMarkMapping.init(repositoryContext))
.register(QAffectedResourceObjectsMapping.init(repositoryContext))
.register(QAffectedObjectsMapping.init(repositoryContext))
.seal();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SqaleUtils {
*/
public static final String SCHEMA_AUDIT_CHANGE_NUMBER = "schemaAuditChangeNumber";

public static final int CURRENT_SCHEMA_CHANGE_NUMBER = 18;
public static final int CURRENT_SCHEMA_CHANGE_NUMBER = 19;

public static final int CURRENT_SCHEMA_AUDIT_CHANGE_NUMBER = 4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@

public class MAffectedObjects extends MContainer {

public Integer activityId;

// Object container
public MObjectType type;
public UUID archetypeRefTargetOid;
public MObjectType archetypeRefTargetType;
public Integer archetypeRefRelationId;

// ResourceObject container
public Integer objectClassId;
public UUID resourceRefTargetOid;
public MObjectType resourceRefTargetType;
public Integer resourceRefRelationId;
public String intent;
public String tag;
public ShadowKindType kind;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,51 @@ public class QAffectedObjects extends QContainer<MAffectedObjects, MTask> {
public static final ColumnMetadata OBJECT_TYPE =
ColumnMetadata.named("type").ofType(Types.OTHER).notNull();

public static final ColumnMetadata ACTIVITY_ID =
ColumnMetadata.named("activityId").ofType(Types.INTEGER);

public static final ColumnMetadata ARCHETYPE_REF_TARGET_OID =
ColumnMetadata.named("archetypeRefTargetOid").ofType(UuidPath.UUID_TYPE);
public static final ColumnMetadata ARCHETYPE_REF_TARGET_TYPE =
ColumnMetadata.named("archetypeRefTargetType").ofType(Types.OTHER);
public static final ColumnMetadata ARCHETYPE_REF_RELATION_ID =
ColumnMetadata.named("archetypeRefRelationId").ofType(Types.INTEGER);

public static final ColumnMetadata OBJECT_CLASS_ID =
ColumnMetadata.named("objectClassId").ofType(Types.INTEGER);
public static final ColumnMetadata RESOURCE_REF_TARGET_OID =
ColumnMetadata.named("resourceRefTargetOid").ofType(UuidPath.UUID_TYPE);
public static final ColumnMetadata RESOURCE_REF_TARGET_TYPE =
ColumnMetadata.named("resourceRefTargetType").ofType(Types.OTHER);
public static final ColumnMetadata RESOURCE_REF_RELATION_ID =
ColumnMetadata.named("resourceRefRelationId").ofType(Types.INTEGER);
public static final ColumnMetadata INTENT =
ColumnMetadata.named("intent").ofType(Types.VARCHAR);
public static final ColumnMetadata KIND =
ColumnMetadata.named("kind").ofType(Types.OTHER);

public final NumberPath<Integer> activityId =
createInteger("activityId", ACTIVITY_ID);

public final EnumPath<MObjectType> type = createEnum("type", MObjectType.class, OBJECT_TYPE);
public final UuidPath archetypeRefTargetOid =
createUuid("archetypeRefTargetOid", ARCHETYPE_REF_TARGET_OID);
public final EnumPath<MObjectType> archetypeRefTargetType =
createEnum("archetypeRefTargetType", MObjectType.class, ARCHETYPE_REF_TARGET_TYPE);
public final NumberPath<Integer> archetypeRefRelationId =
createInteger("archetypeRefRelationId", ARCHETYPE_REF_RELATION_ID);

public final NumberPath<Integer> objectClassId =
createInteger("objectClassId", OBJECT_CLASS_ID);
public final UuidPath resourceRefTargetOid =
createUuid("resourceRefTargetOid", RESOURCE_REF_TARGET_OID);
public final EnumPath<MObjectType> resourceRefTargetType =
createEnum("resourceRefTargetType", MObjectType.class, RESOURCE_REF_TARGET_TYPE);
public final NumberPath<Integer> resourceRefRelationId =
createInteger("resourceRefRelationId", RESOURCE_REF_RELATION_ID);
public final StringPath intent = createString("intent", INTENT);
public final EnumPath<ShadowKindType> kind = createEnum("kind", ShadowKindType.class, KIND);

public QAffectedObjects(String variable) {
this(variable, DEFAULT_SCHEMA_NAME, TABLE_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,30 @@
import com.evolveum.midpoint.repo.sqale.mapping.SqaleItemSqlMapper;
import com.evolveum.midpoint.repo.sqale.qmodel.common.QContainerMapping;
import com.evolveum.midpoint.repo.sqale.qmodel.object.MObjectType;
import com.evolveum.midpoint.repo.sqale.qmodel.resource.QResourceMapping;
import com.evolveum.midpoint.repo.sqale.qmodel.role.QArchetypeMapping;
import com.evolveum.midpoint.repo.sqlbase.JdbcSession;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivityAffectedObjectsType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.BasicObjectSetType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.BasicResourceObjectSetType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;

import com.querydsl.core.types.dsl.EnumPath;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;

import static com.evolveum.midpoint.xml.ns._public.common.common_3.BasicResourceObjectSetType.*;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.SimulationResultProcessedObjectType.F_TYPE;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.ActivityAffectedObjectsType.*;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.BasicObjectSetType.*;

public class QAffectedObjectsMapping extends QContainerMapping<BasicObjectSetType, QAffectedObjects, MAffectedObjects, MTask> {

public class QAffectedObjectsMapping extends QContainerMapping<ActivityAffectedObjectsType, QAffectedObjects, MAffectedObjects, MTask> {

public static final String DEFAULT_ALIAS_NAME = "tao";

Expand All @@ -42,19 +51,31 @@ public static QAffectedObjectsMapping init(@NotNull SqaleRepoContext repositoryC
}

protected QAffectedObjectsMapping(@NotNull SqaleRepoContext repositoryContext) {
super(QAffectedObjects.TABLE_NAME, DEFAULT_ALIAS_NAME, BasicObjectSetType.class, QAffectedObjects.class, repositoryContext);
super(QAffectedObjects.TABLE_NAME, DEFAULT_ALIAS_NAME, ActivityAffectedObjectsType.class, QAffectedObjects.class, repositoryContext);

addItemMapping(F_ACTIVITY_TYPE, uriMapper(q -> q.activityId));
Function<QAffectedObjects, EnumPath<MObjectType>> objectTypePath = q -> q.type;
addItemMapping(F_TYPE, new SqaleItemSqlMapper<>(
ctx -> new TypeQNameItemFilterProcessor(ctx, objectTypePath),
ctx -> new SinglePathItemDeltaProcessor<>(ctx, objectTypePath),
objectTypePath
));
addRefMapping(F_ARCHETYPE_REF,
q -> q.archetypeRefTargetOid,
q -> q.archetypeRefTargetType,
q -> q.archetypeRefRelationId,
QArchetypeMapping::getArchetypeMapping);
addNestedMapping(F_OBJECTS, BasicObjectSetType.class)
.addItemMapping(F_TYPE, new SqaleItemSqlMapper<>(
ctx -> new TypeQNameItemFilterProcessor(ctx, objectTypePath),
ctx -> new SinglePathItemDeltaProcessor<>(ctx, objectTypePath),
objectTypePath
))
.addRefMapping(F_ARCHETYPE_REF,
q -> q.archetypeRefTargetOid,
q -> q.archetypeRefTargetType,
q -> q.archetypeRefRelationId,
QArchetypeMapping::getArchetypeMapping);
addNestedMapping(F_RESOURCE_OBJECTS, BasicResourceObjectSetType.class)
.addItemMapping(F_OBJECTCLASS, uriMapper(q -> q.objectClassId))
.addRefMapping(F_RESOURCE_REF,
q -> q.resourceRefTargetOid,
q -> q.resourceRefTargetType,
q -> q.resourceRefRelationId,
QResourceMapping::get)
.addItemMapping(F_INTENT, stringMapper(q -> q.intent))
.addItemMapping(F_KIND, enumMapper(q -> q.kind));

}

public static QAffectedObjectsMapping get() {
Expand All @@ -79,15 +100,32 @@ protected QAffectedObjects newAliasInstance(String alias) {
}

@Override
public MAffectedObjects insert(BasicObjectSetType object, MTask ownerRow, JdbcSession jdbcSession) throws SchemaException {
var row = initRowObject(object, ownerRow);
setReference(object.getArchetypeRef(),
o -> row.archetypeRefTargetOid = o ,
t -> row.archetypeRefTargetType = t,
r -> row.archetypeRefRelationId = r
);
if (object.getType() != null) {
row.type = MObjectType.fromTypeQName(object.getType());
public MAffectedObjects insert(ActivityAffectedObjectsType activity, MTask ownerRow, JdbcSession jdbcSession) throws SchemaException {
var row = initRowObject(activity, ownerRow);

row.activityId = processCacheableUri(activity.getActivityType());

var object = activity.getObjects();
if (object != null) {
setReference(object.getArchetypeRef(),
o -> row.archetypeRefTargetOid = o,
t -> row.archetypeRefTargetType = t,
r -> row.archetypeRefRelationId = r
);
if (object.getType() != null) {
row.type = MObjectType.fromTypeQName(object.getType());
}
}
var resource = activity.getResourceObjects();
if (resource != null) {
setReference(resource.getResourceRef(),
o -> row.resourceRefTargetOid = o ,
t -> row.resourceRefTargetType = t,
r -> row.resourceRefRelationId = r
);
row.kind = resource.getKind();
row.intent = resource.getIntent();
row.objectClassId = processCacheableUri(resource.getObjectclass());
}
insert(row, jdbcSession);
return row;
Expand Down

0 comments on commit 973a7d8

Please sign in to comment.