-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "feat: compute hash at core level"
This reverts commit 77d780e.
- Loading branch information
Showing
6 changed files
with
109 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
39 changes: 39 additions & 0 deletions
39
internal/storage/bucket/migrations/30-logs-hash-in-database.sql
This file contains 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,39 @@ | ||
create function "{{.Bucket}}".set_log_hash() | ||
returns trigger | ||
security definer | ||
language plpgsql | ||
as | ||
$$ | ||
declare | ||
previousHash bytea; | ||
marshalledAsJSON varchar; | ||
begin | ||
select hash into previousHash | ||
from "{{.Bucket}}".logs | ||
where ledger = new.ledger | ||
order by seq desc | ||
limit 1; | ||
|
||
-- select only fields participating in the hash on the backend and format json representation the same way | ||
select '{' || | ||
'"type":"' || new.type || '",' || | ||
'"data":' || encode(new.memento, 'escape') || ',' || | ||
'"date":"' || (to_json(new.date::timestamp)#>>'{}') || 'Z",' || | ||
'"idempotencyKey":"' || coalesce(new.idempotency_key, '') || '",' || | ||
'"id":0,' || | ||
'"hash":null' || | ||
'}' into marshalledAsJSON; | ||
|
||
new.hash = ( | ||
select public.digest( | ||
case | ||
when previousHash is null | ||
then marshalledAsJSON::bytea | ||
else '"' || encode(previousHash::bytea, 'base64')::bytea || E'"\n' || convert_to(marshalledAsJSON, 'LATIN1')::bytea | ||
end || E'\n', 'sha256'::text | ||
) | ||
); | ||
|
||
return new; | ||
end; | ||
$$; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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