-
Notifications
You must be signed in to change notification settings - Fork 24
feat(policy): add obligation tables #2532
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
Merged
+192
−0
Merged
Changes from 18 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
6911ac8
obligation_definitions table
alkalescent 2f0f405
implicit index
alkalescent d1da961
obligation_values_standard table
alkalescent de7106f
obligation fulfillers
alkalescent 1b88775
clean up and add comments
alkalescent a7eb276
obligation triggers table
alkalescent 3204a33
add time cols to defs and vals
alkalescent 0352bf1
add triggers
alkalescent c1095a8
modify function
alkalescent f2e15fa
two functions
alkalescent eb9ff65
adding optional metadata
alkalescent fb0afea
no errors so far
alkalescent aaac715
good so far
alkalescent ec04ccb
still ok
alkalescent e1ee54a
fx working
alkalescent 8dace64
working
alkalescent fa55f2a
clean up
alkalescent 08c3f24
update docs
alkalescent 965252a
remove extraneous uniqueness
alkalescent de79eca
add uniqueness constraint
alkalescent 3d513dd
update erd
alkalescent 0f3510d
remove table and clean up function
alkalescent 7d9d808
on cascade delete
alkalescent 7f63aae
update erd
alkalescent c90df0a
move function drop back to down
alkalescent 8267125
move drop fx to up
alkalescent 798480e
gemini suggestions
alkalescent 9d88bb4
merge fxs
alkalescent 212b907
remove functions
alkalescent 5d8daf6
doc
alkalescent c9bb4ad
Merge branch 'main' of github.com:opentdf/platform into feature/oblig…
alkalescent 1a62807
drop tables in reverse order
alkalescent 6a9c252
move constraints to end
alkalescent 59718e9
Merge branch 'main' of github.com:opentdf/platform into feature/oblig…
alkalescent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
service/policy/db/migrations/20250703000000_obligations.md
This file contains hidden or 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,75 @@ | ||
| [ADR for Obligations](https://github.com/opentdf/platform/issues/1933) | ||
|
|
||
| This migration adds the obligation tables for definitions, values, triggers, fulfillers, and action attribute value relationships. | ||
|
|
||
| ```mermaid | ||
| erDiagram | ||
| attribute_namespaces ||--o{ obligation_definitions : "belongs_to" | ||
| obligation_definitions ||--o{ obligation_values_standard : "has_many" | ||
| obligation_values_standard ||--o{ obligation_triggers : "has_many" | ||
| obligation_values_standard ||--o{ obligation_fulfillers : "has_many" | ||
| obligation_values_standard ||--o{ obligation_action_attribute_values : "has_many" | ||
| attribute_values ||--o{ obligation_triggers : "triggers" | ||
| actions ||--o{ obligation_action_attribute_values : "requires" | ||
alkalescent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| attribute_values ||--o{ obligation_action_attribute_values : "requires" | ||
|
|
||
| attribute_namespaces { | ||
| UUID id PK | ||
| string name | ||
| } | ||
|
|
||
| obligation_definitions { | ||
| UUID id PK | ||
| UUID namespace_id FK | ||
| string name | ||
| jsonb metadata | ||
| timestamp created_at | ||
| timestamp updated_at | ||
| } | ||
|
|
||
| obligation_values_standard { | ||
| UUID id PK | ||
| UUID obligation_definition_id FK | ||
| string value | ||
| jsonb metadata | ||
| timestamp created_at | ||
| timestamp updated_at | ||
| } | ||
|
|
||
| obligation_triggers { | ||
| UUID id PK | ||
| UUID attribute_value_id FK | ||
| UUID obligation_value_id FK | ||
| jsonb metadata | ||
| timestamp created_at | ||
| timestamp updated_at | ||
| } | ||
|
|
||
| obligation_fulfillers { | ||
| UUID id PK | ||
| UUID obligation_value_id FK | ||
| jsonb conditionals | ||
| jsonb metadata | ||
| timestamp created_at | ||
| timestamp updated_at | ||
| } | ||
|
|
||
| obligation_action_attribute_values { | ||
| UUID id PK | ||
| UUID obligation_value_id FK | ||
| UUID action_id FK | ||
| UUID attribute_value_id FK | ||
| timestamp created_at | ||
| timestamp updated_at | ||
| } | ||
|
|
||
| attribute_values { | ||
| UUID id PK | ||
| string value | ||
| } | ||
|
|
||
| actions { | ||
| UUID id PK | ||
| string name | ||
| } | ||
| ``` | ||
115 changes: 115 additions & 0 deletions
115
service/policy/db/migrations/20250703000000_obligations.sql
This file contains hidden or 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,115 @@ | ||
| -- +goose Up | ||
| -- +goose StatementBegin | ||
|
|
||
| CREATE TABLE IF NOT EXISTS obligation_definitions | ||
| ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| namespace_id UUID NOT NULL REFERENCES attribute_namespaces(id), | ||
| -- name is a unique identifier for the obligation definition within the namespace | ||
| name VARCHAR NOT NULL UNIQUE, | ||
alkalescent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| -- implicit index on unique (namespace_id, name) combo | ||
| -- index name: obligation_definitions_namespace_id_name_key | ||
| UNIQUE (namespace_id, name) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS obligation_values_standard | ||
| ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| obligation_definition_id UUID NOT NULL REFERENCES obligation_definitions(id), | ||
| -- value is a unique identifier for the obligation value within the definition | ||
| value VARCHAR NOT NULL UNIQUE, | ||
alkalescent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| -- implicit index on unique (obligation_definition_id, value) combo | ||
| -- index name: obligation_values_standard_obligation_definition_id_value_key | ||
| UNIQUE (obligation_definition_id, value) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS obligation_triggers | ||
| ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| attribute_value_id UUID NOT NULL REFERENCES attribute_values(id), | ||
| obligation_value_id UUID NOT NULL REFERENCES obligation_values_standard(id) | ||
| ); | ||
alkalescent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| CREATE TABLE IF NOT EXISTS obligation_fulfillers | ||
| ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| obligation_value_id UUID NOT NULL REFERENCES obligation_values_standard(id), | ||
| conditionals JSONB | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS obligation_action_attribute_values | ||
| ( | ||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
| obligation_value_id UUID NOT NULL REFERENCES obligation_values_standard(id), | ||
| action_id UUID NOT NULL REFERENCES actions(id), | ||
| attribute_value_id UUID NOT NULL REFERENCES attribute_values(id), | ||
| UNIQUE(obligation_value_id, action_id, attribute_value_id) | ||
| ); | ||
|
|
||
| CREATE OR REPLACE FUNCTION get_obligation_tables() | ||
ryanulit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| RETURNS text[] AS $$ | ||
| BEGIN | ||
| RETURN ARRAY['obligation_definitions', 'obligation_values_standard', | ||
| 'obligation_triggers', 'obligation_fulfillers', | ||
| 'obligation_action_attribute_values']; | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| CREATE OR REPLACE FUNCTION standardize_table(table_name regclass) | ||
| RETURNS void AS $$ | ||
| BEGIN | ||
| -- Add standard columns to the table | ||
| EXECUTE FORMAT(' | ||
| ALTER TABLE %I | ||
| ADD COLUMN metadata JSONB, | ||
| ADD COLUMN created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| ADD COLUMN updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
| ', table_name); | ||
|
|
||
| -- Create trigger for updating updated_at column | ||
| EXECUTE FORMAT(' | ||
| CREATE TRIGGER %I_updated_at | ||
| BEFORE UPDATE ON %I | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION update_updated_at() | ||
| ', table_name, table_name); | ||
alkalescent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| CREATE OR REPLACE FUNCTION standardize_tables(tables text[]) | ||
| RETURNS void AS $$ | ||
| DECLARE table_name text; | ||
| BEGIN | ||
| FOREACH table_name IN ARRAY tables | ||
| LOOP | ||
| PERFORM standardize_table(table_name::regclass); | ||
| END LOOP; | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| CREATE OR REPLACE FUNCTION drop_tables(tables text[]) | ||
| RETURNS void AS $$ | ||
| DECLARE table_name text; | ||
| BEGIN | ||
| FOREACH table_name IN ARRAY tables | ||
| LOOP | ||
| EXECUTE FORMAT('DROP TABLE IF EXISTS %I', table_name); | ||
| END LOOP; | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| SELECT standardize_tables(get_obligation_tables()); | ||
| ALTER TABLE obligation_action_attribute_values DROP COLUMN IF EXISTS metadata; | ||
alkalescent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| -- +goose StatementEnd | ||
|
|
||
| -- +goose Down | ||
| -- +goose StatementBegin | ||
|
|
||
| SELECT drop_tables(get_obligation_tables()); | ||
| DROP FUNCTION IF EXISTS get_obligation_tables; | ||
| DROP FUNCTION IF EXISTS standardize_table; | ||
| DROP FUNCTION IF EXISTS standardize_tables; | ||
| DROP FUNCTION IF EXISTS drop_tables; | ||
|
|
||
| -- +goose StatementEnd | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.