More Related Content Similar to Functional Database Strategies (20) More from Jason Swartz (7) Functional Database Strategies17. 1. POST /issues title=’Config ELB’
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
18. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:16 | Config ELB | 10 | false |
+------+-------------+------------+----------+-------+
19. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
20. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
Not Bad.
21. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
Do You Know How We Got Here?
23. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
Do You Know How We Got Here?
24. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
Why is ‘assignee’ NULL?
28. 1. POST /issues title=’Config ELB’
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
29. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:16 | Config ELB | 10 | false |
+------+-------------+------------+----------+-------+
30. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:16 | Config ELB | 10 | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:19 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
31. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:16 | Config ELB | 10 | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:19 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
32. 1. GET /issues/1
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:16 | Config ELB | 10 | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:19 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
40. 1. GET /issues/1
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:16 | Config ELB | 10 | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:19 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | NULL | true |
+------+-------------+------------+----------+-------+
41. create table issues (
id serial,
created timestamp default now(),
issue_id int default nextval(‘iseq’),
title text,
assignee int,
done boolean default false
)
42. 1. GET /issues/1
+------+-------------+------------+------------+----------+-------+
| id | created | issue_id | title | assignee | done |
+------+-------------+------------+------------+----------+-------+
| 1 | 09-18 18:13 | 1 | Config ELB | NULL | false |
+------+-------------+------------+------------+----------+-------+
| 2 | 09-18 18:16 | 1 | Config ELB | 10 | false |
+------+-------------+------------+------------+----------+-------+
| 3 | 09-18 18:19 | 1 | Config ELB | NULL | false |
+------+-------------+------------+------------+----------+-------+
+------+-------------+------------+------------+----------+-------+
| 4 | 09-18 18:24 | 1 | Config ELB | NULL | true |
+------+-------------+------------+------------+----------+-------+
43. select * from issues
where issue_id = :issue_id
order by id desc limit 1;
49. ● Aggregation functions, eg sum(), rank(), avg()
● Window definitions with over()
● Grouping with partition by, order
Window Functions
50. +------+------------+------------+----------+-------+
| id | issue_id | title | assignee | done |
+------+------------+------------+----------+-------+
| 1 | 1 | Config ELB | NULL | false |
+------+------------+------------+----------+-------+
| 2 | 1 | Config ELB | 10 | false |
+------+------------+------------+----------+-------+
| 3 | 2 | Bug to fix | 11 | false |
+------+------------+------------+----------+-------+
| 4 | 2 | Bug to fix | 11 | true |
+------+------------+------------+----------+-------+
51. +------+------------+------------+----------+-------+
| id | issue_id | title | assignee | done |
+------+------------+------------+----------+-------+
| 1 | 1 | Config ELB | NULL | false |
+------+------------+------------+----------+-------+
| 2 | 1 | Config ELB | 10 | false |
+------+------------+------------+----------+-------+
| 3 | 2 | Bug to fix | 11 | false |
+------+------------+------------+----------+-------+
| 4 | 2 | Bug to fix | 11 | true |
+------+------------+------------+----------+-------+
52. with latest_issues as (
select *, row_number() over (
partition by issue_id
order by id
desc
)
from issues where created > now() - interval '7 day'
)
select * from latest_issues where row_number = 1
53. with latest_issues as (
select *, row_number() over (
partition by issue_id
order by id
desc
)
from issues where created > now() - interval '7 day'
)
select * from latest_issues where row_number = 1
54. with latest_issues as (
select *, row_number() over (
partition by issue_id
order by id
desc
)
from issues where created > now() - interval '7 day'
)
select * from latest_issues where row_number = 1
55. with latest_issues as (
select *, row_number() over (
partition by issue_id
order by id
desc
)
from issues where created > now() - interval '7 day'
)
select * from latest_issues where row_number = 1
56. +------+------------+------------+----------+-------+------------+
| id | issue_id | title | assignee | done | row_number |
+------+------------+------------+----------+-------+------------+
| 1 | 1 | Config ELB | NULL | false | 2 |
+------+------------+------------+----------+-------+------------+
| 2 | 1 | Config ELB | 10 | false | 1 |
+------+------------+------------+----------+-------+------------+
| 3 | 2 | Bug to fix | 11 | false | 2 |
+------+------------+------------+----------+-------+------------+
| 4 | 2 | Bug to fix | 11 | true | 1 |
+------+------------+------------+----------+-------+------------+
57. +------+------------+------------+----------+-------+------------+
| id | issue_id | title | assignee | done | row_number |
+------+------------+------------+----------+-------+------------+
| 1 | 1 | Config ELB | NULL | false | 2 |
+------+------------+------------+----------+-------+------------+
| 2 | 1 | Config ELB | 10 | false | 1 |
+------+------------+------------+----------+-------+------------+
| 3 | 2 | Bug to fix | 11 | false | 2 |
+------+------------+------------+----------+-------+------------+
| 4 | 2 | Bug to fix | 11 | true | 1 |
+------+------------+------------+----------+-------+------------+
58. +------+------------+------------+----------+-------+------------+
| id | issue_id | title | assignee | done | row_number |
+------+------------+------------+----------+-------+------------+
| 1 | 1 | Config ELB | NULL | false | 2 |
+------+------------+------------+----------+-------+------------+
| 2 | 1 | Config ELB | 10 | false | 1 |
+------+------------+------------+----------+-------+------------+
| 3 | 2 | Bug to fix | 11 | false | 2 |
+------+------------+------------+----------+-------+------------+
| 4 | 2 | Bug to fix | 11 | true | 1 |
+------+------------+------------+----------+-------+------------+
65. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:16 | Config ELB | 10 | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | 10 | true |
+------+-------------+------------+----------+-------+
66. 1. POST /issues title=’Config ELB’
2. PUT /issues/1 assignee=10
3. PUT /issues/1 done=true
+------+-------------+------------+----------+-------+
| id | updated | title | assignee | done |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:13 | Config ELB | NULL | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:16 | Config ELB | 10 | false |
+------+-------------+------------+----------+-------+
| 1 | 09-18 18:24 | Config ELB | 10 | true |
+------+-------------+------------+----------+-------+
Events
States
72. create table issue_events (
id serial,
created timestamp default now(),
issue_id int default nextval(‘iseq’),
originator text,
payload text
)
73. 1. POST /issue/1/event ‘Originator: 4a48239-8a..’
payload=’<Update val=”done=true”>’
+----+-------------+----------+------------+---------+
| id | created | issue_id | originator | payload |
+----+-------------+----------+------------+---------+
| 14 | 09-18 18:50 | 1 | 4a482... | <...> |
+----+-------------+----------+------------+---------+
79. 1. High Write Performance
2. Potential for Command/Query Separation
3. Auditable
4. Replayable
5. Undo-able
6. Monitorable
Reasons For Event-Sourcing
83. 1. Frankly, It’s Weird
2. Requires Events. No Events, No Event-Sourcing.
3. As Of November 2016, It’s Still Non-Standard
Reasons Against Event-Sourcing
91. ● Avoid shared mutable SESSIONS
● Avoid shared mutable CURSORS
● Mutating state? Cool! But MODEL IT FIRST
● Execute state changes at THE EDGE
Safe Database Interactions