diff --git a/packages/db/prisma/leafwatch/migrations/20240810071922_init/migration.sql b/packages/db/prisma/leafwatch/migrations/20240810092806_init/migration.sql similarity index 51% rename from packages/db/prisma/leafwatch/migrations/20240810071922_init/migration.sql rename to packages/db/prisma/leafwatch/migrations/20240810092806_init/migration.sql index d14278936d3f..b570d932438b 100644 --- a/packages/db/prisma/leafwatch/migrations/20240810071922_init/migration.sql +++ b/packages/db/prisma/leafwatch/migrations/20240810092806_init/migration.sql @@ -11,16 +11,22 @@ CREATE TABLE "Event" ( "ip" TEXT, "city" TEXT, "country" TEXT, - "created" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "created" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT "Event_pkey" PRIMARY KEY ("id") + CONSTRAINT "Event_pkey" PRIMARY KEY ("created","id") ); -- CreateTable CREATE TABLE "Impression" ( "id" UUID NOT NULL DEFAULT gen_random_uuid(), "publication" TEXT NOT NULL, - "viewed" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "viewed" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT "Impression_pkey" PRIMARY KEY ("id") + CONSTRAINT "Impression_pkey" PRIMARY KEY ("viewed","id") ); + +-- CreateIndex +CREATE INDEX "Event_created_idx" ON "Event"("created"); + +-- CreateIndex +CREATE INDEX "Impression_viewed_idx" ON "Impression"("viewed"); diff --git a/packages/db/prisma/leafwatch/schema.prisma b/packages/db/prisma/leafwatch/schema.prisma index 16aff1a3f357..2e175765b5d5 100644 --- a/packages/db/prisma/leafwatch/schema.prisma +++ b/packages/db/prisma/leafwatch/schema.prisma @@ -9,7 +9,7 @@ datasource db { } model Event { - id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid + id String @default(dbgenerated("gen_random_uuid()")) @db.Uuid actor String? fingerprint String? name String @@ -20,11 +20,26 @@ model Event { ip String? city String? country String? - created DateTime @default(now()) + created DateTime @default(now()) @db.Timestamptz(3) + + @@id([created, id]) + @@index([created]) } model Impression { - id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid + id String @default(dbgenerated("gen_random_uuid()")) @db.Uuid publication String - viewed DateTime @default(now()) + viewed DateTime @default(now()) @db.Timestamptz(3) + + @@id([viewed, id]) + @@index([viewed]) } + +// Run the following command to create the extension +// CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE; +// Create the hypertables +// SELECT create_hypertable('"Event"', 'created', migrate_data => true); +// SELECT create_hypertable('"Impression"', 'viewed', migrate_data => true); +// +// Add retention policy +// SELECT add_retention_policy('"Event"', INTERVAL '1 year');