From 058f42a6dcb23100b15f6e5b4f1e15dcc575cdbe Mon Sep 17 00:00:00 2001 From: XtophB Date: Sat, 23 May 2026 11:54:18 +0200 Subject: [PATCH 1/4] feat: add DB service to docker compose file Backend service will now only start if postgres service has successfully started --- docker-compose.yml | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 44d70a2e..c69d88c2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,33 @@ services: + postgres: + image: postgres:16 + restart: unless-stopped + environment: + POSTGRES_DB: codenames + POSTGRES_USER: codenames_user + POSTGRES_PASSWORD: codenames_pass + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U codenames_user -d codenames"] + interval: 10s + timeout: 5s + retries: 5 + backend: image: ghcr.io/ss26-se2-codenames/backend:latest restart: unless-stopped ports: - "53213:8080" volumes: - - ./data:/app/data \ No newline at end of file + - ./data:/app/data + environment: + SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/codenames + SPRING_DATASOURCE_USERNAME: codenames_user + SPRING_DATASOURCE_PASSWORD: codenames_pass + depends_on: + postgres: + condition: service_healthy + +volumes: + postgres_data: \ No newline at end of file From 3c8227b47e08c557495bdf59c34ff5cd4a9dadcc Mon Sep 17 00:00:00 2001 From: XtophB Date: Sat, 23 May 2026 12:06:06 +0200 Subject: [PATCH 2/4] chore: add jpa, postgres and flyway dependencies to pom.xml file --- pom.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pom.xml b/pom.xml index a5533765..d32871d0 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,19 @@ jspecify 1.0.0 + + org.postgresql + postgresql + runtime + + + org.flywaydb + flyway-core + + + org.springframework.boot + spring-boot-starter-data-jpa + From 238102b68329999050785d49f17cdfd7d92a4727 Mon Sep 17 00:00:00 2001 From: XtophB Date: Sat, 23 May 2026 12:49:12 +0200 Subject: [PATCH 3/4] chore: update application yaml for db implementation --- docker-compose.yml | 2 +- src/main/resources/application.yaml | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index c69d88c2..c3c49d10 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,7 +21,7 @@ services: - "53213:8080" volumes: - ./data:/app/data - environment: + environment: # Inject datasource url into application.yml when containerized SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/codenames SPRING_DATASOURCE_USERNAME: codenames_user SPRING_DATASOURCE_PASSWORD: codenames_pass diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 67237b60..3a63f2d2 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -2,5 +2,23 @@ spring: application: name: Codenames_Backend + datasource: + url: jdbc:postgresql://localhost:5432/codenames # When we containerize docker compose will change localhost -> postgresql + username: codenames_user + password: codenames_pass + driver-class-name: org.postgresql.Driver + + jpa: + hibernate: + ddl-auto: validate + show-sql: true + properties: + hibernate: + format_sql: true + dialect: org.hibernate.dialect.PostgreSQLDialect + + flyway: + enabled: true + app: allowed-origins: "http://localhost:8080,http://10.0.2.2:8080" \ No newline at end of file From 6ed9b846acfaa35b18ea27bb4740b5327c8bd2d5 Mon Sep 17 00:00:00 2001 From: XtophB Date: Sat, 23 May 2026 22:02:08 +0200 Subject: [PATCH 4/4] test: add h2 to mock DB for CI Allows @SpringBootTest to run without problem in the future while actual DB is not present. --- pom.xml | 5 +++++ src/test/resources/application.yaml | 11 +++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/test/resources/application.yaml diff --git a/pom.xml b/pom.xml index d32871d0..d20b2dcc 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,11 @@ org.springframework.boot spring-boot-starter-data-jpa + + com.h2database + h2 + runtime + diff --git a/src/test/resources/application.yaml b/src/test/resources/application.yaml new file mode 100644 index 00000000..310dc3a0 --- /dev/null +++ b/src/test/resources/application.yaml @@ -0,0 +1,11 @@ +spring: + datasource: + url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=PostgreSQL # create mock h2 DB in RAM that doesn't close until tests finish + driver-class-name: org.h2.Driver + username: sa + password: + jpa: + hibernate: + ddl-auto: create-drop # flyway disabled, hibernate needs to take over and drop tables after test + flyway: + enabled: false # H2 might not work with flyway \ No newline at end of file