Skip to content

Commit

Permalink
"Change id to uuid #14"
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Polyakov committed Jun 18, 2024
1 parent bbcb04b commit 5b08643
Show file tree
Hide file tree
Showing 24 changed files with 467 additions and 275 deletions.
96 changes: 58 additions & 38 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,21 @@ paths:
description: Bad request
'500':
description: Failed response
'/api/users/{id}':
'/api/users/{uuid}':
get:
tags:
- Users
summary: Get a user by ID
operationId: getUserById
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: Detailed information of a user
Expand All @@ -163,13 +164,14 @@ paths:
summary: Update a user
operationId: updateUser
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
requestBody:
content:
application/json:
Expand All @@ -189,13 +191,14 @@ paths:
summary: Delete a user
operationId: deleteUser
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: User deleted successfully
Expand Down Expand Up @@ -242,20 +245,21 @@ paths:
description: Bad request
'500':
description: Failed response
'/api/games/{id}':
'/api/games/{uuid}':
get:
tags:
- Games
summary: Get a game by ID
operationId: getGameById
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: Detailed information of a game
Expand All @@ -273,13 +277,14 @@ paths:
summary: Update a game
operationId: updateGame
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
requestBody:
content:
application/json:
Expand All @@ -305,13 +310,14 @@ paths:
summary: Delete a game
operationId: deleteGame
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: Game deleted successfully
Expand Down Expand Up @@ -371,20 +377,21 @@ paths:
content:
application/json:
schema: {}
'/api/teams/{id}':
'/api/teams/{uuid}':
get:
tags:
- Teams
summary: Get a team by ID
operationId: getTeamById
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: Detailed information of a team
Expand All @@ -398,13 +405,14 @@ paths:
summary: Update a team
operationId: updateTeam
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
requestBody:
content:
application/json:
Expand All @@ -420,13 +428,14 @@ paths:
summary: Delete a team
operationId: deleteTeam
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: Team deleted successfully
Expand Down Expand Up @@ -462,20 +471,21 @@ paths:
responses:
'200':
description: Result created successfully
'/api/results/{id}':
'/api/results/{uuid}':
get:
tags:
- Results
summary: Get a result by ID
operationId: getResultById
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: Detailed information of a result
Expand Down Expand Up @@ -515,20 +525,21 @@ paths:
responses:
'200':
description: Service created successfully
'/api/services/{id}':
'/api/services/{uuid}':
get:
tags:
- Services
summary: Get a service by ID
operationId: getServiceById
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: Detailed information of a service
Expand All @@ -542,13 +553,14 @@ paths:
summary: Update a service
operationId: updateService
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
requestBody:
content:
application/json:
Expand All @@ -564,13 +576,14 @@ paths:
summary: Delete a service
operationId: deleteService
parameters:
- name: id
- name: uuid
in: path
required: true
style: simple
explode: false
schema:
type: integer
type: string
format: uuid
responses:
'200':
description: Service deleted successfully
Expand Down Expand Up @@ -624,14 +637,16 @@ components:
team_ids:
type: array
items:
type: integer
type: string
format: uuid
example: 1
description: Unique identifier for the result entry
UserResponse:
type: object
properties:
id:
type: integer
type: string
format: uuid
description: The unique identifier for the user
user_name:
type: string
Expand Down Expand Up @@ -677,7 +692,8 @@ components:
type: object
properties:
id:
type: integer
type: string
format: uuid
description: Unique identifier for the game
start_time:
type: string
Expand Down Expand Up @@ -722,7 +738,8 @@ components:
type: object
properties:
id:
type: integer
type: string
format: uuid
description: Unique identifier for the result entry
team_id:
type: string
Expand Down Expand Up @@ -767,7 +784,8 @@ components:
type: object
properties:
id:
type: integer
type: string
format: uuid
description: Unique identifier for the service
name:
type: string
Expand Down Expand Up @@ -812,7 +830,8 @@ components:
type: object
properties:
id:
type: integer
type: string
format: uuid
description: Unique identifier for the team
name:
type: string
Expand All @@ -836,7 +855,8 @@ components:
- name
properties:
id:
type: integer
type: string
format: uuid
description: The unique identifier of the university
example: 1
name:
Expand Down
3 changes: 3 additions & 0 deletions internal/app/database/struct_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func RegisterAllUpdates() map[string][]DatabaseUpdateFunc {
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0011_update0011testdata)
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0011_update0012)
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0012_update0012testdata)
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0012_update0013)
allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0013_update0014)

return allUpdates
}

Expand Down
36 changes: 36 additions & 0 deletions internal/app/database/update0012_update0013.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package database

import (
"database/sql"
"log/slog"
"runtime"
)

func DatabaseUpdate_update0012_update0013(db *sql.DB, getInfo bool) (string, string, string, error) {

// WARNING!!!
// Do not change the update if it has already been installed by other developers or in production.
// To correct the database, create a new update and register it in the list of updates.

fromUpdateId, toUpdateId := ParseNameFuncUpdate(runtime.Caller(0))
description := "Change int id to uuid table results"
if getInfo {
return fromUpdateId, toUpdateId, description, nil
}
query := `
BEGIN;
ALTER TABLE results ADD COLUMN new_id UUID DEFAULT gen_random_uuid();
UPDATE results SET new_id = gen_random_uuid();
ALTER TABLE results DROP COLUMN id;
ALTER TABLE results ADD CONSTRAINT new_id_pkey PRIMARY KEY (new_id);
ALTER TABLE results RENAME COLUMN new_id TO id;
COMMIT;
`
_, err := db.Exec(query)
if err != nil {
slog.Error("Problem with update, query: " + query + "\n error:" + err.Error())
return fromUpdateId, toUpdateId, description, err
}

return fromUpdateId, toUpdateId, description, nil
}
Loading

0 comments on commit 5b08643

Please sign in to comment.