From 053e74f66c1254fa021eef6ce729b16482ad852a Mon Sep 17 00:00:00 2001 From: Michael Gaffney Date: Thu, 1 Jun 2023 12:44:11 -0400 Subject: [PATCH] feat(db): Add table for census upload job status --- .../oss/postgres/72/01_census.up.sql | 20 ++++++++++++ internal/db/sqltest/Makefile | 1 + .../db/sqltest/tests/census/last_uploaded.sql | 31 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 internal/db/schema/migrations/oss/postgres/72/01_census.up.sql create mode 100644 internal/db/sqltest/tests/census/last_uploaded.sql diff --git a/internal/db/schema/migrations/oss/postgres/72/01_census.up.sql b/internal/db/schema/migrations/oss/postgres/72/01_census.up.sql new file mode 100644 index 000000000..d96fa1519 --- /dev/null +++ b/internal/db/schema/migrations/oss/postgres/72/01_census.up.sql @@ -0,0 +1,20 @@ +-- Copyright (c) HashiCorp, Inc. +-- SPDX-License-Identifier: MPL-2.0 + +begin; + + create table census_last_uploaded ( + last_uploaded_at wt_timestamp primary key + ); + comment on table census_last_uploaded is + 'census_last_uploaded is a table with 1 row which contains ' + 'the timestamp of the last time census data was uploaded.'; + + -- This index ensures that there will only ever be one row in the table. + -- See: https://www.postgresql.org/docs/current/indexes-expressional.html + create unique index census_last_uploaded_one_row + on census_last_uploaded((last_uploaded_at is not null)); + + insert into census_last_uploaded(last_uploaded_at) values('-infinity'); + +commit; diff --git a/internal/db/sqltest/Makefile b/internal/db/sqltest/Makefile index 70d513988..8ef54093d 100644 --- a/internal/db/sqltest/Makefile +++ b/internal/db/sqltest/Makefile @@ -27,6 +27,7 @@ TESTS ?= tests/setup/*.sql \ tests/target/*.sql \ tests/controller/*.sql \ tests/hcp/*/*.sql \ + tests/census/*.sql \ tests/kms/*.sql \ tests/storage/*.sql \ tests/domain/*.sql \ diff --git a/internal/db/sqltest/tests/census/last_uploaded.sql b/internal/db/sqltest/tests/census/last_uploaded.sql new file mode 100644 index 000000000..ed191cca9 --- /dev/null +++ b/internal/db/sqltest/tests/census/last_uploaded.sql @@ -0,0 +1,31 @@ +-- Copyright (c) HashiCorp, Inc. +-- SPDX-License-Identifier: MPL-2.0 + +begin; + + select plan(6); + + 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; + + prepare insert_row as + insert into census_last_uploaded + (last_uploaded_at) + values + (now()); + + select throws_ok('insert_row', '23505', + 'duplicate key value violates unique constraint "census_last_uploaded_one_row"', + '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 * from finish(); + +rollback;