From 1e0701738c0d943a2a86277bbd1d8c035f1cdb1c Mon Sep 17 00:00:00 2001 From: WhyK Date: Wed, 30 Oct 2024 23:49:39 +0900 Subject: [PATCH] chore: update Prisma schema --- README.md | 2 +- .../20241030143918_init/migration.sql | 62 ++++++++++++++++++ .../20241030144158_init/migration.sql | 65 +++++++++++++++++++ prisma/schema.prisma | 45 +++++++++++-- prisma/seed.ts | 22 ++++++- 5 files changed, 185 insertions(+), 11 deletions(-) create mode 100644 prisma/migrations/20241030143918_init/migration.sql create mode 100644 prisma/migrations/20241030144158_init/migration.sql diff --git a/README.md b/README.md index b980da8..afcbfb9 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ TiDB ServerlessとPrismaツール群を利用して、DB周りを楽したい - [ ] Prismaファイル他の自動整形を追加 - [x] シネログのスキーマを移植し、移行ファイルを生成 - [x] Seedデータの反映スクリプトを作成 +- [x] シネログの新しいスキーマを反映し、移行ファイルを生成 - [ ] HonoでREST APIを構築し、CRUDを構築 -- [ ] シネログの新しいスキーマを反映し、移行ファイルを生成 - [ ] Prisma Accelerateを適用 ## 参考資料 diff --git a/prisma/migrations/20241030143918_init/migration.sql b/prisma/migrations/20241030143918_init/migration.sql new file mode 100644 index 0000000..cc18856 --- /dev/null +++ b/prisma/migrations/20241030143918_init/migration.sql @@ -0,0 +1,62 @@ +/* + Warnings: + + - You are about to drop the column `accompanier` on the `tbl_movieinfo` table. All the data in the column will be lost. + - You are about to drop the column `is_domestic` on the `tbl_movieinfo` table. All the data in the column will be lost. + - You are about to drop the column `is_dubbed` on the `tbl_movieinfo` table. All the data in the column will be lost. + - You are about to drop the column `is_live_action` on the `tbl_movieinfo` table. All the data in the column will be lost. + - Added the required column `creater_country_id` to the `tbl_movieinfo` table without a default value. This is not possible if the table is not empty. + - Added the required column `is_subtitles` to the `tbl_movieinfo` table without a default value. This is not possible if the table is not empty. + - Added the required column `movie_format_id` to the `tbl_movieinfo` table without a default value. This is not possible if the table is not empty. + - Added the required column `screening_format_id` to the `tbl_movieinfo` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE `tbl_movieinfo` DROP COLUMN `accompanier`, + DROP COLUMN `is_domestic`, + DROP COLUMN `is_dubbed`, + DROP COLUMN `is_live_action`, + ADD COLUMN `companions_count` INTEGER NULL, + ADD COLUMN `creater_country_id` INTEGER NOT NULL, + ADD COLUMN `is_subtitles` BOOLEAN NOT NULL, + ADD COLUMN `movie_format_id` INTEGER NOT NULL, + ADD COLUMN `screening_format_id` INTEGER NOT NULL; + +-- CreateTable +CREATE TABLE `creaters_countries` ( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `name` VARCHAR(246) NOT NULL, + + UNIQUE INDEX `creaters_countries_id_key`(`id`), + UNIQUE INDEX `creaters_countries_name_key`(`name`), + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `movie_formats` ( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `name` VARCHAR(246) NOT NULL, + + UNIQUE INDEX `movie_formats_id_key`(`id`), + UNIQUE INDEX `movie_formats_name_key`(`name`), + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `screening_formats` ( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `name` VARCHAR(246) NOT NULL, + + UNIQUE INDEX `screening_formats_id_key`(`id`), + UNIQUE INDEX `screening_formats_name_key`(`name`), + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- AddForeignKey +ALTER TABLE `tbl_movieinfo` ADD CONSTRAINT `tbl_movieinfo_creater_country_id_fkey` FOREIGN KEY (`creater_country_id`) REFERENCES `creaters_countries`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `tbl_movieinfo` ADD CONSTRAINT `tbl_movieinfo_movie_format_id_fkey` FOREIGN KEY (`movie_format_id`) REFERENCES `movie_formats`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `tbl_movieinfo` ADD CONSTRAINT `tbl_movieinfo_screening_format_id_fkey` FOREIGN KEY (`screening_format_id`) REFERENCES `screening_formats`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20241030144158_init/migration.sql b/prisma/migrations/20241030144158_init/migration.sql new file mode 100644 index 0000000..0f413fa --- /dev/null +++ b/prisma/migrations/20241030144158_init/migration.sql @@ -0,0 +1,65 @@ +/* + Warnings: + + - You are about to drop the `tbl_movieinfo` table. If the table is not empty, all the data it contains will be lost. + - You are about to drop the `tbl_theater` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE `tbl_movieinfo` DROP FOREIGN KEY `tbl_movieinfo_creater_country_id_fkey`; + +-- DropForeignKey +ALTER TABLE `tbl_movieinfo` DROP FOREIGN KEY `tbl_movieinfo_movie_format_id_fkey`; + +-- DropForeignKey +ALTER TABLE `tbl_movieinfo` DROP FOREIGN KEY `tbl_movieinfo_screening_format_id_fkey`; + +-- DropForeignKey +ALTER TABLE `tbl_movieinfo` DROP FOREIGN KEY `tbl_movieinfo_theater_id_fkey`; + +-- DropTable +DROP TABLE `tbl_movieinfo`; + +-- DropTable +DROP TABLE `tbl_theater`; + +-- CreateTable +CREATE TABLE `movies` ( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `title` VARCHAR(246) NOT NULL, + `is_subtitles` BOOLEAN NOT NULL, + `theater_id` INTEGER NOT NULL, + `creater_country_id` INTEGER NOT NULL, + `movie_format_id` INTEGER NOT NULL, + `screening_format_id` INTEGER NOT NULL, + `view_start_datetime` DATETIME(3) NOT NULL, + `view_end_datetime` DATETIME(3) NOT NULL, + `companions_count` INTEGER NULL, + `rating` INTEGER NULL, + `comment` TEXT NULL, + + UNIQUE INDEX `movies_id_key`(`id`), + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `theaters` ( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `name` VARCHAR(246) NOT NULL, + + UNIQUE INDEX `theaters_id_key`(`id`), + UNIQUE INDEX `theaters_name_key`(`name`), + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- AddForeignKey +ALTER TABLE `movies` ADD CONSTRAINT `movies_theater_id_fkey` FOREIGN KEY (`theater_id`) REFERENCES `theaters`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `movies` ADD CONSTRAINT `movies_creater_country_id_fkey` FOREIGN KEY (`creater_country_id`) REFERENCES `creaters_countries`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `movies` ADD CONSTRAINT `movies_movie_format_id_fkey` FOREIGN KEY (`movie_format_id`) REFERENCES `movie_formats`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `movies` ADD CONSTRAINT `movies_screening_format_id_fkey` FOREIGN KEY (`screening_format_id`) REFERENCES `screening_formats`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 6d7f38a..0db62ae 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,19 +16,23 @@ datasource db { model Movie { id Int @id @unique @default(autoincrement()) title String @db.VarChar(246) - isDubbed Boolean @map("is_dubbed") - isDomestic Boolean @map("is_domestic") - isLiveAction Boolean @map("is_live_action") + isSubtitles Boolean @map("is_subtitles") theaterId Int @map("theater_id") + createrCountryId Int @map("creater_country_id") + movieFormatId Int @map("movie_format_id") + screeningFormatId Int @map("screening_format_id") viewStartDatetime DateTime @map("view_start_datetime") viewEndDatetime DateTime @map("view_end_datetime") - accompanier Int? + companionsCount Int? @map("companions_count") rating Int? comment String? @db.Text - theater Theater @relation(fields: [theaterId], references: [id]) + theater Theater @relation(fields: [theaterId], references: [id]) + createrCountry CreaterCountry @relation(fields: [createrCountryId], references: [id]) + movieFormat MovieFormat @relation(fields: [movieFormatId], references: [id]) + screeningFormat ScreeningFormat @relation(fields: [screeningFormatId], references: [id]) - @@map("tbl_movieinfo") + @@map("movies") } model Theater { @@ -37,5 +41,32 @@ model Theater { movies Movie[] - @@map("tbl_theater") + @@map("theaters") +} + +model CreaterCountry { + id Int @id @unique @default(autoincrement()) + name String @unique @db.VarChar(246) + + movies Movie[] + + @@map("creaters_countries") +} + +model MovieFormat { + id Int @id @unique @default(autoincrement()) + name String @unique @db.VarChar(246) + + movies Movie[] + + @@map("movie_formats") +} + +model ScreeningFormat { + id Int @id @unique @default(autoincrement()) + name String @unique @db.VarChar(246) + + movies Movie[] + + @@map("screening_formats") } diff --git a/prisma/seed.ts b/prisma/seed.ts index c098042..65ccdc6 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -8,13 +8,29 @@ async function main() { name: "Theater 1", }, }); + await client.createrCountry.create({ + data: { + name: "アメリカ合衆国", + }, + }); + await client.movieFormat.create({ + data: { + name: "2Dアニメーション", + }, + }); + await client.screeningFormat.create({ + data: { + name: "IMAX", + }, + }) await client.movie.create({ data: { title: "The Shawshank Redemption", - isDubbed: false, - isDomestic: true, - isLiveAction: true, + isSubtitles: true, theaterId: 1, + createrCountryId: 1, + movieFormatId: 1, + screeningFormatId: 1, viewStartDatetime: new Date("2022-01-01T00:00:00Z"), viewEndDatetime: new Date("2022-01-02T00:00:00Z"), rating: 2,