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

Chore/1876 rebase pending revisions #1882

Merged
merged 51 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
52baf12
chore: add jsonb minus
mikevespi Oct 30, 2023
39b47cd
chore: rework setup for commit_form_change_internal
mikevespi Oct 30, 2023
921f579
chore: pass correct arguments to commit form change internal
mikevespi Oct 31, 2023
983ad50
chore: rebase changes made in pending form change on top of the commi…
mikevespi Oct 31, 2023
34c77b9
chore: handle the merging of form changes when another project revisi…
mikevespi Nov 9, 2023
9e5ee2f
test: initial tests for concurrent revisions merging
mikevespi Nov 17, 2023
52e2301
test: test the updates of the simple forms in commit_form_change_inte…
mikevespi Nov 18, 2023
6a1bcad
chore: fix revert sqitch function
mikevespi Nov 20, 2023
371807f
chore: fix signature in rever
mikevespi Nov 21, 2023
ac8c74d
chore: explicitly drop the old function signature
mikevespi Nov 21, 2023
e83dfee
test: add create and mutual update tests
mikevespi Nov 21, 2023
29afc50
chore: explicitly drop other function signature in revert file
mikevespi Nov 21, 2023
d4d974e
chore: fix db deploy
mikevespi Nov 21, 2023
d8f52b0
chore: switch test check from form change to project table, fc tested…
mikevespi Nov 21, 2023
bbddafc
test: explicit argument list in function should exist test
mikevespi Nov 21, 2023
e461f80
test: typecast values in test resturn
mikevespi Nov 22, 2023
1723f74
chore: fix quotes in pg test
mikevespi Nov 22, 2023
c51a4c3
chore: handle the new commit_form_change_internal args in commit_form…
mikevespi Nov 22, 2023
1b0669b
test: test the case when the commiting fc has made changes but the pe…
mikevespi Nov 22, 2023
8527c05
test: test that attachments are handled correctly when concurrent rev…
mikevespi Nov 28, 2023
c23fe07
chore: set the previous_form_change_id on pending form changes
mikevespi Nov 30, 2023
a16a838
test: check previous_form_change_id and form_data_record_id on create…
mikevespi Nov 30, 2023
5739bdf
test: add separate test for checking create on contacts
mikevespi Dec 7, 2023
df054fb
chore: handle the various cases for creating form changes in concurre…
mikevespi Dec 7, 2023
f26b393
chore: remove unnecessary comment
mikevespi Dec 7, 2023
d7f47ed
test: cleanup assertions and add testing for creating a funding agree…
mikevespi Dec 7, 2023
8ecb312
chore: cast incremented indices in form data back to jsonb
mikevespi Dec 7, 2023
34cada4
test: prep for the restof the create tests
mikevespi Dec 7, 2023
6ad612d
test: add contacts to use as managers
mikevespi Dec 10, 2023
e1152b0
docs: add start of concurrent revision handling documentation
mikevespi Mar 6, 2024
d99a18e
docs: update concurrent revision doc
mikevespi Mar 7, 2024
68b0b93
chore: address PR comments
mikevespi Mar 13, 2024
ee1108e
chore: set the operation of pending form changes to update when commi…
mikevespi Mar 13, 2024
15e9846
test: add tests for adding project_managers with pending revision
mikevespi Mar 13, 2024
fd25539
test: tests for adding reporting requirements in both committing and …
mikevespi Mar 13, 2024
90550cf
chore: fix update previous_form_change_id scope in commit_form_change…
mikevespi Mar 13, 2024
ef243d8
chore: remove redundant limit 1
mikevespi Mar 14, 2024
f9d8947
chore: added comments for clarity
mikevespi Mar 14, 2024
bf476bf
chore: updated function comment to be more accurate
mikevespi Mar 14, 2024
c728566
test: update the milestone concurrent revision tests
mikevespi Mar 15, 2024
bb2240f
test: move concurrent revision tests to a separate file and add conta…
mikevespi Mar 18, 2024
3a96df5
test: add directory of tests related to concurrent revisions and upda…
mikevespi Mar 19, 2024
c69066b
chore: when committing archives something that pending has updated ch…
mikevespi Mar 20, 2024
0a7101d
chore: remove test file that whose tests have been distributed to mor…
mikevespi Mar 20, 2024
85ce6b4
chore: clean out unrelated items from test
mikevespi Mar 22, 2024
5c1c620
chore: cleanup commit_form_change_internal
mikevespi Mar 22, 2024
3a592c8
chore: fix contact index clashes from happening when both committing …
mikevespi Mar 23, 2024
94b1142
test: fix contact scenarios in concurrent revision test
mikevespi Mar 23, 2024
59d154d
test: add emission_intensity and funding_parameter tests
mikevespi Mar 23, 2024
f15e533
test: confirm form_data_record_ids are being set in concurrent create…
mikevespi Mar 23, 2024
1a2ac24
test: add tests for concurrent revisions that discard form changes
mikevespi Mar 24, 2024
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
34 changes: 33 additions & 1 deletion app/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -50871,6 +50871,32 @@ input JSONFilter {
notIn: [JSON!]
}

