Skip to content

Commit

Permalink
Extra column to materialised view and Unit Test (#64)
Browse files Browse the repository at this point in the history
Ticket: #61

In this commit:
* Add contact_type column to contactview_metadata materialized view.
* Add a unit test to check that materialized views have the required columns defined.
  • Loading branch information
latin-panda authored Mar 16, 2021
1 parent d744c88 commit 34f1d2a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-- filter contact docs into one place
CREATE OR REPLACE VIEW raw_contacts AS SELECT * FROM couchdb WHERE doc->>'type' IN ('clinic', 'district_hospital', 'health_center', 'person');
CREATE OR REPLACE VIEW raw_contacts AS SELECT * FROM couchdb WHERE doc->>'type' IN ('clinic', 'district_hospital', 'health_center', 'person', 'contact');

-- extract JSON data from contact docs and cache it
DROP MATERIALIZED VIEW IF EXISTS contactview_metadata CASCADE;
CREATE MATERIALIZED VIEW contactview_metadata AS
SELECT doc->>'_id' AS uuid, doc->>'name' AS name, doc->>'type' AS type, doc#>>'{contact,_id}' AS contact_uuid, doc#>>'{parent,_id}' AS parent_uuid, doc->>'notes' AS notes,
SELECT doc->>'_id' AS uuid, doc->>'name' AS name, doc->>'type' AS type, doc->>'contact_type' AS contact_type, doc#>>'{contact,_id}' AS contact_uuid, doc#>>'{parent,_id}' AS parent_uuid, doc->>'notes' AS notes,
TIMESTAMP WITH TIME ZONE 'epoch' + (doc->>'reported_date')::numeric / 1000 * interval '1 second' AS reported
FROM raw_contacts;

Expand Down
20 changes: 20 additions & 0 deletions tests/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('xmlforms', () => {
assert(await db.schema.hasTable('xmlforms_migrations'));

const views = await db.views();

assert.equal(views[0].table_name, 'contactview_chw');
assert.equal(views[1].table_name, 'contactview_clinic');
assert.equal(views[2].table_name, 'contactview_clinic_person');
Expand All @@ -32,4 +33,23 @@ describe('xmlforms', () => {
assert.equal(views[5].table_name, 'raw_contacts');
await db.destroy();
});

it('checks that materialized views have the required columns', async () => {
await pgutils.ensureDbExists(PG_DB_URL);
const db = new pgutils.Pg(PG_DB_URL);
await couch2pg.migrate(PG_DB_URL);
await analytics.migrate(PG_DB_URL);

const viewsMap = await db.materializedViews();

assert.isDefined(viewsMap.contactview_metadata);
assert.includeMembers(
viewsMap.contactview_metadata.columns,
['uuid', 'name', 'type', 'contact_type', 'contact_uuid', 'parent_uuid', 'notes', 'reported']
);
assert.isDefined(viewsMap.form_metadata);
assert.includeMembers(viewsMap.form_metadata.columns, ['uuid', 'chw', 'chw_area', 'formname', 'reported']);

await db.destroy();
});
});
19 changes: 19 additions & 0 deletions tests/utils/pgutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const SELECT_VIEWS =
WHERE table_schema = ANY (current_schemas(false)) \
order by table_name';

const SELECT_MATERIALIZED_VIEWS =
'SELECT matviewname AS view_name, attributes.attname AS column_name \
FROM pg_matviews LEFT JOIN pg_attribute AS attributes ON (matviewname::regclass = attributes.attrelid) \
ORDER BY view_name;';

class Pg {

constructor(url) {
Expand All @@ -52,6 +57,20 @@ class Pg {
return (await this.conn.raw(SELECT_VIEWS)).rows;
}

async materializedViews() {
const rows = (await this.conn.raw(SELECT_MATERIALIZED_VIEWS)).rows;
const viewsMap = {};

rows.forEach(row => {
if (!viewsMap[row.view_name]) {
viewsMap[row.view_name] = { columns: [] };
}
viewsMap[row.view_name].columns.push(row.column_name);
});

return viewsMap;
}

async destroy() {
await this.conn.destroy();
}
Expand Down

0 comments on commit 34f1d2a

Please sign in to comment.