fix a typo (pallete -> palette) & move to postgres

This commit is contained in:
Grant 2024-03-11 20:52:51 -06:00
parent 72c9589582
commit 51e13dd4df
8 changed files with 130 additions and 107 deletions

View File

@ -10,7 +10,7 @@ Table User {
FactionMember FactionMember [not null] FactionMember FactionMember [not null]
} }
Table PalleteColor { Table PaletteColor {
id Int [pk, increment] id Int [pk, increment]
name String [not null] name String [not null]
hex String [unique, not null] hex String [unique, not null]
@ -25,7 +25,7 @@ Table Pixel {
color String [not null] color String [not null]
createdAt DateTime [default: `now()`, not null] createdAt DateTime [default: `now()`, not null]
user User [not null] user User [not null]
pallete PalleteColor [not null] pallete PaletteColor [not null]
} }
Table Faction { Table Faction {
@ -82,7 +82,7 @@ Table FactionSettingDefinition {
Ref: Pixel.userId > User.sub Ref: Pixel.userId > User.sub
Ref: Pixel.color > PalleteColor.hex Ref: Pixel.color > PaletteColor.hex
Ref: FactionMember.sub > User.sub Ref: FactionMember.sub > User.sub

View File

@ -1,27 +0,0 @@
-- CreateTable
CREATE TABLE "User" (
"sub" TEXT NOT NULL PRIMARY KEY,
"lastPixelTime" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- CreateTable
CREATE TABLE "PalleteColor" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"hex" TEXT NOT NULL
);
-- CreateTable
CREATE TABLE "Pixel" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"userId" TEXT NOT NULL,
"x" INTEGER NOT NULL,
"y" INTEGER NOT NULL,
"color" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Pixel_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("sub") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Pixel_color_fkey" FOREIGN KEY ("color") REFERENCES "PalleteColor" ("hex") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "PalleteColor_hex_key" ON "PalleteColor"("hex");

View File

@ -1,52 +0,0 @@
-- CreateTable
CREATE TABLE "Faction" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"image" TEXT
);
-- CreateTable
CREATE TABLE "FactionMember" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"sub" TEXT NOT NULL,
"factionId" TEXT NOT NULL,
CONSTRAINT "FactionMember_sub_fkey" FOREIGN KEY ("sub") REFERENCES "User" ("sub") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "FactionMember_factionId_fkey" FOREIGN KEY ("factionId") REFERENCES "Faction" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "FactionRole" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"level" INTEGER NOT NULL,
"factionId" TEXT NOT NULL,
CONSTRAINT "FactionRole_factionId_fkey" FOREIGN KEY ("factionId") REFERENCES "Faction" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "FactionSocial" (
"id" TEXT NOT NULL PRIMARY KEY,
"factionId" TEXT NOT NULL,
"title" TEXT,
"url" TEXT NOT NULL,
"position" INTEGER NOT NULL,
CONSTRAINT "FactionSocial_factionId_fkey" FOREIGN KEY ("factionId") REFERENCES "Faction" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "FactionSetting" (
"id" TEXT NOT NULL PRIMARY KEY,
"factionId" TEXT NOT NULL,
"key" TEXT NOT NULL,
"value" TEXT NOT NULL,
CONSTRAINT "FactionSetting_key_fkey" FOREIGN KEY ("key") REFERENCES "FactionSettingDefinition" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "FactionSetting_factionId_fkey" FOREIGN KEY ("factionId") REFERENCES "Faction" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "FactionSettingDefinition" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"type" TEXT NOT NULL,
"minimumLevel" INTEGER NOT NULL
);

View File

@ -1,12 +0,0 @@
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_User" (
"sub" TEXT NOT NULL PRIMARY KEY,
"lastPixelTime" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"pixelStack" INTEGER NOT NULL DEFAULT 0
);
INSERT INTO "new_User" ("lastPixelTime", "sub") SELECT "lastPixelTime", "sub" FROM "User";
DROP TABLE "User";
ALTER TABLE "new_User" RENAME TO "User";
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;

View File

@ -0,0 +1,115 @@
-- CreateTable
CREATE TABLE "User" (
"sub" TEXT NOT NULL,
"lastPixelTime" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"pixelStack" INTEGER NOT NULL DEFAULT 0,
CONSTRAINT "User_pkey" PRIMARY KEY ("sub")
);
-- CreateTable
CREATE TABLE "PaletteColor" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"hex" TEXT NOT NULL,
CONSTRAINT "PaletteColor_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Pixel" (
"id" SERIAL NOT NULL,
"userId" TEXT NOT NULL,
"x" INTEGER NOT NULL,
"y" INTEGER NOT NULL,
"color" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Pixel_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Faction" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"image" TEXT,
CONSTRAINT "Faction_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "FactionMember" (
"id" SERIAL NOT NULL,
"sub" TEXT NOT NULL,
"factionId" TEXT NOT NULL,
CONSTRAINT "FactionMember_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "FactionRole" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"level" INTEGER NOT NULL,
"factionId" TEXT NOT NULL,
CONSTRAINT "FactionRole_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "FactionSocial" (
"id" TEXT NOT NULL,
"factionId" TEXT NOT NULL,
"title" TEXT,
"url" TEXT NOT NULL,
"position" INTEGER NOT NULL,
CONSTRAINT "FactionSocial_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "FactionSetting" (
"id" TEXT NOT NULL,
"factionId" TEXT NOT NULL,
"key" TEXT NOT NULL,
"value" TEXT NOT NULL,
CONSTRAINT "FactionSetting_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "FactionSettingDefinition" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"type" TEXT NOT NULL,
"minimumLevel" INTEGER NOT NULL,
CONSTRAINT "FactionSettingDefinition_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "PaletteColor_hex_key" ON "PaletteColor"("hex");
-- AddForeignKey
ALTER TABLE "Pixel" ADD CONSTRAINT "Pixel_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("sub") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Pixel" ADD CONSTRAINT "Pixel_color_fkey" FOREIGN KEY ("color") REFERENCES "PaletteColor"("hex") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "FactionMember" ADD CONSTRAINT "FactionMember_sub_fkey" FOREIGN KEY ("sub") REFERENCES "User"("sub") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "FactionMember" ADD CONSTRAINT "FactionMember_factionId_fkey" FOREIGN KEY ("factionId") REFERENCES "Faction"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "FactionRole" ADD CONSTRAINT "FactionRole_factionId_fkey" FOREIGN KEY ("factionId") REFERENCES "Faction"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "FactionSocial" ADD CONSTRAINT "FactionSocial_factionId_fkey" FOREIGN KEY ("factionId") REFERENCES "Faction"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "FactionSetting" ADD CONSTRAINT "FactionSetting_key_fkey" FOREIGN KEY ("key") REFERENCES "FactionSettingDefinition"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "FactionSetting" ADD CONSTRAINT "FactionSetting_factionId_fkey" FOREIGN KEY ("factionId") REFERENCES "Faction"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -1,3 +1,3 @@
# Please do not edit this file manually # Please do not edit this file manually
# It should be added in your version-control system (i.e. Git) # It should be added in your version-control system (i.e. Git)
provider = "sqlite" provider = "postgresql"

View File

@ -10,7 +10,7 @@ generator dbml {
} }
datasource db { datasource db {
provider = "sqlite" provider = "postgresql"
url = env("DATABASE_URL") url = env("DATABASE_URL")
} }
@ -23,8 +23,7 @@ model User {
FactionMember FactionMember[] FactionMember FactionMember[]
} }
// TODO: i cannot spell, rename this to PaletteColor model PaletteColor {
model PalleteColor {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
name String name String
hex String @unique hex String @unique
@ -42,7 +41,7 @@ model Pixel {
createdAt DateTime @default(now()) createdAt DateTime @default(now())
user User @relation(fields: [userId], references: [sub]) user User @relation(fields: [userId], references: [sub])
pallete PalleteColor @relation(fields: [color], references: [hex]) pallete PaletteColor @relation(fields: [color], references: [hex])
} }
model Faction { model Faction {

View File

@ -5,11 +5,11 @@ import {
Pixel, Pixel,
ServerToClientEvents, ServerToClientEvents,
} from "@sc07-canvas/lib/src/net"; } from "@sc07-canvas/lib/src/net";
import { CanvasLib } from "@sc07-canvas/lib/src/canvas"; import { CanvasLib } from "@sc07-canvas/lib";
import { Server, Socket as RawSocket } from "socket.io"; import { Server, Socket as RawSocket } from "socket.io";
import { session } from "./Express"; import { session } from "./Express";
import Canvas from "./Canvas"; import Canvas from "./Canvas";
import { PalleteColor } from "@prisma/client"; import { PaletteColor } from "@prisma/client";
import { prisma } from "./prisma"; import { prisma } from "./prisma";
import { Logger } from "./Logger"; import { Logger } from "./Logger";
import { Redis } from "./redis"; import { Redis } from "./redis";
@ -40,14 +40,14 @@ const getSocketConfig = () => {
}; };
// this is terrible, another way to get the client config needs to be found // this is terrible, another way to get the client config needs to be found
let PALLETE: PalleteColor[] = []; let PALLETE: PaletteColor[] = [];
const PIXEL_TIMEOUT_MS = 1000; const PIXEL_TIMEOUT_MS = 1000;
prisma.palleteColor prisma.paletteColor
.findMany() .findMany()
.then((palleteColors) => { .then((paletteColors) => {
PALLETE = palleteColors; PALLETE = paletteColors;
Logger.info(`Loaded ${palleteColors.length} pallete colors`); Logger.info(`Loaded ${paletteColors.length} pallete colors`);
}) })
.catch((e) => { .catch((e) => {
Logger.error("Failed to get pallete colors", e); Logger.error("Failed to get pallete colors", e);
@ -188,7 +188,7 @@ export class SocketServer {
await user.modifyStack(-1); await user.modifyStack(-1);
const paletteColor = await prisma.palleteColor.findFirst({ const paletteColor = await prisma.paletteColor.findFirst({
where: { where: {
id: pixel.color, id: pixel.color,
}, },