"""All input for the `jsonbMinus` mutation."""
input JsonbMinusInput {
"""
An arbitrary string value with no semantic meaning. Will be included in the
payload verbatim. May be used to track mutations by the client.
"""
clientMutationId: String
minuend: JSON!
subtrahend: JSON!
}

"""The output of our `jsonbMinus` mutation."""
type JsonbMinusPayload {
"""
The exact same `clientMutationId` that was provided in the mutation input,
unchanged and unused. May be used by a client to track mutations.
"""
clientMutationId: String
json: JSON

"""
Our root query field type. Allows us to run any query from our mutation payload.
"""
query: Query
}

type KeycloakJwt {
acr: String
aud: String
Expand Down Expand Up @@ -52297,6 +52323,12 @@ type Mutation {
"""
input: GenerateQuarterlyReportsInput!
): GenerateQuarterlyReportsPayload
jsonbMinus(
"""
The exclusive input argument for this mutation. An object type, make sure to see documentation for this object’s fields.
"""
input: JsonbMinusInput!
): JsonbMinusPayload
stageDirtyFormChanges(
"""
The exclusive input argument for this mutation. An object type, make sure to see documentation for this object’s fields.
Expand Down Expand Up @@ -66928,7 +66960,7 @@ type Query implements Node {
paymentByRowId(rowId: Int!): Payment

"""
returns a form_change for a table in the pending state for the current user, i.e. allows to resume the creation of any table row
returns list of key-value pairs present in the first argument but not the second argument
"""
pendingNewFormChangeForTable(tableName: String!): FormChange

Expand Down
125 changes: 124 additions & 1 deletion app/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -171233,6 +171233,102 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "JsonbMinusInput",
"description": "All input for the `jsonbMinus` mutation.",
"fields": null,
"inputFields": [
{
"name": "clientMutationId",
"description": "An arbitrary string value with no semantic meaning. Will be included in the\npayload verbatim. May be used to track mutations by the client.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
},
{
"name": "minuend",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "JSON",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "subtrahend",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "JSON",
"ofType": null
}
},
"defaultValue": null
}
],
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "JsonbMinusPayload",
"description": "The output of our `jsonbMinus` mutation.",
"fields": [
{
"name": "clientMutationId",
"description": "The exact same `clientMutationId` that was provided in the mutation input,\nunchanged and unused. May be used by a client to track mutations.",
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "json",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "JSON",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "query",
"description": "Our root query field type. Allows us to run any query from our mutation payload.",
"args": [],
"type": {
"kind": "OBJECT",
"name": "Query",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "KeycloakJwt",
Expand Down Expand Up @@ -176610,6 +176706,33 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "jsonbMinus",
"description": null,
"args": [
{
"name": "input",
"description": "The exclusive input argument for this mutation. An object type, make sure to see documentation for this object’s fields.",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "JsonbMinusInput",
"ofType": null
}
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "JsonbMinusPayload",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "stageDirtyFormChanges",
"description": null,
Expand Down Expand Up @@ -229591,7 +229714,7 @@
},
{
"name": "pendingNewFormChangeForTable",
"description": "returns a form_change for a table in the pending state for the current user, i.e. allows to resume the creation of any table row",
"description": "returns list of key-value pairs present in the first argument but not the second argument",
"args": [
{
"name": "tableName",
Expand Down
41 changes: 41 additions & 0 deletions docs/concurrentRevisionHandling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Handling of Concurrent Revisions

The purpose of this document is to outline how we allow concurrent revisions to be made to a proeject.

## Introduction

By "concurrent revisions" we are referring to the ability for two discrete sets of changes to a project to exist at the same time. In CIF, we limit the number of concurrent revisions on any given project to be two: one "Amendment", and one "General Revision". While this was never intended to be a functionality of CIF, a new user flow was introduced that required it. As such, the approach taken is more of an adaptaion of the existing CIF architecture (which is described in `docs/dbRecordsHistory.md`) than an architecture designed to handle concurrency. The result is that any divergence from the original architecture pre-concureency also appears in the concurrent behaviour. For example, project contacts are handled using a different pattern than the project form in the original architecture, therfore they behave differently from the genral pattern in the concurrent approach as well.

### Terminology

There are three terms we need to use to identify the three `form_change` records in question: "committing", "pending", and "original parent". I'll use the more common scenario to outline the terminology used throughout this document.
An Amendment is opened on a project, and left open while it is being negotiated. While it is open, a General Revision is opened on that same project, a small change is made, and the revision is committed. The point in time of the General Revision being committed is where the terminology gets its roots. In this example, the General Revision is **committing**, the still-open amendment is **pending**, and the parent revision of the Amendment is the **original parent**.

## Approach

A solution that would allow us to handle concurrency without user input on conflict resolution was needed. To achieve this, the approach taken is comparable to a git rebase. When committing and pending are in conflict, the changes made in pending are applied on top of the committing form change, as if the committing `form_change` were the original parent of the pending `form_change`. While users commit on a `project_revision` level, the change propogates down to the `form_change` level, so when we're talking about this here it is at the `form_change` granularity, and the heart it takes place in the function `cif.commit_form_change_internal`.

One of the ways our various forms can be categorized would be:

- forms a project can have at most one of (`funding_parameter_EP`, `funding_parameter_IA`, `emission_intensity`, `project_summary_report`)
- 'project_contact' are either primary or secondary, and have a `contactIndex`
- 'project_manager' are categorized by `projectManagerLabelId`
- 'reporting_requirement' have a `reportingRequirementIndex` based on the `json_schema_name`

Form changes can have an operation of `create`, `update`, or `archive`, each of which need to be handled for all of the above categories. This results in several unique cases, which have been explained case-by-case using in-line in the `commit_form_change_internal` where they have more context.

After each of the following cases, the `previous_form_change_id` of the pending `form_change` is set to be the id of the committing `form_change`, which leaves every form change with a `previous_form_change_id` of the **last commit** corresponding `form_change`, while preserving the option of a full history by maintaining accurate `created_at`, `updated_at`, and `archived_at` values for all `form_change`.

### Create (the general approach)

If the committing project revision creates a form change that does not exist in the pending revision, for example adding a milestone, then the form needs to be created in the pending revision. In cases such as contacts which have a `contactIndex` associated with them, the index needs to be determined by the existing indices in the pending revision. This will allow the indices to stay sequential if other items were added or removed in the pending revision.

### Update

1. If the committing form change contains the same data as the pending's original parent, then no change to the pending data is needed.
2. If the committing and pending form changes both have changes from the pending form change's parent, then set the pending form change's new_form_data to be the committing form change's, plus the changes made in the penging form change. The result is what would the data would have been if the pending form change had the committing as it's parent, similar to a git rebase.
3. If the pending form change hasn't made any changes since its creation, but the committing form change has, set the pending form change's new_form_data to be the committing form change's, as it is the latest information.

### Archive (the general approach)

If the committing `form_change` is being archived, the pending form change can simply be deleted as it never would have been created in the first place had the committing project revision been the original parent of the pending revision.
31 changes: 31 additions & 0 deletions schema/deploy/functions/jsonb_minus.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Deploy cif:functions/jsonb_minus to pg

begin;
-- A note on the functionality:
-- If a key is present in the subtrahend but not the minuend, it will not appear in the result set.
-- {"a": 1, "b": 3} - {"a": 1, "b": 2, "c": 3} = {"b": 3}
-- If however a key is present in the minuend but not the subtrahend, it will appear in the result set with its value.
-- {"a": 1, "b": 3, "c": 3} - {"a": 1, "b": 2} = {"b": 3, "c": 3}

-- This behaviour fits our needs at the time of writing this, so the additional complexity of handling the other cases is not needed.


create or replace function cif.jsonb_minus(minuend jsonb, subtrahend jsonb)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be fun for this one (but just fun, not that we have to do it here) to declare the - operator for the jsonb type with this function
https://www.postgresql.org/docs/current/sql-createoperator.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would! This is cool I didn't know that you could add them like this

returns jsonb as
$$
declare
difference jsonb;
begin
select jsonb_object_agg(key, value) into strict difference
from (
select * from jsonb_each($1)
except select * from jsonb_each($2)
) as temp;

return difference;
end
$$ language plpgsql volatile;

comment on function cif.pending_new_form_change_for_table(text) is
'returns list of key-value pairs present in the first argument but not the second argument';
commit;
8 changes: 7 additions & 1 deletion schema/deploy/mutations/commit_form_change.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ begin
validation_errors = coalesce(form_change_patch.validation_errors, validation_errors)
where id=row_id;

return (select cif_private.commit_form_change_internal((select row(form_change.*)::cif.form_change from cif.form_change where id = row_id)));
return (select cif_private.commit_form_change_internal(
(select row(form_change.*)::cif.form_change from cif.form_change where id = row_id),
-- This is guaranteed to be a single row as we have unique inidices on pending general revision and pending amendment
(select id from cif.project_revision
where project_id=(select project_id from cif.project_revision where id = form_change_patch.project_revision_id)
and change_status = 'pending' and id != form_change_patch.project_revision_id)
));
end;
$$ language plpgsql volatile;

Expand Down
23 changes: 23 additions & 0 deletions schema/deploy/mutations/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Deploy cif:mutations/commit_form_change to pg
-- requires: tables/form_change

begin;

create or replace function cif.commit_form_change(row_id int, form_change_patch cif.form_change)
returns cif.form_change as $$
begin

update cif.form_change set
new_form_data = coalesce(form_change_patch.new_form_data, new_form_data),
validation_errors = coalesce(form_change_patch.validation_errors, validation_errors)
where id=row_id;

return (select cif_private.commit_form_change_internal((select row(form_change.*)::cif.form_change from cif.form_change where id = row_id)));
end;
$$ language plpgsql volatile;

grant execute on function cif.commit_form_change to cif_internal, cif_external, cif_admin;

comment on function cif.commit_form_change is 'Custom mutation to commit a form_change record via the API. Only used for records that are independent of a project such as the lists of contacts and operators.';

commit;
Loading
Loading