Add spaces concept (#1776)

This commit is contained in:
Luke Vella 2025-06-15 11:48:51 +02:00 committed by GitHub
parent 92a72dde60
commit 04fcc0350f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 389 additions and 93 deletions

View file

@ -0,0 +1,34 @@
/*
Warnings:
- A unique constraint covering the columns `[space_id]` on the table `subscriptions` will be added. If there are existing duplicate values, this will fail.
*/
-- AlterTable
ALTER TABLE "polls" ADD COLUMN "space_id" TEXT;
-- AlterTable
ALTER TABLE "subscriptions" ADD COLUMN "space_id" TEXT;
-- CreateTable
CREATE TABLE "spaces" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"owner_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "spaces_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "subscriptions_space_id_key" ON "subscriptions"("space_id");
-- AddForeignKey
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "spaces"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "polls" ADD CONSTRAINT "polls_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "spaces"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "spaces" ADD CONSTRAINT "spaces_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View file

@ -0,0 +1,17 @@
INSERT INTO spaces (id, name, owner_id, created_at, updated_at)
SELECT gen_random_uuid(), 'Personal', id, NOW(), NOW()
FROM users
WHERE NOT EXISTS (
SELECT 1 FROM spaces WHERE spaces.owner_id = users.id
) ON CONFLICT DO NOTHING;
-- Set space_id for polls
UPDATE polls
SET space_id = spaces.id
FROM spaces
WHERE polls.user_id = spaces.owner_id AND polls.space_id IS NULL;
UPDATE subscriptions
SET space_id = spaces.id
FROM spaces
WHERE subscriptions.user_id = spaces.owner_id AND subscriptions.space_id IS NULL;

View file

@ -0,0 +1,15 @@
/*
Warnings:
- Added the required column `space_id` to the `scheduled_events` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "scheduled_events" ADD COLUMN "space_id" TEXT;
-- AddForeignKey
ALTER TABLE "scheduled_events" ADD CONSTRAINT "scheduled_events_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "spaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;
UPDATE "scheduled_events" SET "space_id" = (SELECT "id" FROM "spaces" WHERE "owner_id" = "scheduled_events"."user_id" LIMIT 1);
ALTER TABLE "scheduled_events" ALTER COLUMN "space_id" SET NOT NULL;