Skip to content

Commit

Permalink
remove archival of purged broadcasts in preparation of removal of rec…
Browse files Browse the repository at this point in the history
…ipients table
  • Loading branch information
nicpottier committed Apr 25, 2019
1 parent 236950f commit ecf2a0a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 77 deletions.
57 changes: 0 additions & 57 deletions archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,45 +538,6 @@ SELECT rec.visibility, row_to_json(rec) FROM (
ORDER BY created_on ASC, id ASC) rec;
`

const lookupPurgedBroadcasts = `
SELECT row_to_json(rec) FROM (
SELECT
NULL AS id,
mb.id AS broadcast,
jsonb_build_object('uuid', c.uuid, 'name', c.name) as contact,
COALESCE(mb.text->c.language, mb.text->$2, mb.text->'base', (avals(mb.text))[1]) AS text,
NULL AS urn,
NULL AS channel,
'out' AS direction,
'flow' AS type,
CASE when br.purged_status = 'I' then 'initializing'
WHEN br.purged_status = 'P' then 'queued'
WHEN br.purged_status = 'Q' then 'queued'
WHEN br.purged_status = 'W' then 'wired'
WHEN br.purged_status = 'D' then 'delivered'
WHEN br.purged_status = 'H' then 'handled'
WHEN br.purged_status = 'E' then 'errored'
WHEN br.purged_status = 'F' then 'failed'
WHEN br.purged_status = 'R' then 'resent'
ELSE 'wired'
END as status,
'visible' AS visibility,
'[]'::jsonb AS attachments,
'[]'::jsonb AS labels,
mb.created_on AS created_on,
mb.created_on AS sent_on,
mb.created_on as modified_on
FROM msgs_broadcast_recipients br
JOIN LATERAL (select uuid, name, language FROM contacts_contact cc WHERE cc.id = br.contact_id AND cc.is_test = FALSE) AS c ON TRUE
JOIN msgs_broadcast mb ON br.broadcast_id = mb.id
WHERE br.broadcast_id = ANY (
ARRAY(
SELECT id FROM msgs_broadcast
WHERE org_id = $1 AND created_on > $3 AND created_on < $4 AND purged = TRUE
ORDER by created_on, id
))
) rec;`

// writeMessageRecords writes the messages in the archive's date range to the passed in writer
func writeMessageRecords(ctx context.Context, db *sqlx.DB, archive *Archive, writer *bufio.Writer) (int, error) {
var rows *sqlx.Rows
Expand Down Expand Up @@ -605,24 +566,6 @@ func writeMessageRecords(ctx context.Context, db *sqlx.DB, archive *Archive, wri
recordCount++
}

// now write any broadcasts that were purged
rows, err = db.QueryxContext(ctx, lookupPurgedBroadcasts, archive.Org.ID, archive.Org.Language, archive.StartDate, archive.endDate())
if err != nil {
return 0, err
}
defer rows.Close()

for rows.Next() {
err = rows.Scan(&record)
if err != nil {
return 0, err
}

writer.WriteString(record)
writer.WriteString("\n")
recordCount++
}

logrus.WithField("record_count", recordCount).Debug("Done Writing")
return recordCount, nil
}
Expand Down
12 changes: 6 additions & 6 deletions archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ func TestArchiveOrgMessages(t *testing.T) {

assert.Equal(t, time.Date(2017, 8, 12, 0, 0, 0, 0, time.UTC), created[2].StartDate)
assert.Equal(t, DayPeriod, created[2].Period)
assert.Equal(t, 6, created[2].RecordCount)
assert.Equal(t, int64(646), created[2].Size)
assert.Equal(t, "5fe5adcd696342367bdc9b877e524f4a", created[2].Hash)
assert.Equal(t, 3, created[2].RecordCount)
assert.Equal(t, int64(483), created[2].Size)
assert.Equal(t, "6fe9265860425cf1f9757ba3d91b1a05", created[2].Hash)

assert.Equal(t, time.Date(2017, 8, 13, 0, 0, 0, 0, time.UTC), created[3].StartDate)
assert.Equal(t, DayPeriod, created[3].Period)
Expand All @@ -334,9 +334,9 @@ func TestArchiveOrgMessages(t *testing.T) {

assert.Equal(t, time.Date(2017, 8, 1, 0, 0, 0, 0, time.UTC), created[61].StartDate)
assert.Equal(t, MonthPeriod, created[61].Period)
assert.Equal(t, 7, created[61].RecordCount)
assert.Equal(t, int64(670), created[61].Size)
assert.Equal(t, "0677e49245b792664d75e9b3aebb4455", created[61].Hash)
assert.Equal(t, 4, created[61].RecordCount)
assert.Equal(t, int64(509), created[61].Size)
assert.Equal(t, "9e40be76913bf58655b70ee96dcac25d", created[61].Hash)

assert.Equal(t, time.Date(2017, 9, 1, 0, 0, 0, 0, time.UTC), created[62].StartDate)
assert.Equal(t, MonthPeriod, created[62].Period)
Expand Down
14 changes: 0 additions & 14 deletions testdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,6 @@ CREATE TABLE msgs_broadcast (
org_id integer NOT NULL references orgs_org(id) on delete cascade
);

CREATE TABLE msgs_broadcast_recipients (
id serial primary key,
broadcast_id integer NOT NULL references msgs_broadcast(id) on delete cascade,
contact_id integer NOT NULL references contacts_contact(id) on delete cascade,
purged_status character varying(1)
);

DROP TABLE IF EXISTS msgs_label CASCADE;
CREATE TABLE msgs_label (
id serial primary key,
Expand Down Expand Up @@ -308,13 +301,6 @@ INSERT INTO msgs_broadcast(id, text, created_on, purged, org_id) VALUES
(2, 'base=>"hola"'::hstore, '2017-08-12 22:11:59.890662+02:00', TRUE, 2),
(3, 'base=>"not purged"'::hstore, '2017-08-12 19:11:59.890662+02:00', FALSE, 2);

INSERT INTO msgs_broadcast_recipients(id, broadcast_id, contact_id, purged_status) VALUES
(1, 1, 8, 'D'),
(2, 1, 11, NULL),
(3, 1, 10, NULL),
(4, 2, 9, NULL),
(5, 3, 9, NULL);

INSERT INTO flows_flow(id, uuid, name) VALUES
(1, '6639286a-9120-45d4-aa39-03ae3942a4a6', 'Flow 1'),
(2, '629db399-a5fb-4fa0-88e6-f479957b63d2', 'Flow 2'),
Expand Down

0 comments on commit ecf2a0a

Please sign in to comment.