2021-03-25 19:29:24 +00:00
|
|
|
-- CreateTable
|
|
|
|
CREATE TABLE "users" (
|
|
|
|
"id" SERIAL NOT NULL,
|
|
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2021-05-22 00:09:11 +00:00
|
|
|
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2021-03-25 19:29:24 +00:00
|
|
|
"name" TEXT,
|
|
|
|
"email" TEXT,
|
|
|
|
"email_verified" TIMESTAMP(3),
|
|
|
|
"image" TEXT,
|
|
|
|
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
|
|
|
|
-- CreateTable
|
|
|
|
CREATE TABLE "Message" (
|
|
|
|
"id" SERIAL NOT NULL,
|
|
|
|
"text" TEXT NOT NULL,
|
|
|
|
"userId" INTEGER NOT NULL,
|
|
|
|
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
|
2021-04-27 21:30:58 +00:00
|
|
|
|
2021-03-25 19:29:24 +00:00
|
|
|
-- Edit: create extension for path
|
|
|
|
CREATE EXTENSION ltree;
|
|
|
|
|
|
|
|
-- CreateTable
|
|
|
|
CREATE TABLE "Item" (
|
|
|
|
"id" SERIAL NOT NULL,
|
|
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
"updated_at" TIMESTAMP(3) NOT NULL,
|
2021-04-14 00:57:32 +00:00
|
|
|
"title" TEXT,
|
2021-04-12 18:05:09 +00:00
|
|
|
"text" TEXT,
|
|
|
|
"url" TEXT,
|
2021-03-25 19:29:24 +00:00
|
|
|
"userId" INTEGER NOT NULL,
|
|
|
|
"parentId" INTEGER,
|
2021-04-12 18:05:09 +00:00
|
|
|
"path" LTREE,
|
2021-03-25 19:29:24 +00:00
|
|
|
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Edit: create index for path
|
2021-04-12 18:05:09 +00:00
|
|
|
CREATE INDEX "item_gist_path_index" ON "Item" USING GIST ("path");
|
2021-03-25 19:29:24 +00:00
|
|
|
|
|
|
|
-- Edit: create trigger for path
|
2021-04-12 18:05:09 +00:00
|
|
|
CREATE OR REPLACE FUNCTION update_item_path() RETURNS TRIGGER AS $$
|
2021-03-25 19:29:24 +00:00
|
|
|
DECLARE
|
2021-04-12 18:05:09 +00:00
|
|
|
npath ltree;
|
2021-03-25 19:29:24 +00:00
|
|
|
BEGIN
|
2021-04-12 18:05:09 +00:00
|
|
|
IF NEW."parentId" IS NULL THEN
|
|
|
|
SELECT NEW.id::text::ltree INTO npath;
|
|
|
|
ELSEIF TG_OP = 'INSERT' OR OLD."parentId" IS NULL OR OLD."parentId" != NEW."parentId" THEN
|
|
|
|
SELECT "path" || NEW.id::text FROM "Item" WHERE id = NEW."parentId" INTO npath;
|
|
|
|
IF npath IS NULL THEN
|
|
|
|
RAISE EXCEPTION 'Invalid parent_id %', NEW."parentId";
|
2021-03-25 19:29:24 +00:00
|
|
|
END IF;
|
|
|
|
END IF;
|
2021-04-12 18:05:09 +00:00
|
|
|
NEW."path" = npath;
|
2021-03-25 19:29:24 +00:00
|
|
|
RETURN NEW;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
|
2021-04-12 18:05:09 +00:00
|
|
|
CREATE TRIGGER path_tgr
|
2021-03-25 19:29:24 +00:00
|
|
|
BEFORE INSERT OR UPDATE ON "Item"
|
2021-04-12 18:05:09 +00:00
|
|
|
FOR EACH ROW EXECUTE PROCEDURE update_item_path();
|
2021-03-25 19:29:24 +00:00
|
|
|
|
2021-04-27 21:30:58 +00:00
|
|
|
-- CreateTable
|
|
|
|
CREATE TABLE "Vote" (
|
|
|
|
"id" SERIAL NOT NULL,
|
|
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
"updated_at" TIMESTAMP(3) NOT NULL,
|
|
|
|
"sats" INTEGER NOT NULL DEFAULT 1,
|
|
|
|
"boost" BOOLEAN NOT NULL DEFAULT false,
|
|
|
|
"itemId" INTEGER NOT NULL,
|
|
|
|
"userId" INTEGER NOT NULL,
|
|
|
|
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
|
2021-03-25 19:29:24 +00:00
|
|
|
-- CreateTable
|
|
|
|
CREATE TABLE "accounts" (
|
|
|
|
"id" SERIAL NOT NULL,
|
|
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
"updated_at" TIMESTAMP(3) NOT NULL,
|
|
|
|
"compound_id" TEXT NOT NULL,
|
|
|
|
"user_id" INTEGER NOT NULL,
|
|
|
|
"provider_type" TEXT NOT NULL,
|
|
|
|
"provider_id" TEXT NOT NULL,
|
|
|
|
"provider_account_id" TEXT NOT NULL,
|
|
|
|
"refresh_token" TEXT,
|
|
|
|
"access_token" TEXT,
|
|
|
|
"access_token_expires" TIMESTAMP(3),
|
|
|
|
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
|
|
|
|
-- CreateTable
|
|
|
|
CREATE TABLE "sessions" (
|
|
|
|
"id" SERIAL NOT NULL,
|
|
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
"updated_at" TIMESTAMP(3) NOT NULL,
|
|
|
|
"user_id" INTEGER NOT NULL,
|
|
|
|
"expires" TIMESTAMP(3) NOT NULL,
|
|
|
|
"session_token" TEXT NOT NULL,
|
|
|
|
"access_token" TEXT NOT NULL,
|
|
|
|
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
|
|
|
|
-- CreateTable
|
|
|
|
CREATE TABLE "verification_requests" (
|
|
|
|
"id" SERIAL NOT NULL,
|
|
|
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
"updated_at" TIMESTAMP(3) NOT NULL,
|
|
|
|
"identifier" TEXT NOT NULL,
|
|
|
|
"token" TEXT NOT NULL,
|
|
|
|
"expires" TIMESTAMP(3) NOT NULL,
|
|
|
|
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE UNIQUE INDEX "users.name_unique" ON "users"("name");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE UNIQUE INDEX "users.email_unique" ON "users"("email");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE INDEX "Item.userId_index" ON "Item"("userId");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE INDEX "Item.parentId_index" ON "Item"("parentId");
|
|
|
|
|
2021-04-27 21:30:58 +00:00
|
|
|
-- CreateIndex
|
|
|
|
CREATE INDEX "Vote.itemId_index" ON "Vote"("itemId");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE INDEX "Vote.userId_index" ON "Vote"("userId");
|
|
|
|
|
2021-03-25 19:29:24 +00:00
|
|
|
-- CreateIndex
|
|
|
|
CREATE UNIQUE INDEX "accounts.compound_id_unique" ON "accounts"("compound_id");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE INDEX "accounts.provider_account_id_index" ON "accounts"("provider_account_id");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE INDEX "accounts.provider_id_index" ON "accounts"("provider_id");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE INDEX "accounts.user_id_index" ON "accounts"("user_id");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE UNIQUE INDEX "sessions.session_token_unique" ON "sessions"("session_token");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE UNIQUE INDEX "sessions.access_token_unique" ON "sessions"("access_token");
|
|
|
|
|
|
|
|
-- CreateIndex
|
|
|
|
CREATE UNIQUE INDEX "verification_requests.token_unique" ON "verification_requests"("token");
|
|
|
|
|
|
|
|
-- AddForeignKey
|
|
|
|
ALTER TABLE "Message" ADD FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
|
|
|
|
-- AddForeignKey
|
|
|
|
ALTER TABLE "Item" ADD FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
|
|
|
|
-- AddForeignKey
|
|
|
|
ALTER TABLE "Item" ADD FOREIGN KEY ("parentId") REFERENCES "Item"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
2021-04-27 21:30:58 +00:00
|
|
|
|
|
|
|
-- AddForeignKey
|
|
|
|
ALTER TABLE "Vote" ADD FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
|
|
|
|
-- AddForeignKey
|
|
|
|
ALTER TABLE "Vote" ADD FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|