/*
  Warnings:

  - You are about to drop the column `authorId` on the `Publication` table. All the data in the column will be lost.
  - You are about to drop the column `data` on the `Publication` table. All the data in the column will be lost.
  - You are about to drop the column `originalId` on the `Schema` table. All the data in the column will be lost.
  - You are about to drop the column `schema` on the `Schema` table. All the data in the column will be lost.
  - You are about to drop the column `types` on the `Schema` table. All the data in the column will be lost.
  - You are about to drop the column `version` on the `Schema` table. All the data in the column will be lost.
  - You are about to drop the `TagsOnPublications` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `TagsOnTags` table. If the table is not empty, all the data it contains will be lost.
  - Added the required column `schemaVersion` to the `Publication` table without a default value. This is not possible if the table is not empty.
  - Added the required column `publicationRevisionId` to the `Tag` table without a default value. This is not possible if the table is not empty.
  - Made the column `publicationId` on table `Tag` required. This step will fail if there are existing NULL values in that column.

*/
-- DropForeignKey
ALTER TABLE "Publication" DROP CONSTRAINT "Publication_authorId_fkey";

-- DropForeignKey
ALTER TABLE "Tag" DROP CONSTRAINT "Tag_publicationId_fkey";

-- DropForeignKey
ALTER TABLE "TagsOnPublications" DROP CONSTRAINT "TagsOnPublications_publicationId_fkey";

-- DropForeignKey
ALTER TABLE "TagsOnPublications" DROP CONSTRAINT "TagsOnPublications_tagId_fkey";

-- DropForeignKey
ALTER TABLE "TagsOnTags" DROP CONSTRAINT "TagsOnTags_childTagId_fkey";

-- DropForeignKey
ALTER TABLE "TagsOnTags" DROP CONSTRAINT "TagsOnTags_parentTagId_fkey";

-- AlterTable
ALTER TABLE "GameSystem" ADD COLUMN     "isPublic" BOOLEAN NOT NULL DEFAULT false;

-- AlterTable
ALTER TABLE "Publication" DROP COLUMN "authorId",
DROP COLUMN "data",
ADD COLUMN     "schemaVersion" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "Schema" DROP COLUMN "originalId",
DROP COLUMN "schema",
DROP COLUMN "types",
DROP COLUMN "version";

-- AlterTable
ALTER TABLE "Tag" ADD COLUMN     "publicationRevisionId" TEXT NOT NULL,
ALTER COLUMN "publicationId" SET NOT NULL;

-- DropTable
DROP TABLE "TagsOnPublications";

-- DropTable
DROP TABLE "TagsOnTags";

-- CreateTable
CREATE TABLE "SchemaRevision" (
    "id" TEXT NOT NULL,
    "schemaId" TEXT NOT NULL,
    "fields" JSONB NOT NULL DEFAULT '{}',
    "types" JSONB NOT NULL DEFAULT '{}',
    "version" INTEGER NOT NULL DEFAULT 0,
    "isFinal" BOOLEAN NOT NULL DEFAULT false,

    CONSTRAINT "SchemaRevision_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "PublicationRevision" (
    "id" TEXT NOT NULL,
    "previousId" TEXT,
    "publicationId" TEXT NOT NULL,
    "authorId" TEXT NOT NULL,
    "version" TEXT NOT NULL,
    "isFinal" BOOLEAN NOT NULL DEFAULT false,
    "data" JSONB NOT NULL DEFAULT '{}',

    CONSTRAINT "PublicationRevision_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "GameSystemFollows" (
    "gameSystemId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,

    CONSTRAINT "GameSystemFollows_pkey" PRIMARY KEY ("userId","gameSystemId")
);

-- CreateIndex
CREATE UNIQUE INDEX "SchemaRevision_schemaId_version_key" ON "SchemaRevision"("schemaId", "version");

-- CreateIndex
CREATE UNIQUE INDEX "PublicationRevision_publicationId_version_key" ON "PublicationRevision"("publicationId", "version");

-- AddForeignKey
ALTER TABLE "SchemaRevision" ADD CONSTRAINT "SchemaRevision_schemaId_fkey" FOREIGN KEY ("schemaId") REFERENCES "Schema"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Publication" ADD CONSTRAINT "Publication_schemaVersion_schemaId_fkey" FOREIGN KEY ("schemaVersion", "schemaId") REFERENCES "SchemaRevision"("version", "schemaId") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PublicationRevision" ADD CONSTRAINT "PublicationRevision_publicationId_fkey" FOREIGN KEY ("publicationId") REFERENCES "Publication"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PublicationRevision" ADD CONSTRAINT "PublicationRevision_previousId_fkey" FOREIGN KEY ("previousId") REFERENCES "PublicationRevision"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PublicationRevision" ADD CONSTRAINT "PublicationRevision_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Tag" ADD CONSTRAINT "Tag_publicationId_fkey" FOREIGN KEY ("publicationId") REFERENCES "Publication"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Tag" ADD CONSTRAINT "Tag_publicationRevisionId_fkey" FOREIGN KEY ("publicationRevisionId") REFERENCES "PublicationRevision"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "GameSystemFollows" ADD CONSTRAINT "GameSystemFollows_gameSystemId_fkey" FOREIGN KEY ("gameSystemId") REFERENCES "GameSystem"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "GameSystemFollows" ADD CONSTRAINT "GameSystemFollows_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
