okuda-seminar / log_service

Apache License 2.0
0 stars 0 forks source link

[Server] Implement SQLC and Mockgen Execution in Docker Container #28

Closed Rwatana closed 1 month ago

Rwatana commented 1 month ago

Issue Number

19

Implementation Summary

This pull request implements the execution of sqlc and mockgen commands within a Docker container. This change ensures that code generation and mocking are done in a consistent environment, reducing the risk of version mismatches across different development setups.

Scope of Impact

Particular Points to Check

Test

1 make docker-generate-mock

log_service git:(feature/#19_Setup_Docker_for_sqlc_and_gomock) ✗ make docker-generate-mock
docker build -f generate.Dockerfile -t my-go-generate-app .
[+] Building 2.5s (14/14) FINISHED                                                                                                                                                                   
 => [internal] load build definition from generate.Dockerfile                                                                                                                                   0.2s
 => => transferring dockerfile: 46B                                                                                                                                                             0.0s
 => [internal] load .dockerignore                                                                                                                                                               0.0s
 => => transferring context: 72B                                                                                                                                                                0.0s
 => [internal] load metadata for docker.io/library/golang:1.23                                                                                                                                  1.3s
 => [1/9] FROM docker.io/library/golang:1.23@sha256:a7f2fc9834049c1f5df787690026a53738e55fc097cd8a4a93faa3e06c67ee32                                                                            0.0s
 => [internal] load build context                                                                                                                                                               0.0s
 => => transferring context: 10.40kB                                                                                                                                                            0.0s
 => CACHED [2/9] WORKDIR /app                                                                                                                                                                   0.0s
 => CACHED [3/9] COPY go.mod ./                                                                                                                                                                 0.0s
 => CACHED [4/9] COPY go.sum ./                                                                                                                                                                 0.0s
 => CACHED [5/9] RUN go mod download                                                                                                                                                            0.0s
 => CACHED [6/9] RUN go install github.com/golang/mock/mockgen@latest                                                                                                                           0.0s
 => CACHED [7/9] RUN go get github.com/sqlc-dev/sqlc/cmd/sqlc@latest                                                                                                                            0.0s
 => CACHED [8/9] RUN go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest                                                                                                                        0.0s
 => [9/9] COPY . .                                                                                                                                                                              0.7s
 => exporting to image                                                                                                                                                                          0.1s
 => => exporting layers                                                                                                                                                                         0.0s
 => => writing image sha256:28c4da90564f08554c7487adf705c92fd72b0aa1731177367c26d742e1e2627a                                                                                                    0.0s
 => => naming to docker.io/library/my-go-generate-app                                                                                                                                           0.0s
docker run --rm -v /Users/ryoma/Desktop/okudaseminar/Project/log_service:/app my-go-generate-app sh -c "make generate && make mock-gen"
sqlc generate
mockgen -package domain -source=internal/server/domain/log_repository.go -destination=internal/server/domain/log_mock.go
mockgen -package usecase -source=internal/server/usecase/insert_log.go -destination=internal/server/usecase/insert_log_mock.go
➜  log_service git:(feature/#19_Setup_Docker_for_sqlc_and_gomock) ✗ 

Schedule

10/13

Rwatana commented 1 month ago

@nayuta-ai Please review my code.

Rwatana commented 1 month ago

@nayuta-ai I think I have modified the points. Please check my code.

Rwatana commented 1 month ago

@nayuta-ai I thought I had made the changes to the makefile, but I am not sure if it was completed completely, but I thought I had made the changes. If it is not sufficient, I would like to ask again. Please check my code.

Rwatana commented 1 month ago

@nayuta-ai I have left the .env line in the .dockerignore file and added the MySQL environment information to the Makefile to sustain the moveable code. Please check my code.

nayuta-ai commented 1 month ago

added the MySQL environment information to the Makefile to sustain the moveable code.

Why do you need to do this? Please include examples of cases where this has been implemented unsuccessfully.

Rwatana commented 1 month ago

Topic

The Makefile includes the following targets:

migrate_up:
    migrate -path internal/server/infrastructure/mysql/db/schema -database "mysql://root:$(MYSQL_ROOT_PASSWORD)@tcp(localhost:3306)/${MYSQL_DATABASE}" up

migrate_down:
    migrate -path internal/server/infrastructure/mysql/db/schema -database "mysql://root:$(MYSQL_ROOT_PASSWORD)@tcp(localhost:3306)/${MYSQL_DATABASE}" down

exec_db:
    docker compose exec db mysql -u root -p$(MYSQL_ROOT_PASSWORD) ${MYSQL_DATABASE}

Therefore, I think it’s necessary to include the environment variables used in these commands. Since I removed the loading of the .env file, I believe these commands will no longer work.

Situation

If I don't do that change, I think errors will occur when executing the above commands in the local environment.

➜  log_service git:(feature/#19_Setup_Docker_for_sqlc_and_gomock) make migrate_up
migrate -path internal/server/infrastructure/mysql/db/schema -database "mysql://root:@tcp(localhost:3306)/" up
error: failed to open database, "mysql://root:@tcp(localhost:3306)/": Error 1045: Access denied for user 'root'@'192.168.32.1' (using password: NO)
make: *** [migrate_up] Error 1
➜  log_service git:(feature/#19_Setup_Docker_for_sqlc_and_gomock) ✗ 
nayuta-ai commented 1 month ago

Did you load the .env file?

It can execute as below.

$ make migrate_up
migrate -path internal/server/infrastructure/mysql/db/schema -database "mysql://root:password@tcp(localhost:3306)/example" up
no change
Rwatana commented 1 month ago

@nayuta-ai I misunderstood the mechanism of operation. I apologize for the inconvenience. I have made corrections, so please check my code.

Rwatana commented 1 month ago

@nayuta-ai I have incorporated the sentence as per your suggestion and confirmed that it works properly. Please review my code.