Skip to content

Commit

Permalink
chore: update Prisma schema
Browse files Browse the repository at this point in the history
  • Loading branch information
windchime-yk committed Oct 30, 2024
1 parent ae7d94c commit 1e07017
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ TiDB ServerlessとPrismaツール群を利用して、DB周りを楽したい
- [ ] Prismaファイル他の自動整形を追加
- [x] シネログのスキーマを移植し、移行ファイルを生成
- [x] Seedデータの反映スクリプトを作成
- [x] シネログの新しいスキーマを反映し、移行ファイルを生成
- [ ] HonoでREST APIを構築し、CRUDを構築
- [ ] シネログの新しいスキーマを反映し、移行ファイルを生成
- [ ] Prisma Accelerateを適用

## 参考資料
Expand Down
62 changes: 62 additions & 0 deletions prisma/migrations/20241030143918_init/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
65 changes: 65 additions & 0 deletions prisma/migrations/20241030144158_init/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
45 changes: 38 additions & 7 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
22 changes: 19 additions & 3 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1e07017

Please sign in to comment.