feat(census): Update census tables to support additional metrics

pull/4445/head
Timothy Messier 2 years ago
parent 5aafe66c3c
commit 5405a275ab
No known key found for this signature in database
GPG Key ID: EFD2F184F7600572

@ -12,6 +12,7 @@ begin;
-- This index ensures that there will only ever be one row in the table.
-- See: https://www.postgresql.org/docs/current/indexes-expressional.html
-- Dropped in 82/09_census_add_metric.up.sql
create unique index census_last_uploaded_one_row
on census_last_uploaded((last_uploaded_at is not null));

@ -0,0 +1,47 @@
-- Copyright (c) HashiCorp, Inc.
-- SPDX-License-Identifier: BUSL-1.1
begin;
-- Create enum table for valid cenus metrics.
create table census_metric_enm (
name text primary key
constraint only_predefined_census_metrics_allowed
check (
name in (
'sessions',
'active_users'
)
)
);
comment on table census_metric_enm is
'census_metric_enm is an enumeration table for census metric types.';
insert into census_metric_enm
(name)
values ('sessions'),
('active_users');
-- Drop constraint and index from 72/01_census.up.sql
drop index census_last_uploaded_one_row;
alter table census_last_uploaded drop constraint census_last_uploaded_pkey;
-- Add new column and populate the default value.
alter table census_last_uploaded
add column metric text
default 'sessions'
not null
primary key
constraint census_metric_enm_fkey
references census_metric_enm (name)
on delete restrict
on update restrict;
alter table census_last_uploaded alter column metric drop default;
comment on table census_last_uploaded is
'census_last_uploaded is a table which contains the timestamp '
'of the last time census data was uploaded, per census metric.';
-- Add new row for active_users.
insert into census_last_uploaded
(last_uploaded_at, metric)
values ('-infinity'::timestamptz, 'active_users');
commit;

@ -3,28 +3,55 @@
begin;
select plan(6);
select plan(7);
select has_table('census_last_uploaded');
select is(count(*), 1::bigint, 'census_last_uploaded should have only 1 row') from census_last_uploaded;
select ok(not isfinite(last_uploaded_at)) from census_last_uploaded;
select is(count(*), 2::bigint, 'census_last_uploaded should have only 2 row') from census_last_uploaded;
select results_eq(
$$
select last_uploaded_at::timestamptz, metric
from census_last_uploaded
$$,
$$
values ('-infinity'::timestamptz, 'sessions'),
('-infinity'::timestamptz, 'active_users')
$$);
prepare insert_row as
insert into census_last_uploaded
(last_uploaded_at)
values
(now());
(last_uploaded_at, metric)
values (now(), 'sessions');
select throws_ok('insert_row',
'23505',
'duplicate key value violates unique constraint "census_last_uploaded_pkey"',
'insert into census_last_uploaded should fail');
prepare insert_row_invalid_metric as
insert into census_last_uploaded
(last_uploaded_at, metric)
values (now(), 'foo');
select throws_ok('insert_row', '23505',
'duplicate key value violates unique constraint "census_last_uploaded_one_row"',
'insert into census_last_uploaded should fail');
select throws_ok('insert_row_invalid_metric',
'23503',
'insert or update on table "census_last_uploaded" violates foreign key constraint "census_metric_enm_fkey"',
'insert into census_last_uploaded should fail');
prepare update_census_last_uploaded as
update census_last_uploaded
set last_uploaded_at = now();
select lives_ok('update_census_last_uploaded');
select ok(isfinite(last_uploaded_at)) from census_last_uploaded;
select results_eq(
$$
select last_uploaded_at::timestamptz, metric
from census_last_uploaded
$$,
$$
values (now(), 'sessions'),
(now(), 'active_users')
$$);
select * from finish();

Loading…
Cancel
Save