Skip to content

Commit

Permalink
Merge pull request #102 from guardian/switch_primary_key_on_reminders
Browse files Browse the repository at this point in the history
Switch primary key and unique constraint on reminder tables to enable data warehousing via Fivetran
  • Loading branch information
paulbrown1982 committed Jul 21, 2023
2 parents 9edef16 + ce42ed6 commit 17d99af
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
20 changes: 10 additions & 10 deletions cdk/lib/__snapshots__/support-reminders.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ exports[`The SupportReminders stack matches the snapshot 1`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -1602,7 +1602,7 @@ exports[`The SupportReminders stack matches the snapshot 1`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -1832,7 +1832,7 @@ exports[`The SupportReminders stack matches the snapshot 1`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -2207,7 +2207,7 @@ exports[`The SupportReminders stack matches the snapshot 1`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -2437,7 +2437,7 @@ exports[`The SupportReminders stack matches the snapshot 1`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -4158,7 +4158,7 @@ exports[`The SupportReminders stack matches the snapshot 2`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -4388,7 +4388,7 @@ exports[`The SupportReminders stack matches the snapshot 2`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -4618,7 +4618,7 @@ exports[`The SupportReminders stack matches the snapshot 2`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -4993,7 +4993,7 @@ exports[`The SupportReminders stack matches the snapshot 2`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down Expand Up @@ -5223,7 +5223,7 @@ exports[`The SupportReminders stack matches the snapshot 2`] = `
"Arn",
],
},
"Runtime": "nodejs12.x",
"Runtime": "nodejs14.x",
"Tags": [
{
"Key": "App",
Expand Down
2 changes: 1 addition & 1 deletion cdk/lib/support-reminders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SupportReminders extends GuStack {
// ---- Miscellaneous constants ---- //
const app = "support-reminders";
const vpc = GuVpc.fromIdParameter(this, "vpc");
const runtime = Runtime.NODEJS_12_X;
const runtime = Runtime.NODEJS_14_X;
const fileName = "support-reminders.zip";
const environment = {
"Bucket": props.datalakeBucket,
Expand Down
18 changes: 12 additions & 6 deletions sql/create-signups-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ CREATE TABLE one_off_reminder_signups(
reminder_stage TEXT NOT NULL,
reminder_period DATE NOT NULL,
reminder_option TEXT,
reminder_code uuid NOT NULL UNIQUE DEFAULT uuid_generate_v4 (),
reminder_code uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4 (),
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (identity_id, reminder_period)
UNIQUE (identity_id, reminder_period)
);

CREATE INDEX one_off_reminder_signups_reminder_code ON one_off_reminder_signups (reminder_code);
/* PostgreSQL automatically creates a unique index when a unique constraint or primary key is defined for a table
- https://www.postgresql.org/docs/current/indexes-unique.html
*/
-- CREATE INDEX one_off_reminder_signups_reminder_code ON one_off_reminder_signups (identity_id);

DROP TABLE IF EXISTS recurring_reminder_signups;
CREATE TABLE recurring_reminder_signups(
identity_id TEXT NOT NULL PRIMARY KEY,
identity_id TEXT NOT NULL UNIQUE,
country TEXT,
reminder_created_at TIMESTAMP NOT NULL,
reminder_cancelled_at TIMESTAMP,
Expand All @@ -30,12 +33,15 @@ CREATE TABLE recurring_reminder_signups(
reminder_stage TEXT NOT NULL,
reminder_frequency_months INT NOT NULL,
reminder_option TEXT,
reminder_code uuid NOT NULL UNIQUE DEFAULT uuid_generate_v4 (),
reminder_code uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4 (),
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX recurring_reminder_signups_reminder_code ON recurring_reminder_signups (reminder_code);
/* PostgreSQL automatically creates a unique index when a unique constraint or primary key is defined for a table
- https://www.postgresql.org/docs/current/indexes-unique.html
*/
-- CREATE INDEX recurring_reminder_signups_reminder_code ON recurring_reminder_signups (identity_id);

CREATE OR REPLACE FUNCTION set_updated_at_column()
RETURNS TRIGGER AS $$
Expand Down
4 changes: 2 additions & 2 deletions src/create-reminder-signup/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function writeOneOffSignup(
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)
ON CONFLICT ON CONSTRAINT one_off_reminder_signups_pkey
ON CONFLICT ON CONSTRAINT one_off_reminder_signups_identity_id_reminder_period_key
DO
UPDATE SET
country = $2,
Expand Down Expand Up @@ -65,7 +65,7 @@ export function writeRecurringSignup(
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)
ON CONFLICT ON CONSTRAINT recurring_reminder_signups_pkey
ON CONFLICT ON CONSTRAINT recurring_reminder_signups_identity_id_key
DO
UPDATE SET
country = $2,
Expand Down

0 comments on commit 17d99af

Please sign in to comment.