Skip to content
Open
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions pgledger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ CREATE TABLE pgledger_entries (
CREATE INDEX ON pgledger_entries(account_id);
CREATE INDEX ON pgledger_entries(transfer_id);

-- It's recommended to make pgledger_entries an append-only table
-- REVOKE UPDATE, DELETE, INSERT ON pgledger_entries FROM PUBLIC;

Choose a reason for hiding this comment

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

Minor nit-pick only. For a full hardening, these should never be granted to PUBLIC. They should only be granted as necessary.

-- GRANT INSERT ON pgledger_entries TO app_role; -- change to the role your code uses

-- CREATE OR REPLACE FUNCTION prevent_mutation_on_entries()
-- RETURNS trigger AS $$
-- BEGIN
-- RAISE EXCEPTION

Choose a reason for hiding this comment

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

By always raising the exception, you prevent the option of having an admin that can update the row. Perhaps it would be useful to catch a missing permission error, and then raise this exception.

Copy link

Choose a reason for hiding this comment

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

Nobody should ever update any entries. If the admin wants to make a change, they must append a new entry instead

-- 'pgledger_entries is immutable – % not allowed', TG_OP;
-- END;
-- $$ LANGUAGE plpgsql;
-- CREATE TRIGGER pgledger_entries_nochange
-- BEFORE UPDATE OR DELETE ON pgledger_entries
-- FOR EACH ROW EXECUTE FUNCTION prevent_mutation_on_entries();

Choose a reason for hiding this comment

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

Is there any particular reason why you choose the non-default "FOR EACH ROW" here? In my opinion it should be enough to fire it once per statement. If you try to UPDATE multiple rows in a single statement, you will unnecessarily raise many exceptions instead of only one.


CREATE OR REPLACE FUNCTION pgledger_create_account(
name_param TEXT,
currency_param TEXT,
Expand Down