Skip to content

Commit 517237b

Browse files
authored
Merge pull request nyaruka#38 from nyaruka/current_flow
Index `contact.current_flow_id` as flow uuid
2 parents 5aa5407 + 79a983c commit 517237b

File tree

4 files changed

+73
-61
lines changed

4 files changed

+73
-61
lines changed

contacts/index_settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@
150150
"status": {
151151
"type": "keyword"
152152
},
153+
"flow": {
154+
"type": "keyword"
155+
},
153156
"tickets": {
154157
"type": "integer"
155158
},

contacts/query.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM (
5858
FROM contacts_contactgroup_contacts, contacts_contactgroup
5959
WHERE contact_id = contacts_contact.id AND contacts_contactgroup_contacts.contactgroup_id = contacts_contactgroup.id
6060
) g
61-
) AS groups
61+
) AS groups,
62+
(
63+
SELECT f.uuid FROM flows_flow f WHERE f.id = contacts_contact.current_flow_id
64+
) AS flow
6265
FROM contacts_contact
6366
WHERE modified_on >= $1
6467
ORDER BY modified_on ASC

indexer_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func TestIndexing(t *testing.T) {
101101
assertQuery(t, client, physicalName, elastic.NewMatchQuery("tickets", 1), []int64{2, 3})
102102
assertQuery(t, client, physicalName, elastic.NewRangeQuery("tickets").Gt(0), []int64{1, 2, 3})
103103

104+
assertQuery(t, client, physicalName, elastic.NewMatchQuery("flow", "6d3cf1eb-546e-4fb8-a5ca-69187648fbf6"), []int64{2, 3})
105+
assertQuery(t, client, physicalName, elastic.NewMatchQuery("flow", "4eea8ff1-4fe2-4ce5-92a4-0870a499973a"), []int64{4})
106+
104107
// created_on range query
105108
assertQuery(t, client, physicalName, elastic.NewRangeQuery("created_on").Gt("2017-01-01"), []int64{1, 6, 8})
106109

testdb.sql

+63-60
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
DROP TABLE IF EXISTS flows_flow CASCADE;
2+
CREATE TABLE flows_flow (
3+
id SERIAL PRIMARY KEY,
4+
uuid character varying(36) NOT NULL,
5+
name character varying(128) NOT NULL
6+
);
7+
18
DROP TABLE IF EXISTS contacts_contact CASCADE;
29
CREATE TABLE contacts_contact (
3-
id integer NOT NULL,
10+
id SERIAL PRIMARY KEY,
411
is_active boolean NOT NULL,
512
status character varying(1) NOT NULL,
613
created_by_id integer NOT NULL,
@@ -12,22 +19,14 @@ CREATE TABLE contacts_contact (
1219
name character varying(128),
1320
language character varying(3),
1421
uuid character varying(36) NOT NULL,
22+
current_flow_id integer REFERENCES flows_flow(id),
1523
fields jsonb,
1624
ticket_count integer NOT NULL
1725
);
1826

19-
CREATE SEQUENCE contacts_contact_id_seq
20-
START WITH 1
21-
INCREMENT BY 1
22-
NO MINVALUE
23-
NO MAXVALUE
24-
CACHE 1;
25-
26-
ALTER SEQUENCE contacts_contact_id_seq OWNED BY contacts_contact.id;
27-
2827
DROP TABLE IF EXISTS contacts_contacturn CASCADE;
2928
CREATE TABLE contacts_contacturn (
30-
id integer NOT NULL,
29+
id SERIAL PRIMARY KEY,
3130
contact_id integer,
3231
scheme character varying(128) NOT NULL,
3332
org_id integer NOT NULL,
@@ -39,46 +38,23 @@ CREATE TABLE contacts_contacturn (
3938
identity character varying(255) NOT NULL
4039
);
4140

42-
CREATE SEQUENCE contacts_contacturn_id_seq
43-
START WITH 1
44-
INCREMENT BY 1
45-
NO MINVALUE
46-
NO MAXVALUE
47-
CACHE 1;
48-
49-
ALTER SEQUENCE contacts_contacturn_id_seq OWNED BY contacts_contacturn.id;
50-
5141
DROP TABLE IF EXISTS contacts_contactgroup CASCADE;
5242
CREATE TABLE contacts_contactgroup (
53-
id integer NOT NULL,
43+
id SERIAL PRIMARY KEY,
5444
uuid character varying(36) NOT NULL,
5545
name character varying(128) NOT NULL
5646
);
5747

58-
CREATE SEQUENCE contacts_contactgroup_id_seq
59-
START WITH 1
60-
INCREMENT BY 1
61-
NO MINVALUE
62-
NO MAXVALUE
63-
CACHE 1;
64-
65-
ALTER SEQUENCE contacts_contactgroup_id_seq OWNED BY contacts_contactgroup.id;
66-
6748
DROP TABLE IF EXISTS contacts_contactgroup_contacts CASCADE;
6849
CREATE TABLE contacts_contactgroup_contacts (
69-
id integer NOT NULL,
70-
contactgroup_id integer NOT NULL,
71-
contact_id integer NOT NULL
50+
id SERIAL PRIMARY KEY,
51+
contactgroup_id integer NOT NULL REFERENCES contacts_contactgroup(id),
52+
contact_id integer NOT NULL REFERENCES contacts_contact(id)
7253
);
7354

74-
CREATE SEQUENCE contacts_contactgroup_contacts_id_seq
75-
START WITH 1
76-
INCREMENT BY 1
77-
NO MINVALUE
78-
NO MAXVALUE
79-
CACHE 1;
80-
81-
ALTER SEQUENCE contacts_contactgroup_contacts_id_seq OWNED BY contacts_contactgroup_contacts.id;
55+
INSERT INTO flows_flow(id, uuid, name) VALUES
56+
(1, '6d3cf1eb-546e-4fb8-a5ca-69187648fbf6', 'Favorites'),
57+
(2, '4eea8ff1-4fe2-4ce5-92a4-0870a499973a', 'Catch All');
8258

8359
-- Fields:
8460
-- 17103bb1-1b48-4b70-92f7-1f6b73bd3488 - nickname (text)
@@ -88,42 +64,69 @@ ALTER SEQUENCE contacts_contactgroup_contacts_id_seq OWNED BY contacts_contactgr
8864
-- fcab2439-861c-4832-aa54-0c97f38f24ab - home_district (district)
8965
-- a551ade4-e5a0-4d83-b185-53b515ad2f2a - home_ward (ward)
9066

91-
INSERT INTO contacts_contact(id, is_active, created_by_id, created_on, modified_by_id, modified_on, last_seen_on, org_id, status, name, language, uuid, fields, ticket_count) VALUES
67+
INSERT INTO contacts_contact(id, is_active, created_by_id, created_on, modified_by_id, modified_on, last_seen_on, org_id, status, name, language, uuid, fields, ticket_count, current_flow_id) VALUES
9268
(
93-
1, TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', '2020-08-04 21:11', 1, 'A', NULL, 'eng', 'c7a2dd87-a80e-420b-8431-ca48d422e924',
94-
'{ "17103bb1-1b48-4b70-92f7-1f6b73bd3488": {"text": "the rock"}}', 2
69+
1,
70+
TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', '2020-08-04 21:11', 1, 'A', NULL, 'eng', 'c7a2dd87-a80e-420b-8431-ca48d422e924',
71+
'{ "17103bb1-1b48-4b70-92f7-1f6b73bd3488": {"text": "the rock"}}',
72+
2,
73+
NULL
9574
),
9675
(
97-
2, TRUE, -1, '2015-03-26 10:07:14.054521+00', -1, '2015-03-26 10:07:14.054521+00', '2020-08-03 13:11', 1, 'S', NULL, NULL, '7a6606c7-ff41-4203-aa98-454a10d37209',
98-
'{ "05bca1cd-e322-4837-9595-86d0d85e5adb": {"text": "11", "number": 11 }}', 1
76+
2,
77+
TRUE, -1, '2015-03-26 10:07:14.054521+00', -1, '2015-03-26 10:07:14.054521+00', '2020-08-03 13:11', 1, 'S', NULL, NULL, '7a6606c7-ff41-4203-aa98-454a10d37209',
78+
'{ "05bca1cd-e322-4837-9595-86d0d85e5adb": {"text": "11", "number": 11 }}',
79+
1,
80+
1
9981
),
10082
(
101-
3, TRUE, -1, '2015-03-26 13:04:58.699648+00', -1, '2015-03-26 13:04:58.699648+00', '2018-05-04 21:11', 1, 'B', NULL, NULL, '29b45297-15ad-4061-a7d4-e0b33d121541',
102-
'{ "05bca1cd-e322-4837-9595-86d0d85e5adb": {"text": "9", "number": 9 }, "e0eac267-463a-4c00-9732-cab62df07b16": { "text": "2018-04-06T18:37:59+00:00", "datetime": "2018-04-06T18:37:59+00:00"}}', 1
83+
3,
84+
TRUE, -1, '2015-03-26 13:04:58.699648+00', -1, '2015-03-26 13:04:58.699648+00', '2018-05-04 21:11', 1, 'B', NULL, NULL, '29b45297-15ad-4061-a7d4-e0b33d121541',
85+
'{ "05bca1cd-e322-4837-9595-86d0d85e5adb": {"text": "9", "number": 9 }, "e0eac267-463a-4c00-9732-cab62df07b16": { "text": "2018-04-06T18:37:59+00:00", "datetime": "2018-04-06T18:37:59+00:00"}}',
86+
1,
87+
1
10388
),
10489
(
105-
4, TRUE, -1, '2015-03-27 07:39:28.955051+00', -1, '2015-03-27 07:39:28.955051+00', '2015-12-31 23:59', 1, 'A', 'John Doe', NULL, '51762bba-01a2-4c4e-b5cd-b182d0405cd4',
106-
'{ "e0eac267-463a-4c00-9732-cab62df07b16": { "text": "2030-04-06T18:37:59+00:00", "datetime": "2030-04-06T18:37:59+00:00"}}', 0
90+
4,
91+
TRUE, -1, '2015-03-27 07:39:28.955051+00', -1, '2015-03-27 07:39:28.955051+00', '2015-12-31 23:59', 1, 'A', 'John Doe', NULL, '51762bba-01a2-4c4e-b5cd-b182d0405cd4',
92+
'{ "e0eac267-463a-4c00-9732-cab62df07b16": { "text": "2030-04-06T18:37:59+00:00", "datetime": "2030-04-06T18:37:59+00:00"}}',
93+
0,
94+
2
10795
),
10896
(
109-
5, TRUE, -1, '2015-10-30 19:42:27.001837+00', -1, '2015-10-30 19:42:27.001837+00', '2020-08-04 21:11', 2, 'A', 'Ajodinabiff Dane', NULL, '3e814add-e614-41f7-8b5d-a07f670a698f',
110-
'{ "22d11697-edba-4186-b084-793e3b876379": { "text": "USA > Washington", "state": "USA > Washington"} }', 0
97+
5,
98+
TRUE, -1, '2015-10-30 19:42:27.001837+00', -1, '2015-10-30 19:42:27.001837+00', '2020-08-04 21:11', 2, 'A', 'Ajodinabiff Dane', NULL, '3e814add-e614-41f7-8b5d-a07f670a698f',
99+
'{ "22d11697-edba-4186-b084-793e3b876379": { "text": "USA > Washington", "state": "USA > Washington"} }',
100+
0,
101+
NULL
111102
),
112103
(
113-
6, TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', '2020-08-04 21:00', 2, 'A', 'Joanne Stone', NULL, '7051dff0-0a27-49d7-af1f-4494239139e6',
114-
'{ "22d11697-edba-4186-b084-793e3b876379": { "text": "USA > Colorado", "state": "USA > Colorado"} }', 0
104+
6,
105+
TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', '2020-08-04 21:00', 2, 'A', 'Joanne Stone', NULL, '7051dff0-0a27-49d7-af1f-4494239139e6',
106+
'{ "22d11697-edba-4186-b084-793e3b876379": { "text": "USA > Colorado", "state": "USA > Colorado"} }',
107+
0,
108+
NULL
115109
),
116110
(
117-
7, TRUE, -1, '2015-03-27 13:39:43.995812+00', -1, '2015-03-27 13:39:43.995812+00', NULL, 2, 'A', NULL, NULL, 'b46f6e18-95b4-4984-9926-dded047f4eb3',
118-
'{ "fcab2439-861c-4832-aa54-0c97f38f24ab": { "text": "USA > Washington > King-Côunty", "district": "USA > Washington > King-Côunty"} }', 0
111+
7,
112+
TRUE, -1, '2015-03-27 13:39:43.995812+00', -1, '2015-03-27 13:39:43.995812+00', NULL, 2, 'A', NULL, NULL, 'b46f6e18-95b4-4984-9926-dded047f4eb3',
113+
'{ "fcab2439-861c-4832-aa54-0c97f38f24ab": { "text": "USA > Washington > King-Côunty", "district": "USA > Washington > King-Côunty"} }',
114+
0,
115+
NULL
119116
),
120117
(
121-
8, TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', NULL, 2, 'A', NULL, NULL, '9195c8b7-6138-4d84-ac56-5192cc3d8ceb',
122-
'{ "a551ade4-e5a0-4d83-b185-53b515ad2f2a": { "text": "USA > Washington > King-Côunty > Central District", "ward": "USA > Washington > King-Côunty > Central District"} }', 0
118+
8,
119+
TRUE, -1, '2017-11-10 21:11:59.890662+00', -1, '2017-11-10 21:11:59.890662+00', NULL, 2, 'A', NULL, NULL, '9195c8b7-6138-4d84-ac56-5192cc3d8ceb',
120+
'{ "a551ade4-e5a0-4d83-b185-53b515ad2f2a": { "text": "USA > Washington > King-Côunty > Central District", "ward": "USA > Washington > King-Côunty > Central District"} }',
121+
0,
122+
NULL
123123
),
124124
(
125-
9, TRUE, -1, '2016-08-22 14:20:05.690311+00', -1, '2016-08-22 14:20:05.690311+00', NULL, 2, 'A', NULL, NULL, '2b8bd28d-43e0-4c34-a4bb-0f10b11fdb8a',
126-
'{ "fcab2439-861c-4832-aa54-0c97f38f24ab": { "text": "USA > Colorado > King", "district": "USA > Colorado > King"} }', 0
125+
9,
126+
TRUE, -1, '2016-08-22 14:20:05.690311+00', -1, '2016-08-22 14:20:05.690311+00', NULL, 2, 'A', NULL, NULL, '2b8bd28d-43e0-4c34-a4bb-0f10b11fdb8a',
127+
'{ "fcab2439-861c-4832-aa54-0c97f38f24ab": { "text": "USA > Colorado > King", "district": "USA > Colorado > King"} }',
128+
0,
129+
NULL
127130
);
128131

129132
INSERT INTO contacts_contacturn(id, contact_id, scheme, org_id, priority, path, display, identity) VALUES

0 commit comments

Comments
 (0)