Skip to content

Commit

Permalink
chore: delete gameusers id
Browse files Browse the repository at this point in the history
  • Loading branch information
Simirall committed Aug 26, 2024
1 parent e3c06b0 commit 65300a4
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 16 deletions.
11 changes: 11 additions & 0 deletions migrations/0006_lush_the_renegades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ALTER TABLE `GameUsers` RENAME TO `OldGameUsers`;
CREATE TABLE `GameUsers` (
`gameId` integer NOT NULL,
`userId` integer NOT NULL,
`gm` integer NOT NULL,
FOREIGN KEY (`gameId`) REFERENCES `Games`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`userId`) REFERENCES `Users`(`id`) ON UPDATE no action ON DELETE no action,
PRIMARY KEY (`gameId`, `userId`)
);
INSERT INTO `GameUsers` SELECT `gameId`, `userId`, `gm` FROM `OldGameUsers`;
DROP TABLE `OldGameUsers`;
222 changes: 222 additions & 0 deletions migrations/meta/0006_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{
"version": "6",
"dialect": "sqlite",
"id": "a1c01444-70b3-45b3-ac93-2919f69eb6aa",
"prevId": "3ff41688-48ed-493b-b3a5-eb75108762a8",
"tables": {
"GameUsers": {
"name": "GameUsers",
"columns": {
"gm": {
"name": "gm",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"gameId": {
"name": "gameId",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"userId": {
"name": "userId",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"GameUsers_gameId_Games_id_fk": {
"name": "GameUsers_gameId_Games_id_fk",
"tableFrom": "GameUsers",
"tableTo": "Games",
"columnsFrom": [
"gameId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"GameUsers_userId_Users_id_fk": {
"name": "GameUsers_userId_Users_id_fk",
"tableFrom": "GameUsers",
"tableTo": "Users",
"columnsFrom": [
"userId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"GameUsers_gameId_userId_pk": {
"columns": [
"gameId",
"userId"
],
"name": "GameUsers_gameId_userId_pk"
},
"gu_pk": {
"columns": [
"gameId",
"userId"
],
"name": "gu_pk"
}
},
"uniqueConstraints": {}
},
"Games": {
"name": "Games",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"date": {
"name": "date",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"madamisId": {
"name": "madamisId",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"Games_madamisId_Madamis_id_fk": {
"name": "Games_madamisId_Madamis_id_fk",
"tableFrom": "Games",
"tableTo": "Madamis",
"columnsFrom": [
"madamisId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"Madamis": {
"name": "Madamis",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"link": {
"name": "link",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"player": {
"name": "player",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"gmRequired": {
"name": "gmRequired",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"bought": {
"name": "bought",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"Madamis_link_unique": {
"name": "Madamis_link_unique",
"columns": [
"link"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"Users": {
"name": "Users",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"color": {
"name": "color",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
7 changes: 7 additions & 0 deletions migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
"when": 1724302449155,
"tag": "0005_cheerful_supernaut",
"breakpoints": true
},
{
"idx": 6,
"version": "6",
"when": 1724652126296,
"tag": "0006_lush_the_renegades",
"breakpoints": true
}
]
}
53 changes: 37 additions & 16 deletions schema.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { relations } from "drizzle-orm";
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import {
integer,
primaryKey,
sqliteTable,
text,
} from "drizzle-orm/sqlite-core";

// マダミスシナリオ情報テーブル
export const madamis = sqliteTable("Madamis", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
title: text("title").notNull(),
link: text("link").notNull().unique(),
player: integer("player").notNull(),
gmRequired: integer("gmRequired").notNull(),
bought: integer("bought").notNull(),
player: integer("player").notNull(), // プレイヤー人数
gmRequired: integer("gmRequired").notNull(), // GM必須シナリオか否か
bought: integer("bought").notNull(), // 買ったか否か
});

export const madamisRelations = relations(madamis, ({ many }) => ({
games: many(games),
}));

// 試合情報テーブル
export const games = sqliteTable("Games", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
date: text("date").notNull(),
date: text("date").notNull(), // 試合日

madamisId: integer("madamisId")
.references(() => madamis.id)
Expand All @@ -31,27 +38,41 @@ export const gamesRelations = relations(games, ({ one, many }) => ({
gameUsers: many(gameUsers),
}));

// ユーザー情報テーブル
export const users = sqliteTable("Users", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
color: text("color").notNull(),
color: text("color").notNull(), // カラーコード
});

export const usersRelations = relations(users, ({ many }) => ({
gameUsers: many(gameUsers),
}));

export const gameUsers = sqliteTable("GameUsers", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
gm: integer("gm").notNull(),
// 試合・ユーザーの中間テーブル
export const gameUsers = sqliteTable(
"GameUsers",
{
gm: integer("gm").notNull(), // GMか否か

gameId: integer("gameId")
.references(() => games.id)
.notNull(),
userId: integer("userId")
.references(() => users.id)
.notNull(),
});
gameId: integer("gameId")
.references(() => games.id)
.notNull(),
userId: integer("userId")
.references(() => users.id)
.notNull(),
},
(table) => {
// 複合主キー
return {
pk: primaryKey({ columns: [table.gameId, table.userId] }),
pkWithCustomName: primaryKey({
name: "gu_pk",
columns: [table.gameId, table.userId],
}),
};
}
);

export const usersToGamesRelations = relations(gameUsers, ({ one }) => ({
game: one(games, {
Expand Down

0 comments on commit 65300a4

Please sign in to comment.