diff --git a/db/postgresql/migrations/02_add_event_data/migration.sql b/db/postgresql/migrations/02_add_event_data/migration.sql new file mode 100644 index 00000000..c4d9862d --- /dev/null +++ b/db/postgresql/migrations/02_add_event_data/migration.sql @@ -0,0 +1,47 @@ +-- AlterTable +ALTER TABLE "event" ADD COLUMN "event_name" VARCHAR(50); + +-- CreateTable +CREATE TABLE "event_data" ( + "event_data_id" SERIAL NOT NULL, + "event_id" INTEGER NOT NULL, + "event_data" JSONB NOT NULL, + + CONSTRAINT "event_data_pkey" PRIMARY KEY ("event_data_id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "event_data_event_id_key" ON "event_data"("event_id"); + +-- AddForeignKey +ALTER TABLE "event_data" ADD CONSTRAINT "event_data_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "event"("event_id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- RenameIndex +ALTER INDEX "account.username_unique" RENAME TO "account_username_key"; + +-- RenameIndex +ALTER INDEX "session.session_uuid_unique" RENAME TO "session_session_uuid_key"; + +-- RenameIndex +ALTER INDEX "website.share_id_unique" RENAME TO "website_share_id_key"; + +-- RenameIndex +ALTER INDEX "website.website_uuid_unique" RENAME TO "website_website_uuid_key"; + +/* + Warnings: + + - You are about to drop the column `event_type` on the `event` table. All the data in the column will be lost. + - You are about to drop the column `event_value` on the `event` table. All the data in the column will be lost. + +*/ +-- Populate event_name +update event +set "event_name" = event.event_value || '-' || event.event_type; + +--Set event_name not null +ALTER TABLE "event" ALTER COLUMN "event_name" SET NOT NULL; + +-- Drop old columns +ALTER TABLE "event" DROP COLUMN "event_type", +DROP COLUMN "event_value"; diff --git a/db/postgresql/schema.prisma b/db/postgresql/schema.prisma index e425475f..2f2cc8de 100644 --- a/db/postgresql/schema.prisma +++ b/db/postgresql/schema.prisma @@ -9,7 +9,7 @@ datasource db { model account { user_id Int @id @default(autoincrement()) - username String @unique(map: "account.username_unique") @db.VarChar(255) + username String @unique @db.VarChar(255) password String @db.VarChar(60) is_admin Boolean @default(false) created_at DateTime? @default(now()) @db.Timestamptz(6) @@ -18,21 +18,28 @@ model account { } model event { - event_id Int @id @default(autoincrement()) - website_id Int - session_id Int - created_at DateTime? @default(now()) @db.Timestamptz(6) - url String @db.VarChar(500) - event_type String @db.VarChar(50) - event_value String @db.VarChar(50) - session session @relation(fields: [session_id], references: [session_id], onDelete: Cascade) - website website @relation(fields: [website_id], references: [website_id], onDelete: Cascade) + event_id Int @id @default(autoincrement()) + website_id Int + session_id Int + created_at DateTime? @default(now()) @db.Timestamptz(6) + url String @db.VarChar(500) + event_name String @db.VarChar(50) + session session @relation(fields: [session_id], references: [session_id], onDelete: Cascade) + website website @relation(fields: [website_id], references: [website_id], onDelete: Cascade) + eventData event_data? @@index([created_at]) @@index([session_id]) @@index([website_id]) } +model event_data { + id Int @id @default(autoincrement()) @map("event_data_id") + eventId Int @unique @map("event_id") + eventData Json @map("event_data") + event event @relation(fields: [eventId], references: [event_id]) +} + model pageview { view_id Int @id @default(autoincrement()) website_id Int @@ -52,7 +59,7 @@ model pageview { model session { session_id Int @id @default(autoincrement()) - session_uuid String @unique(map: "session.session_uuid_unique") @db.Uuid + session_uuid String @unique @db.Uuid website_id Int created_at DateTime? @default(now()) @db.Timestamptz(6) hostname String? @db.VarChar(100) @@ -72,11 +79,11 @@ model session { model website { website_id Int @id @default(autoincrement()) - website_uuid String @unique(map: "website.website_uuid_unique") @db.Uuid + website_uuid String @unique @db.Uuid user_id Int name String @db.VarChar(100) domain String? @db.VarChar(500) - share_id String? @unique(map: "website.share_id_unique") @db.VarChar(64) + share_id String? @unique @db.VarChar(64) created_at DateTime? @default(now()) @db.Timestamptz(6) account account @relation(fields: [user_id], references: [user_id], onDelete: Cascade) event event[]