Add database support for event amend operations.

[?]
Oct 15, 2016, 1:19 AM
3QVT6MA6I2CILQH3LUZABS4YQ7MN6CNRYTDRVS376OOHTPLYTFJAC

Dependencies

  • [2] NEDDHXUK Reformat via stylish-haskell
  • [3] 2WOOGXDH Use dbmigrations to manage database state.
  • [4] GCVQD44V Create amends endpoint, switch to UUID primary keys
  • [5] 7KZP4RHZ Switch from Data.Time to Data.Thyme
  • [6] A6HKMINB Attempting to improve JSON handling.
  • [7] QMRKFEPG Refactor QDB to use a free monad algebra instead.
  • [8] IZEVQF62 Work in progress replacing sqlite with postgres.

Change contents

  • replacement in lib/Aftok/Database/PostgreSQL.hs at line 159
    [4.608][2.2458:2529](),[2.2529][4.680:840](),[4.680][4.680:840](),[4.840][2.2530:2551](),[2.2551][4.862:974](),[4.862][4.862:974]()
    dbEval (CreateEvent (ProjectId pid) (UserId uid) (LogEntry a e m)) =
    pinsert EventId
    "INSERT INTO work_events (project_id, user_id, btc_addr, event_type, event_time, event_metadata) \
    \VALUES (?, ?, ?, ?, ?, ?) \
    \RETURNING id"
    ( pid, uid
    , a ^. _BtcAddr
    , eventName e
    , fromThyme $ e ^. eventTime
    , m
    )
    [4.608]
    [4.1807]
    dbEval (CreateEvent (ProjectId pid) (UserId uid) (LogEntry c e m)) =
    case c of
    CreditToAddress addr ->
    pinsert EventId
    "INSERT INTO work_events \
    \(project_id, user_id, credit_to_btc_addr, event_type, event_time, event_metadata) \
    \VALUES (?, ?, ?, ?, ?, ?) \
    \RETURNING id"
    ( pid, uid, addr ^. _BtcAddr, eventName e, fromThyme $ e ^. eventTime, m)
    CreditToProject pid ->
    pinsert EventId
    "INSERT INTO work_events \
    \(project_id, user_id, credit_to_project_id, event_type, event_time, event_metadata) \
    \VALUES (?, ?, ?, ?, ?, ?) \
    \RETURNING id"
    ( pid, uid, pid ^. _ProjectId, eventName e, fromThyme $ e ^. eventTime, m)
    CreditToUser uid ->
    pinsert EventId
    "INSERT INTO work_events \
    \(project_id, user_id, credit_to_user_id, event_type, event_time, event_metadata) \
    \VALUES (?, ?, ?, ?, ?, ?) \
    \RETURNING id"
    ( pid, uid, pid ^. _UserId, eventName e, fromThyme $ e ^. eventTime, m)
  • replacement in lib/Aftok/Database/PostgreSQL.hs at line 209
    [4.2116][4.2116:2221]()
    "INSERT INTO event_time_amendments (event_id, mod_time, event_time) VALUES (?, ?, ?) RETURNING id"
    [4.2116]
    [4.2221]
    "INSERT INTO event_time_amendments \
    \(event_id, amended_at, event_time) \
    \VALUES (?, ?, ?) RETURNING id"
  • replacement in lib/Aftok/Database/PostgreSQL.hs at line 214
    [4.1952][2.2840:2902](),[2.2902][4.2340:2527](),[4.2340][4.2340:2527]()
    dbEval (AmendEvent (EventId eid) (AddressChange mt addr)) =
    pinsert AmendmentId
    "INSERT INTO event_addr_amendments (event_id, mod_time, btc_addr) VALUES (?, ?, ?) RETURNING id"
    ( eid, fromThyme $ mt ^. _ModTime, addr ^. _BtcAddr )
    [4.1952]
    [2.2903]
    dbEval (AmendEvent (EventId eid) (CreditToChange mt creditTo)) =
    case creditTo of
    CreditToAddress addr ->
    pinsert AmendmentId
    "INSERT INTO event_credit_to_amendments \
    \(event_id, amended_at, credit_to_type, credit_to_btc_addr) \
    \VALUES (?, ?, ?, ?) RETURNING id"
    ( eid, fromThyme $ mt ^. _ModTime, "credit_to_address", addr ^. _BtcAddr )
  • edit in lib/Aftok/Database/PostgreSQL.hs at line 223
    [2.2904]
    [2.2904]
    CreditToProject pid ->
    pinsert AmendmentId
    "INSERT INTO event_credit_to_amendments \
    \(event_id, amended_at, credit_to_type, credit_to_project_id) \
    \VALUES (?, ?, ?, ?) RETURNING id"
    ( eid, fromThyme $ mt ^. _ModTime, "credit_to_project", pid ^. _ProjectId )
    CreditToUser uid ->
    "INSERT INTO event_credit_to_amendments \
    \(event_id, amended_at, credit_to_type, credit_to_user_id) \
    \VALUES (?, ?, ?, ?) RETURNING id"
    ( eid, fromThyme $ mt ^. _ModTime, "credit_to_user", uid ^. _UserId )
  • replacement in lib/Aftok/Database/PostgreSQL.hs at line 238
    [4.2613][4.2613:2720]()
    "INSERT INTO event_metadata_amendments (event_id, mod_time, btc_addr) VALUES (?, ?, ?) RETURNING id"
    [4.2613]
    [4.2720]
    "INSERT INTO event_metadata_amendments \
    \(event_id, amended_at, event_metadata) \
    \VALUES (?, ?, ?) RETURNING id"
  • file addition: 2016-10-14_02-49-36_event-amendments.txt (----------)
    [3.1]
    Description: (Describe migration here.)
    Created: 2016-10-14 02:49:48.862103 UTC
    Depends: 2016-10-13_05-36-55_user-event-log
    Apply: |
    create type credit_to_t as enum ('credit_to_address', 'credit_to_user', 'credit_to_project');
    alter table work_events add column credit_to_type credit_to_t not null default 'credit_to_user';
    alter table work_events alter column btc_addr drop not null;
    alter table work_events rename column btc_addr to credit_to_btc_addr;
    alter table work_events add column credit_to_user_id uuid references users(id);
    alter table work_events add column credit_to_project_id uuid references projects(id);
    create table event_time_amendments(
    event_id uuid references work_events,
    amended_at timestamp with time zone not null,
    event_time timestamp with time zone not null
    );
    create table event_credit_to_amendments(
    event_id uuid references work_events,
    amended_at timestamp with time zone not null,
    credit_to_type credit_to_t not null,
    credit_to_btc_addr text,
    credit_to_user_id uuid references users(id),
    credit_to_project_id uuid references projects(id)
    );
    create table event_metadata_amendments(
    event_id uuid references work_events,
    amended_at timestamp with time zone not null,
    event_metadata json not null
    );
    Revert: |
    drop table event_metadata_amendments;
    drop table event_credit_to_amendments;
    drop table event_time_amendments;
    alter table work_events drop column credit_to_project_id;
    alter table work_events drop column credit_to_user_id;
    alter table work_events rename column credit_to_btc_addr to btc_addr;
    alter table work_events alter column btc_addr set not null;
    alter table work_events drop column credit_to_type;
    drop type credit_to_t;