Skip to content

Commit

Permalink
fix: refactor and improve Prisma id implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirasaki committed Apr 12, 2024
1 parent baefbbf commit 88d29ec
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
33 changes: 33 additions & 0 deletions prisma/migrations/20240412030727_refactorIds/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Warnings:
- The primary key for the `CommandStatistics` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `CommandStatistics` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `EmbedField` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `EmbedField` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- You are about to drop the column `guildId` on the `Guild` table. All the data in the column will be lost.
*/

-- DropIndex
DROP INDEX "Guild_guildId_key";

-- AlterTable
ALTER TABLE "CommandStatistics" DROP CONSTRAINT "CommandStatistics_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
ADD CONSTRAINT "CommandStatistics_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "EmbedField" DROP CONSTRAINT "EmbedField_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
ADD CONSTRAINT "EmbedField_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "Guild" DROP COLUMN "guildId";

-- CreateIndex
CREATE UNIQUE INDEX "CommandStatistics_id_key" ON "CommandStatistics"("id");

-- CreateIndex
CREATE UNIQUE INDEX "EmbedField_id_key" ON "EmbedField"("id");
17 changes: 8 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ datasource db {
}

model User {
id String @id @unique
id String @id @unique
members Member[]
}

model Member {
id String @id @unique
id String @id @unique @default(cuid())
userId String
user User @relation(fields: [userId], references: [id])
guildId String
guild Guild @relation(fields: [guildId], references: [id])
}

model Guild {
id String @id @unique
guildId String @unique
Members Member[]
id String @id @unique
Members Member[]
adminRoleId String?
adminLogChannelId String?
Expand All @@ -46,7 +45,7 @@ model Guild {
}

model CommandCooldown {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
cooldownId String @unique
duration Int
usages DateTime[]
Expand All @@ -55,7 +54,7 @@ model CommandCooldown {
// Configurable Messages for users
// Check out the join/leave feed for an example
model Embed {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
messageText String?
color Int?
authorName String?
Expand All @@ -75,7 +74,7 @@ model Embed {
}

model EmbedField {
id String @id @unique
id Int @id @unique @default(autoincrement())
name String
value String
inline Boolean
Expand All @@ -84,7 +83,7 @@ model EmbedField {
}

model CommandStatistics {
id String @id @unique
id Int @id @unique @default(autoincrement())
type Int
commandId String @unique
Expand Down
6 changes: 3 additions & 3 deletions src/chat-input/Administrator/embeds/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ export const configureEmbedController: EmbedController = async (
? resolveColor(`#${configureEmbedData.color.replaceAll('#', '')}`)
: null;
const createEmbedData: Prisma.EmbedCreateInput = {
memberJoinEmbed: {
[settingKey]: {
connect: {
guildId: interaction.guildId,
id: guildSettings.id,
},
},
messageText: configureEmbedMessage ?? null,
Expand All @@ -241,7 +241,7 @@ export const configureEmbedController: EmbedController = async (
update: upsertData,
create: createEmbedData,
where: {
id: upsertId!,
id: upsertId,
},
include: { fields: true },
});
Expand Down
7 changes: 3 additions & 4 deletions src/database/Guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type GuildWithEmbeds = Prisma.GuildGetPayload<{

export const guildSettingsFromDb = async (guildId: string): Promise<GuildWithEmbeds> => {
const guild = await prisma.guild.findUnique({
where: { guildId: guildId },
where: { id: guildId },
include: {
memberJoinEmbed: {
include: { fields: true },
Expand All @@ -31,7 +31,7 @@ export const guildSettingsFromDb = async (guildId: string): Promise<GuildWithEmb
return (
guild ??
(await prisma.guild.create({
data: { guildId },
data: { id: guildId },
include: {
memberJoinEmbed: {
include: { fields: true },
Expand Down Expand Up @@ -62,7 +62,6 @@ export const updateGuildSettings = async (
const updatedGuild = await prisma.guild.update({
...updateArgs,
where: {
guildId: guildSettings.guildId,
id: guildSettings.id,
},
include: {
Expand All @@ -74,6 +73,6 @@ export const updateGuildSettings = async (
},
},
});
guildTTLCache.set(guildSettings.guildId, updatedGuild);
guildTTLCache.set(guildSettings.id, updatedGuild);
return updatedGuild;
};

0 comments on commit 88d29ec

Please sign in to comment.