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

AJ-2008: json-based entities POC #3026

Draft
wants to merge 26 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@
<include file="changesets/20240418_workspace_monitor_args.xml" relativeToChangelogFile="true"/>
<include file="changesets/20240723_workspace_settings.xml" relativeToChangelogFile="true"/>
<include file="changesets/20240820_submission_cost_cap_threshold.xml" relativeToChangelogFile="true"/>
<include file="changesets/20240910_entity_json_support.xml" relativeToChangelogFile="true"/>
<include file="changesets/20240830_workflow_cost.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog logicalFilePath="dummy" xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.29.xsd">

<!-- add the ENTITY.attributes JSON column -->
<changeSet logicalFilePath="dummy" author="davidan" id="add_attributes_col_to_entity">
<addColumn tableName="ENTITY">
<column name="attributes" type="JSON"/>
</addColumn>
</changeSet>

<!-- create the ENTITY_KEYS table -->
<changeSet logicalFilePath="dummy" author="davidan" id="create_table_entity_keys">
<createTable tableName="ENTITY_KEYS">
<column name="id" type="BIGINT UNSIGNED">
<constraints primaryKey="true"
nullable="false"
referencedTableName="ENTITY" referencedColumnNames="id"
foreignKeyName="fk_entity_keys_entity_id" deleteCascade="true"/>
</column>
<column name="workspace_id" type="BINARY(16)">
<constraints nullable="false"
referencedTableName="WORKSPACE" referencedColumnNames="id"
foreignKeyName="fk_entity_keys_workspace_id" deleteCascade="true"/>
</column>
<column name="entity_type" type="VARCHAR(254)">
<constraints nullable="false"/>
</column>
<column name="attribute_keys" type="JSON">
<constraints nullable="false"/>
</column>
<column name="last_updated" type="TIMESTAMP(3)">
<constraints nullable="false"/>
</column>
</createTable>
<createIndex indexName="idx_entity_keys_workspace_and_entity_type" tableName="ENTITY_KEYS" unique="false">
<column name="workspace_id"/>
<column name="entity_type"/>
</createIndex>
<createIndex indexName="idx_entity_keys_last_updated_desc" tableName="ENTITY_KEYS" unique="false">
<column name="last_updated" descending="true"/>
</createIndex>
</changeSet>

<!-- create the ENTITY triggers to populate ENTITY_KEYS -->
<changeSet logicalFilePath="dummy" author="davidan" id="entity_keys_triggers">
<sql stripComments="true" endDelimiter="~">
DROP TRIGGER IF EXISTS after_entity_insert ~
DROP TRIGGER IF EXISTS after_entity_update ~

CREATE TRIGGER after_entity_insert AFTER INSERT ON ENTITY
FOR EACH ROW
if new.attributes is not null then
INSERT INTO ENTITY_KEYS
(id, workspace_id, entity_type, attribute_keys, last_updated)
VALUES
(new.id, new.workspace_id, new.entity_type, JSON_KEYS(new.attributes), now(3));
end if ~

CREATE TRIGGER after_entity_update AFTER UPDATE ON ENTITY
FOR EACH ROW
BEGIN
-- is this row soft-deleted?
if old.deleted = 0 and new.deleted = 1 then
DELETE FROM ENTITY_KEYS WHERE id = new.id;
elseif new.attributes is not null then
-- compare old keys to new keys; update the ENTITY_KEYS table only if they are different
set @new_keys := JSON_KEYS(new.attributes);
set @old_keys := JSON_KEYS(old.attributes);
if JSON_LENGTH(@new_keys) != JSON_LENGTH(@old_keys) OR JSON_CONTAINS(@new_keys, @old_keys) = 0 then
UPDATE ENTITY_KEYS SET attribute_keys=@new_keys, last_updated=now(3)
WHERE id = new.id;
end if;
end if;
END ~
</sql>
<rollback>
DROP TRIGGER IF EXISTS after_entity_insert;
DROP TRIGGER IF EXISTS after_entity_update;
</rollback>
</changeSet>

<!-- create the ENTITY_REFS table -->
<changeSet logicalFilePath="dummy" author="davidan" id="create_table_entity_refs">
<createTable tableName="ENTITY_REFS">
<column name="from_id" type="BIGINT UNSIGNED">
<constraints nullable="false" />
</column>
<column name="to_id" type="BIGINT UNSIGNED">
<constraints nullable="false" />
</column>
</createTable>
<!-- index for lookups against to_id -->
<createIndex tableName="ENTITY_REFS" indexName="idx_to">
<column name="to_id" />
</createIndex>
<!-- unique constraint for from+to, which covers lookups against from_id -->
<addUniqueConstraint columnNames="from_id, to_id" constraintName="unq_from_to" tableName="ENTITY_REFS"/>
</changeSet>




</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait DataAccess
with RawlsBillingProjectComponent
with WorkspaceComponent
with EntityComponent
with JsonEntityComponent
with AttributeComponent
with MethodConfigurationComponent
with SubmissionComponent
Expand Down
Loading
Loading