-- CreateEnum CREATE TYPE "MoneyUpdateType" AS ENUM ('START', 'NEW_RUN', 'WIN_CHALLENGE', 'BUY_TRAIN'); -- CreateTable CREATE TABLE "Player" ( "id" SERIAL NOT NULL, "name" TEXT NOT NULL, "password" TEXT NOT NULL, "money" INTEGER NOT NULL DEFAULT 0, CONSTRAINT "Player_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "Game" ( "id" SERIAL NOT NULL, "started" BOOLEAN NOT NULL DEFAULT false, "currentRunId" INTEGER, CONSTRAINT "Game_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "PlayerRun" ( "id" SERIAL NOT NULL, "gameId" INTEGER NOT NULL, "runnerId" INTEGER NOT NULL, "start" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "end" TIMESTAMPTZ(3), CONSTRAINT "PlayerRun_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "Geolocation" ( "id" SERIAL NOT NULL, "playerId" INTEGER NOT NULL, "longitude" DOUBLE PRECISION NOT NULL, "latitude" DOUBLE PRECISION NOT NULL, "speed" DOUBLE PRECISION NOT NULL, "accuracy" DOUBLE PRECISION NOT NULL, "altitude" DOUBLE PRECISION NOT NULL, "altitudeAccuracy" DOUBLE PRECISION NOT NULL, "timestamp" TIMESTAMPTZ(3) NOT NULL, CONSTRAINT "Geolocation_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "Challenge" ( "id" SERIAL NOT NULL, "title" TEXT NOT NULL, "description" TEXT NOT NULL, "reward" INTEGER NOT NULL, CONSTRAINT "Challenge_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "ChallengeAction" ( "id" SERIAL NOT NULL, "playerId" INTEGER NOT NULL, "challengeId" INTEGER NOT NULL, "active" BOOLEAN NOT NULL DEFAULT false, "success" BOOLEAN NOT NULL DEFAULT false, "start" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "end" TIMESTAMPTZ(3), "penaltyStart" TIMESTAMPTZ(3), "penaltyEnd" TIMESTAMPTZ(3), "runId" INTEGER NOT NULL, CONSTRAINT "ChallengeAction_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "TrainTrip" ( "id" TEXT NOT NULL, "playerId" INTEGER NOT NULL, "distance" DOUBLE PRECISION NOT NULL, "from" TEXT NOT NULL, "to" TEXT NOT NULL, "departureTime" TIMESTAMPTZ(3) NOT NULL, "arrivalTime" TIMESTAMPTZ(3) NOT NULL, "infoJson" JSONB NOT NULL, "runId" INTEGER NOT NULL, CONSTRAINT "TrainTrip_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "MoneyUpdate" ( "id" SERIAL NOT NULL, "playerId" INTEGER NOT NULL, "amount" INTEGER NOT NULL, "reason" "MoneyUpdateType" NOT NULL, "actionId" INTEGER, "tripId" TEXT, "runId" INTEGER, "timestamp" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "MoneyUpdate_pkey" PRIMARY KEY ("id") ); -- CreateIndex CREATE UNIQUE INDEX "Player_name_key" ON "Player"("name"); -- CreateIndex CREATE UNIQUE INDEX "Game_currentRunId_key" ON "Game"("currentRunId"); -- CreateIndex CREATE UNIQUE INDEX "Challenge_title_key" ON "Challenge"("title"); -- CreateIndex CREATE UNIQUE INDEX "ChallengeAction_challengeId_key" ON "ChallengeAction"("challengeId"); -- CreateIndex CREATE UNIQUE INDEX "MoneyUpdate_actionId_key" ON "MoneyUpdate"("actionId"); -- CreateIndex CREATE UNIQUE INDEX "MoneyUpdate_tripId_key" ON "MoneyUpdate"("tripId"); -- CreateIndex CREATE UNIQUE INDEX "MoneyUpdate_runId_key" ON "MoneyUpdate"("runId"); -- AddForeignKey ALTER TABLE "Game" ADD CONSTRAINT "Game_currentRunId_fkey" FOREIGN KEY ("currentRunId") REFERENCES "PlayerRun"("id") ON DELETE SET NULL ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "PlayerRun" ADD CONSTRAINT "PlayerRun_gameId_fkey" FOREIGN KEY ("gameId") REFERENCES "Game"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "PlayerRun" ADD CONSTRAINT "PlayerRun_runnerId_fkey" FOREIGN KEY ("runnerId") REFERENCES "Player"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "Geolocation" ADD CONSTRAINT "Geolocation_playerId_fkey" FOREIGN KEY ("playerId") REFERENCES "Player"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "ChallengeAction" ADD CONSTRAINT "ChallengeAction_playerId_fkey" FOREIGN KEY ("playerId") REFERENCES "Player"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "ChallengeAction" ADD CONSTRAINT "ChallengeAction_challengeId_fkey" FOREIGN KEY ("challengeId") REFERENCES "Challenge"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "ChallengeAction" ADD CONSTRAINT "ChallengeAction_runId_fkey" FOREIGN KEY ("runId") REFERENCES "PlayerRun"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "TrainTrip" ADD CONSTRAINT "TrainTrip_playerId_fkey" FOREIGN KEY ("playerId") REFERENCES "Player"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "TrainTrip" ADD CONSTRAINT "TrainTrip_runId_fkey" FOREIGN KEY ("runId") REFERENCES "PlayerRun"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "MoneyUpdate" ADD CONSTRAINT "MoneyUpdate_playerId_fkey" FOREIGN KEY ("playerId") REFERENCES "Player"("id") ON DELETE RESTRICT ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "MoneyUpdate" ADD CONSTRAINT "MoneyUpdate_actionId_fkey" FOREIGN KEY ("actionId") REFERENCES "ChallengeAction"("id") ON DELETE SET NULL ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "MoneyUpdate" ADD CONSTRAINT "MoneyUpdate_tripId_fkey" FOREIGN KEY ("tripId") REFERENCES "TrainTrip"("id") ON DELETE SET NULL ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "MoneyUpdate" ADD CONSTRAINT "MoneyUpdate_runId_fkey" FOREIGN KEY ("runId") REFERENCES "PlayerRun"("id") ON DELETE SET NULL ON UPDATE CASCADE;