Skip to content

Commit

Permalink
Backend タイムライン実験など (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi authored Nov 9, 2024
1 parent 2872bb1 commit a131982
Show file tree
Hide file tree
Showing 27 changed files with 534 additions and 266 deletions.
33 changes: 25 additions & 8 deletions backend/docs/development.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
## DBのマイグレーションを作成する
開発時にDrizzleのスキーマの変更が生じた場合は、\
以下のコマンドを実行してDBのマイグレーションを作成・適用してください。
## 開発時: DBをリセットする
現在あるDBを削除し、DBをマイグレーションが適用された状態まで進めます。
```
pnpm migration:generate --name [このマイグレーションの名前]
pnpm migration:apply
pnpm db:reset
```

例:
まだマイグレーションが作成されていないスキーマの変更をDBに適用します。\
マイグレーションとスキーマが同じ状態であればこの操作は必要ありません。
```
pnpm migration:generate --name honi
pnpm migration:apply
pnpm db:push
```

DBに初期データを作成します。
```
pnpm db:seed
```

## 開発時: スキーマ変更を適用する
開発時は、基本的にマイグレーションの作成を行わず、
DBのスキーマ変更だけを行います。
```
pnpm db:push
```

## マイグレーションの作成
DBに適用するスキーマの変更が確定したら、マイグレーションを作成します。
```
pnpm db:push
pnpm db:create-migration
```
24 changes: 18 additions & 6 deletions backend/openapi/generated/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ paths:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Api.v1.Leaf'
$ref: '#/components/schemas/Api.v1.GetTimelineResult'
/api/v1/chatRoom/searchChatRooms:
get:
tags:
Expand Down Expand Up @@ -328,9 +326,7 @@ paths:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Api.v1.Leaf'
$ref: '#/components/schemas/Api.v1.GetTimelineResult'
/api/v1/user/getUser:
get:
tags:
Expand Down Expand Up @@ -540,12 +536,26 @@ components:
type: string
limit:
type: string
Api.v1.GetTimelineResult:
type: object
required:
- leafs
properties:
leafs:
type: array
items:
$ref: '#/components/schemas/Api.v1.Leaf'
nextCursor:
type: string
prevCursor:
type: string
Api.v1.Leaf:
type: object
required:
- leafId
- userId
- content
- createdAt
properties:
leafId:
type: string
Expand All @@ -555,6 +565,8 @@ components:
type: string
content:
type: string
createdAt:
type: string
Api.v1.SearchChatRoomsQueryString:
type: object
properties:
Expand Down
10 changes: 8 additions & 2 deletions backend/openapi/generated/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,17 @@ export interface components {
prevCursor?: string;
limit?: string;
};
"Api.v1.GetTimelineResult": {
leafs: components["schemas"]["Api.v1.Leaf"][];
nextCursor?: string;
prevCursor?: string;
};
"Api.v1.Leaf": {
leafId: string;
chatRoomId?: string;
userId: string;
content: string;
createdAt: string;
};
"Api.v1.SearchChatRoomsQueryString": {
offset?: string;
Expand Down Expand Up @@ -590,7 +596,7 @@ export interface operations {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Api.v1.Leaf"][];
"application/json": components["schemas"]["Api.v1.GetTimelineResult"];
};
};
};
Expand Down Expand Up @@ -848,7 +854,7 @@ export interface operations {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Api.v1.Leaf"][];
"application/json": components["schemas"]["Api.v1.GetTimelineResult"];
};
};
};
Expand Down
14 changes: 9 additions & 5 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"clean": "rm -rf dist",
"update-spec": "mkdir -p openapi/generated && cp -r ../spec/generated/* openapi/generated",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"prisma:migrate-create": "prisma migrate dev --create-only",
"prisma:migrate": "prisma migrate dev",
"prisma:push":"prisma db push",
"prisma:generate": "prisma generate",
"prisma:generate-sql": "prisma generate --sql",
"db:push": "prisma db push",
"db:reset": "prisma migrate reset --skip-seed",
"db:seed": "prisma db seed",
"db:create-migration": "prisma migrate dev --create-only",
"db:generate": "prisma generate && prisma generate --sql",
"start": "node --enable-source-maps dist/main",
"start:dev": "node --enable-source-maps --watch dist/main",
"start:prod": "node dist/main",
Expand All @@ -22,6 +22,9 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage"
},
"prisma": {
"seed": "ts-node --transpile-only prisma/seed.ts"
},
"dependencies": {
"@prisma/client": "^5.19.0",
"express": "^4.17.0",
Expand All @@ -30,6 +33,7 @@
"passport-http-bearer": "^1.0.1",
"reflect-metadata": "^0.2.0",
"threads": "^1.7.0",
"ts-node": "^10.9.2",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
34 changes: 12 additions & 22 deletions backend/pnpm-lock.yaml

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

11 changes: 0 additions & 11 deletions backend/prisma/migrations/20240907002954_relations/migration.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
-- CreateTable
CREATE TABLE "user" (
"user_id" UUID NOT NULL DEFAULT gen_random_uuid(),
"name" VARCHAR(64) NOT NULL,
"display_name" VARCHAR(64) NOT NULL DEFAULT 'frost user',
"password_auth_enabled" BOOLEAN NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "user_pkey" PRIMARY KEY ("user_id")
);

-- CreateTable
CREATE TABLE "password_verification" (
"password_verification_id" UUID NOT NULL DEFAULT gen_random_uuid(),
Expand All @@ -10,23 +21,14 @@ CREATE TABLE "password_verification" (
CONSTRAINT "password_verification_pkey" PRIMARY KEY ("password_verification_id")
);

-- CreateTable
CREATE TABLE "post" (
"post_id" UUID NOT NULL DEFAULT gen_random_uuid(),
"chat_room_id" UUID,
"user_id" UUID NOT NULL,
"content" VARCHAR(256) NOT NULL,

CONSTRAINT "post_pkey" PRIMARY KEY ("post_id")
);

-- CreateTable
CREATE TABLE "token" (
"token_id" UUID NOT NULL DEFAULT gen_random_uuid(),
"token_kind" VARCHAR(16) NOT NULL,
"user_id" UUID NOT NULL,
"token" VARCHAR(32) NOT NULL,
"expires" TIMESTAMP(6),
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "token_pkey" PRIMARY KEY ("token_id")
);
Expand All @@ -40,15 +42,14 @@ CREATE TABLE "token_scope" (
CONSTRAINT "token_scope_pkey" PRIMARY KEY ("token_scope_id")
);

-- CreateTable
CREATE TABLE "user" (
"user_id" UUID NOT NULL DEFAULT gen_random_uuid(),
"name" VARCHAR(64) NOT NULL,
"display_name" VARCHAR(64) NOT NULL DEFAULT 'frost user',
"password_auth_enabled" BOOLEAN NOT NULL,

CONSTRAINT "user_pkey" PRIMARY KEY ("user_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "user_name_unique" ON "user"("name");

-- AddForeignKey
ALTER TABLE "password_verification" ADD CONSTRAINT "password_verification_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("user_id") ON DELETE RESTRICT ON UPDATE RESTRICT;

-- AddForeignKey
ALTER TABLE "token" ADD CONSTRAINT "token_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("user_id") ON DELETE RESTRICT ON UPDATE RESTRICT;

-- AddForeignKey
ALTER TABLE "token_scope" ADD CONSTRAINT "token_scope_token_id_fkey" FOREIGN KEY ("token_id") REFERENCES "token"("token_id") ON DELETE RESTRICT ON UPDATE RESTRICT;
Loading

0 comments on commit a131982

Please sign in to comment.