Skip to content

Commit

Permalink
feat(db): move to prisma (#1367)
Browse files Browse the repository at this point in the history
Co-authored-by: Mogyuchi <[email protected]>
  • Loading branch information
kazukazu123123 and Mogyuchi authored Jul 22, 2024
1 parent 08760c5 commit 46da125
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 152 deletions.
4 changes: 4 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ DB_PORT=
DB_USER=
DB_PASSWORD=
DB_DATABASE=

DATABASE_URL_BASE=mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}
DATABASE_URL=${DATABASE_URL_BASE}/${DB_DATABASE}
#SHADOW_DATABASE_URL=${DATABASE_URL_BASE}/pow-shadow
3 changes: 3 additions & 0 deletions .github/workflows/maintain-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
- name: Install dependencies
run: pnpm install --frozen-lockfile --strict-peer-dependencies

- name: Generate Prisma Client
run: pnpm run db:generate

- name: Check all
run: pnpm run --aggregate-output "/^(format:check|lint|check:types)$/"
env:
Expand Down
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"eslint.experimental.useFlatConfig": true
"eslint.experimental.useFlatConfig": true,
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma"
}
}
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"scripts": {
"start": "node .",
"dev": "run-p build:debug:watch dev:run",
"db:generate": "prisma generate",
"db:dev": "prisma migrate dev",
"db:deploy": "prisma migrate deploy",
"db:push": "prisma db push",
"dev:run": "node --watch -r dotenv/config . dotenv_config_override=true",
"build": "swc src --out-dir dist",
"build:debug": "swc --source-maps=true src --out-dir=dist",
Expand All @@ -27,6 +31,8 @@
"emoji-regex": "10.3.0",
"ffmpeg-static": "5.2.0",
"mariadb": "3.3.1",
"prisma": "5.17.0",
"@prisma/client": "5.17.0",
"sodium-native": "4.1.1"
},
"devDependencies": {
Expand Down
64 changes: 64 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions prisma/migrations/0_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- CreateTable
CREATE TABLE `connectionStates` (
`voiceChannel` BIGINT UNSIGNED NOT NULL,
`guild` BIGINT UNSIGNED NOT NULL,
`readChannel` BIGINT UNSIGNED NOT NULL,
`skipUser` TEXT NULL,

PRIMARY KEY (`voiceChannel` ASC)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `userSetting` (
`id` BIGINT NOT NULL DEFAULT 0,
`speaker` VARCHAR(20) NOT NULL DEFAULT '',
`pitch` INTEGER UNSIGNED NOT NULL,
`speed` INTEGER UNSIGNED NOT NULL,
`isDontRead` TINYINT NOT NULL DEFAULT 0,

PRIMARY KEY (`id` ASC)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

18 changes: 18 additions & 0 deletions prisma/migrations/20240722053023_change_to_enum/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Warnings:
- The primary key for the `userSetting` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to alter the column `id` on the `userSetting` table. The data in that column could be lost. The data in that column will be cast from `BigInt` to `UnsignedBigInt`.
- You are about to alter the column `speaker` on the `userSetting` table. The data in that column could be lost. The data in that column will be cast from `VarChar(20)` to `Enum(EnumId(0))`.
- Made the column `skipUser` on table `connectionstates` required. This step will fail if there are existing NULL values in that column.
*/
-- AlterTable
ALTER TABLE `connectionStates` MODIFY `skipUser` TEXT NOT NULL;

-- AlterTable
ALTER TABLE `userSetting` DROP PRIMARY KEY,
MODIFY `id` BIGINT UNSIGNED NOT NULL,
MODIFY `speaker` ENUM('show', 'haruka', 'hikari', 'takeru', 'santa', 'bear') NOT NULL,
MODIFY `isDontRead` BOOLEAN NOT NULL DEFAULT false,
ADD PRIMARY KEY (`id`);
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "mysql"
33 changes: 33 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

enum SpeakerList {
show
haruka
hikari
takeru
santa
bear
}

model connectionStates {
voiceChannel BigInt @id @db.UnsignedBigInt
guild BigInt @db.UnsignedBigInt
readChannel BigInt @db.UnsignedBigInt
skipUser String @db.Text
}

model userSetting {
id BigInt @id @db.UnsignedBigInt
speaker SpeakerList
pitch Int @db.UnsignedInt
speed Int @db.UnsignedInt
isDontRead Boolean @default(false)
}
23 changes: 13 additions & 10 deletions src/commands/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {
getErrorReply,
userSettingToString,
userSettingToDiff,
randomUserSetting,
} from '../utils.js'
import { getUserSetting, randomizeUserSetting, setUserSetting } from '../db.js'
import { getUserSetting, setUserSetting } from '../db.js'
import type { InteractionReplyOptions } from 'discord.js'
import { SpeakerList } from '@prisma/client'

export class UserSettingsCommand extends Subcommand {
public constructor(context: Subcommand.Context, options: Subcommand.Options) {
Expand Down Expand Up @@ -186,23 +188,22 @@ export class UserSettingsCommand extends Subcommand {
}

try {
const oldUserSetting = await getUserSetting(interaction.member.id)

const errorMsg: string[] = []

const { options } = interaction
const random = options.getBoolean('random')
const speaker = options.getString('speaker')
const speaker = options.getString('speaker') as SpeakerList | null
const pitch = options.getInteger('pitch')
const speed = options.getInteger('speed')

if (random) {
await randomizeUserSetting(interaction.member.id)
}
const oldUserSetting = await getUserSetting(interaction.member.id)
const newUserSetting = random
? randomUserSetting(oldUserSetting.id)
: { ...oldUserSetting }

if (speaker !== null) {
if (allowedVoiceList.includes(speaker)) {
await setUserSetting(interaction.member.id, 'speaker', speaker)
newUserSetting.speaker = speaker
} else {
errorMsg.push(
[
Expand All @@ -215,7 +216,7 @@ export class UserSettingsCommand extends Subcommand {

if (pitch !== null) {
if (pitch > 49 && pitch < 201) {
await setUserSetting(interaction.member.id, 'pitch', pitch)
newUserSetting.pitch = pitch
} else {
errorMsg.push(
`その声の高さ(${pitch}%)は指定できません。指定できる声の高さは、50%~200%です。`,
Expand All @@ -225,13 +226,15 @@ export class UserSettingsCommand extends Subcommand {

if (speed !== null) {
if (speed > 49 && speed < 401) {
await setUserSetting(interaction.member.id, 'speed', speed)
newUserSetting.speed = speed
} else {
errorMsg.push(
`その速度(${speed}%)は指定できません。指定できる声の速度は、50%~400%です。`,
)
}
}

await setUserSetting(newUserSetting)
const userSetting = await getUserSetting(interaction.member.id)

if (errorMsg.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/connectionCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class ConnectionCtxManager extends Map<
}
})
}
await deleteState({ voiceChannelId })
await deleteState({ voiceChannelId: BigInt(voiceChannelId) })
return workerId
}
}
Loading

0 comments on commit 46da125

Please sign in to comment.