Skip to content

Commit 40db85a

Browse files
authored
Merge pull request #17 from Netsbump/refactor/uuid
refactor: migrate entity IDs from number to UUID
2 parents 97cc11c + d2374f6 commit 40db85a

File tree

14 files changed

+135
-61
lines changed

14 files changed

+135
-61
lines changed

apps/api/src/entities/exercise.entity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Video } from './video.entity';
44

55
@Entity()
66
export class Exercise {
7-
@PrimaryKey()
8-
id!: number;
7+
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
8+
id!: string;
99

1010
@ManyToOne(() => ExerciseType)
1111
exerciseType!: ExerciseType;

apps/api/src/entities/exerciseType.entity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { Exercise } from './exercise.entity';
99

1010
@Entity()
1111
export class ExerciseType {
12-
@PrimaryKey()
13-
id!: number;
12+
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
13+
id!: string;
1414

1515
@OneToMany(
1616
() => Exercise,

apps/api/src/entities/test.entity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
22

33
@Entity()
44
export class Test {
5-
@PrimaryKey()
6-
id!: number;
5+
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
6+
id!: string;
77

88
@Property()
99
createdAt = new Date();

apps/api/src/entities/video.entity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ interface VideoMetadata {
88

99
@Entity()
1010
export class Video {
11-
@PrimaryKey()
12-
id!: number;
11+
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
12+
id!: string;
1313

1414
@Property()
1515
src!: string;

apps/api/src/migrations/.snapshot-dropit.json

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"columns": {
99
"id": {
1010
"name": "id",
11-
"type": "serial",
11+
"type": "uuid",
1212
"unsigned": false,
13-
"autoincrement": true,
14-
"primary": true,
13+
"autoincrement": false,
14+
"primary": false,
1515
"nullable": false,
16-
"mappedType": "integer"
16+
"default": "gen_random_uuid()",
17+
"mappedType": "uuid"
1718
},
1819
"name": {
1920
"name": "name",
@@ -68,12 +69,13 @@
6869
"columns": {
6970
"id": {
7071
"name": "id",
71-
"type": "serial",
72+
"type": "uuid",
7273
"unsigned": false,
73-
"autoincrement": true,
74-
"primary": true,
74+
"autoincrement": false,
75+
"primary": false,
7576
"nullable": false,
76-
"mappedType": "integer"
77+
"default": "gen_random_uuid()",
78+
"mappedType": "uuid"
7779
},
7880
"created_at": {
7981
"name": "created_at",
@@ -128,12 +130,13 @@
128130
"columns": {
129131
"id": {
130132
"name": "id",
131-
"type": "serial",
133+
"type": "uuid",
132134
"unsigned": false,
133-
"autoincrement": true,
134-
"primary": true,
135+
"autoincrement": false,
136+
"primary": false,
135137
"nullable": false,
136-
"mappedType": "integer"
138+
"default": "gen_random_uuid()",
139+
"mappedType": "uuid"
137140
},
138141
"src": {
139142
"name": "src",
@@ -198,30 +201,31 @@
198201
"columns": {
199202
"id": {
200203
"name": "id",
201-
"type": "serial",
204+
"type": "uuid",
202205
"unsigned": false,
203-
"autoincrement": true,
204-
"primary": true,
206+
"autoincrement": false,
207+
"primary": false,
205208
"nullable": false,
206-
"mappedType": "integer"
209+
"default": "gen_random_uuid()",
210+
"mappedType": "uuid"
207211
},
208212
"exercise_type_id": {
209213
"name": "exercise_type_id",
210-
"type": "int",
214+
"type": "uuid",
211215
"unsigned": false,
212216
"autoincrement": false,
213217
"primary": false,
214218
"nullable": false,
215-
"mappedType": "integer"
219+
"mappedType": "uuid"
216220
},
217221
"video_id": {
218222
"name": "video_id",
219-
"type": "int",
223+
"type": "uuid",
220224
"unsigned": false,
221225
"autoincrement": false,
222226
"primary": false,
223227
"nullable": true,
224-
"mappedType": "integer"
228+
"mappedType": "uuid"
225229
},
226230
"name": {
227231
"name": "name",
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Migration } from '@mikro-orm/migrations';
2+
3+
export class Migration20250103192244_idNumberToUuid extends Migration {
4+
5+
override async up(): Promise<void> {
6+
this.addSql(`alter table "exercise" drop constraint "exercise_exercise_type_id_foreign";`);
7+
this.addSql(`alter table "exercise" drop constraint "exercise_video_id_foreign";`);
8+
9+
this.addSql(`alter table "exercise_type" alter column "id" drop default;`);
10+
this.addSql(`alter table "exercise_type" alter column "id" type uuid using ("id"::text::uuid);`);
11+
this.addSql(`alter table "exercise_type" alter column "id" set default gen_random_uuid();`);
12+
13+
this.addSql(`alter table "test" alter column "id" drop default;`);
14+
this.addSql(`alter table "test" alter column "id" type uuid using ("id"::text::uuid);`);
15+
this.addSql(`alter table "test" alter column "id" set default gen_random_uuid();`);
16+
17+
this.addSql(`alter table "video" alter column "id" drop default;`);
18+
this.addSql(`alter table "video" alter column "id" type uuid using ("id"::text::uuid);`);
19+
this.addSql(`alter table "video" alter column "id" set default gen_random_uuid();`);
20+
21+
this.addSql(`alter table "exercise" alter column "id" drop default;`);
22+
this.addSql(`alter table "exercise" alter column "id" type uuid using ("id"::text::uuid);`);
23+
this.addSql(`alter table "exercise" alter column "id" set default gen_random_uuid();`);
24+
this.addSql(`alter table "exercise" alter column "exercise_type_id" drop default;`);
25+
this.addSql(`alter table "exercise" alter column "exercise_type_id" type uuid using ("exercise_type_id"::text::uuid);`);
26+
this.addSql(`alter table "exercise" alter column "video_id" drop default;`);
27+
this.addSql(`alter table "exercise" alter column "video_id" type uuid using ("video_id"::text::uuid);`);
28+
this.addSql(`alter table "exercise" add constraint "exercise_exercise_type_id_foreign" foreign key ("exercise_type_id") references "exercise_type" ("id") on update cascade;`);
29+
this.addSql(`alter table "exercise" add constraint "exercise_video_id_foreign" foreign key ("video_id") references "video" ("id") on update cascade on delete set null;`);
30+
}
31+
32+
override async down(): Promise<void> {
33+
this.addSql(`alter table "exercise_type" alter column "id" type text using ("id"::text);`);
34+
35+
this.addSql(`alter table "test" alter column "id" type text using ("id"::text);`);
36+
37+
this.addSql(`alter table "video" alter column "id" type text using ("id"::text);`);
38+
39+
this.addSql(`alter table "exercise" alter column "id" type text using ("id"::text);`);
40+
this.addSql(`alter table "exercise" alter column "exercise_type_id" type text using ("exercise_type_id"::text);`);
41+
this.addSql(`alter table "exercise" alter column "video_id" type text using ("video_id"::text);`);
42+
43+
this.addSql(`alter table "exercise" drop constraint "exercise_exercise_type_id_foreign";`);
44+
this.addSql(`alter table "exercise" drop constraint "exercise_video_id_foreign";`);
45+
46+
this.addSql(`alter table "exercise_type" alter column "id" drop default;`);
47+
this.addSql(`alter table "exercise_type" alter column "id" type int using ("id"::int);`);
48+
this.addSql(`create sequence if not exists "exercise_type_id_seq";`);
49+
this.addSql(`select setval('exercise_type_id_seq', (select max("id") from "exercise_type"));`);
50+
this.addSql(`alter table "exercise_type" alter column "id" set default nextval('exercise_type_id_seq');`);
51+
52+
this.addSql(`alter table "test" alter column "id" drop default;`);
53+
this.addSql(`alter table "test" alter column "id" type int using ("id"::int);`);
54+
this.addSql(`create sequence if not exists "test_id_seq";`);
55+
this.addSql(`select setval('test_id_seq', (select max("id") from "test"));`);
56+
this.addSql(`alter table "test" alter column "id" set default nextval('test_id_seq');`);
57+
58+
this.addSql(`alter table "video" alter column "id" drop default;`);
59+
this.addSql(`alter table "video" alter column "id" type int using ("id"::int);`);
60+
this.addSql(`create sequence if not exists "video_id_seq";`);
61+
this.addSql(`select setval('video_id_seq', (select max("id") from "video"));`);
62+
this.addSql(`alter table "video" alter column "id" set default nextval('video_id_seq');`);
63+
64+
this.addSql(`alter table "exercise" alter column "id" drop default;`);
65+
this.addSql(`alter table "exercise" alter column "id" type int using ("id"::int);`);
66+
this.addSql(`alter table "exercise" alter column "exercise_type_id" type int using ("exercise_type_id"::int);`);
67+
this.addSql(`alter table "exercise" alter column "video_id" type int using ("video_id"::int);`);
68+
this.addSql(`create sequence if not exists "exercise_id_seq";`);
69+
this.addSql(`select setval('exercise_id_seq', (select max("id") from "exercise"));`);
70+
this.addSql(`alter table "exercise" alter column "id" set default nextval('exercise_id_seq');`);
71+
this.addSql(`alter table "exercise" add constraint "exercise_exercise_type_id_foreign" foreign key ("exercise_type_id") references "exercise_type" ("id") on update cascade;`);
72+
this.addSql(`alter table "exercise" add constraint "exercise_video_id_foreign" foreign key ("video_id") references "video" ("id") on update cascade on delete set null;`);
73+
}
74+
75+
}

apps/api/src/modules/exercise/exercise.controller.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ export class ExerciseController implements NestControllerInterface<typeof c> {
5050
// Dans le contrat, pathParams = { id: z.string() }
5151
// => on cast en number (ou on utilise z.coerce.number() dans le contrat)
5252
try {
53-
const id = parseInt(params.id, 10);
54-
55-
const exercise = await this.exerciseService.getExercise(id);
53+
const exercise = await this.exerciseService.getExercise(params.id);
5654

5755
return {
5856
status: 200 as const,
@@ -102,12 +100,9 @@ export class ExerciseController implements NestControllerInterface<typeof c> {
102100
async updateExercise(
103101
@TsRestRequest() { params, body }: RequestShapes['updateExercise']
104102
) {
105-
// Todo: revoir si je passe en number ou string dans le contrat
106103
try {
107-
const id = parseInt(params.id, 10);
108-
109104
const updatedExercise = await this.exerciseService.updateExercise(
110-
id,
105+
params.id,
111106
body
112107
);
113108

@@ -132,9 +127,7 @@ export class ExerciseController implements NestControllerInterface<typeof c> {
132127
@TsRestRequest() { params }: RequestShapes['deleteExercise']
133128
) {
134129
try {
135-
const id = parseInt(params.id, 10);
136-
137-
await this.exerciseService.deleteExercise(id);
130+
await this.exerciseService.deleteExercise(params.id);
138131

139132
return {
140133
status: 200 as const,

apps/api/src/modules/exercise/exercise.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class ExerciseService {
4141
});
4242
}
4343

44-
async getExercise(id: number): Promise<ExerciseResponse> {
44+
async getExercise(id: string): Promise<ExerciseResponse> {
4545
const exercise = await this.em.findOne(
4646
Exercise,
4747
{ id },
@@ -121,7 +121,7 @@ export class ExerciseService {
121121
}
122122

123123
async updateExercise(
124-
id: number,
124+
id: string,
125125
exercise: UpdateExercise
126126
): Promise<ExerciseResponse> {
127127
const exerciseToUpdate = await this.em.findOne(
@@ -168,7 +168,7 @@ export class ExerciseService {
168168
};
169169
}
170170

171-
async deleteExercise(id: number): Promise<{ message: string }> {
171+
async deleteExercise(id: string): Promise<{ message: string }> {
172172
const exerciseToDelete = await this.em.findOne(Exercise, { id });
173173

174174
if (!exerciseToDelete) {

apps/api/src/modules/exerciseType/exerciseType.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class ExerciseTypeController {
2020
}
2121

2222
@Get(':id')
23-
async getExerciseType(@Param('id') id: number) {
23+
async getExerciseType(@Param('id') id: string) {
2424
return this.exerciseTypeService.getExerciseType(id);
2525
}
2626

@@ -31,14 +31,14 @@ export class ExerciseTypeController {
3131

3232
@Put(':id')
3333
async updateExerciseType(
34-
@Param('id') id: number,
34+
@Param('id') id: string,
3535
@Body() exerciseType: ExerciseType
3636
) {
3737
return this.exerciseTypeService.updateExerciseType(id, exerciseType);
3838
}
3939

4040
@Delete(':id')
41-
async deleteExerciseType(@Param('id') id: number) {
41+
async deleteExerciseType(@Param('id') id: string) {
4242
return this.exerciseTypeService.deleteExerciseType(id);
4343
}
4444
}

apps/api/src/modules/exerciseType/exerciseType.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class ExerciseTypeService {
1111
return this.em.find(ExerciseType, {});
1212
}
1313

14-
async getExerciseType(id: number) {
14+
async getExerciseType(id: string) {
1515
return this.em.findOne(ExerciseType, { id });
1616
}
1717

@@ -22,7 +22,7 @@ export class ExerciseTypeService {
2222
return exerciseTypeToCreate;
2323
}
2424

25-
async updateExerciseType(id: number, exerciseType: ExerciseTypeDto) {
25+
async updateExerciseType(id: string, exerciseType: ExerciseTypeDto) {
2626
const exerciseTypeToUpdate = await this.em.findOne(ExerciseType, { id });
2727

2828
if (!exerciseTypeToUpdate) {
@@ -35,7 +35,7 @@ export class ExerciseTypeService {
3535
return exerciseTypeToUpdate;
3636
}
3737

38-
async deleteExerciseType(id: number) {
38+
async deleteExerciseType(id: string) {
3939
const exerciseTypeToDelete = await this.em.findOne(ExerciseType, { id });
4040

4141
if (!exerciseTypeToDelete) {

0 commit comments

Comments
 (0)