Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#19 Setup postgres databases and migrations #32

Merged
merged 12 commits into from
Oct 3, 2024
20 changes: 20 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>ru.ifmo.se.dating</groupId>
<artifactId>itmo-dating</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>common</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>common</name>

<modules>
<module>testing</module>
</modules>
</project>
32 changes: 32 additions & 0 deletions common/testing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>ru.ifmo.se.dating</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>common-testing</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>common-testing</name>
Kimiega marked this conversation as resolved.
Show resolved Hide resolved

<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
Kimiega marked this conversation as resolved.
Show resolved Hide resolved

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ru.ifmo.se.dating.container;

import org.testcontainers.containers.PostgreSQLContainer;

import lombok.ToString;

@ToString
public final class Postgres implements AutoCloseable {
private static final String DOCKER_IMAGE_NAME = "postgres";

private final PostgreSQLContainer<?> container;

private Postgres() {
this.container = new PostgreSQLContainer<>(DOCKER_IMAGE_NAME);
}

public String jdbcUrl() {
return container.getJdbcUrl();
}

public String username() {
return container.getUsername();
}

public String password() {
return container.getPassword();
}

@Override
public void close() throws Exception {
container.stop();
container.close();
}

public static Postgres start() {
Kimiega marked this conversation as resolved.
Show resolved Hide resolved
final var postgres = new Postgres();
postgres.container.start();
return postgres;
}
}
36 changes: 36 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,48 @@ services:
volumes:
- ./matchmaker/app/target/matchmaker-app-1.0.0.jar:/matchmaker.jar
command: java -jar /matchmaker.jar
environment:
POSTGRES_DB: ${ITMO_DATING_MATCHMAKER_POSTGRES_DB?:err}
POSTGRES_USER: ${ITMO_DATING_MATCHMAKER_POSTGRES_USER?:err}
POSTGRES_PASSWORD: ${ITMO_DATING_MATCHMAKER_POSTGRES_PASSWORD?:err}
ports:
- 8080:8080
depends_on:
matchmaker-database:
condition: service_healthy
matchmaker-database:
image: postgres
environment:
POSTGRES_DB: ${ITMO_DATING_MATCHMAKER_POSTGRES_DB?:err}
POSTGRES_USER: ${ITMO_DATING_MATCHMAKER_POSTGRES_USER?:err}
POSTGRES_PASSWORD: ${ITMO_DATING_MATCHMAKER_POSTGRES_PASSWORD?:err}
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 1s
timeout: 1s
retries: 16
people:
image: eclipse-temurin:21-jdk-alpine
volumes:
- ./people/app/target/people-app-1.0.0.jar:/people.jar
command: java -jar /people.jar
environment:
POSTGRES_DB: ${ITMO_DATING_PEOPLE_POSTGRES_DB?:err}
POSTGRES_USER: ${ITMO_DATING_PEOPLE_POSTGRES_USER?:err}
POSTGRES_PASSWORD: ${ITMO_DATING_PEOPLE_POSTGRES_PASSWORD?:err}
ports:
- 8081:8080
depends_on:
people-database:
condition: service_healthy
people-database:
image: postgres
environment:
POSTGRES_DB: ${ITMO_DATING_PEOPLE_POSTGRES_DB?:err}
POSTGRES_USER: ${ITMO_DATING_PEOPLE_POSTGRES_USER?:err}
POSTGRES_PASSWORD: ${ITMO_DATING_PEOPLE_POSTGRES_PASSWORD?:err}
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 1s
timeout: 1s
retries: 16
7 changes: 7 additions & 0 deletions config/env/local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export ITMO_DATING_MATCHMAKER_POSTGRES_DB="postgres"
export ITMO_DATING_MATCHMAKER_POSTGRES_USER="postgres"
export ITMO_DATING_MATCHMAKER_POSTGRES_PASSWORD="postgres"

export ITMO_DATING_PEOPLE_POSTGRES_DB="postgres"
export ITMO_DATING_PEOPLE_POSTGRES_USER="postgres"
export ITMO_DATING_PEOPLE_POSTGRES_PASSWORD="postgres"
15 changes: 13 additions & 2 deletions matchmaker/app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
<groupId>ru.ifmo.se.dating</groupId>
<artifactId>matchmaker-server</artifactId>
</dependency>
<dependency>
<groupId>ru.ifmo.se.dating</groupId>
<artifactId>matchmaker-database</artifactId>
</dependency>
<dependency>
<groupId>ru.ifmo.se.dating</groupId>
<artifactId>common-testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -30,10 +40,11 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

Expand Down
9 changes: 3 additions & 6 deletions matchmaker/app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
springdoc:
api-docs:
path: /openapi
swagger-ui:
url: /openapi/api.yml
path: /swagger-ui.html
spring:
config:
import: application-database.yml, application-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.ifmo.se.dating.matchmaker;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

import lombok.ToString;
import ru.ifmo.se.dating.container.Postgres;

@ToString
public final class DatabaseInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {

private final Postgres postgres;

DatabaseInitializer() {
this.postgres = Postgres.start();
}

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"spring.datasource.url=%s".formatted(postgres.jdbcUrl()),
"spring.datasource.username=%s".formatted(postgres.username()),
"spring.datasource.password=%s".formatted(postgres.password()))
.applyTo(applicationContext.getEnvironment());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package ru.ifmo.se.dating.matchmaker;

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@ActiveProfiles(profiles = {"test"})
@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = { "test" })
@SpringBootTest(
classes = {OpenApiGeneratorApplication.class},
classes = {OpenApiGeneratorApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
useMainMethod = SpringBootTest.UseMainMethod.ALWAYS
)
abstract class MatchmakerTestSuite {
@ContextConfiguration(
initializers = {
DatabaseInitializer.class,
}
)
public abstract class MatchmakerTestSuite {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.ifmo.se.dating.matchmaker.api;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import ru.ifmo.se.dating.matchmaker.MatchmakerTestSuite;

public final class HttpMonitoringApiTest extends MatchmakerTestSuite {
@Autowired
private TestRestTemplate rest;

@Test
public void healthcheck() {
assertEquals(getHealthcheck(), "public");
}

private String getHealthcheck() {
final var url = "http://localhost:8080/api/v1/monitoring/healthcheck";
final var response = rest.getForEntity(url, String.class);
return response.getBody();
}
}
31 changes: 31 additions & 0 deletions matchmaker/database/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>ru.ifmo.se.dating</groupId>
<artifactId>matchmaker</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>matchmaker-database</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>matchmaker-database</name>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
datasource:
url: jdbc:postgresql://matchmaker-database:5432/${POSTGRES_DB}
username: ${POSTGRES_USER}
password: ${POSTGRES_PASSWORD}
driver-class-name: org.postgresql.Driver
liquibase:
change-log: classpath:database/changelog.sql
4 changes: 4 additions & 0 deletions matchmaker/database/src/main/resources/database/changelog.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--liquibase formatted sql
--
--changeset vityaman:initialize
CREATE SCHEMA matchmaker;
1 change: 1 addition & 0 deletions matchmaker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<modules>
<module>api</module>
<module>app</module>
<module>database</module>
<module>server</module>
</modules>
</project>
6 changes: 6 additions & 0 deletions matchmaker/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
package ru.ifmo.se.dating.matchmaker.api;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import lombok.ToString;
import ru.ifmo.se.dating.matchmaker.api.generated.MonitoringApiDelegate;

@ToString
@Service
class HttpMonitoringApi implements MonitoringApiDelegate {
private final DataSource data;

HttpMonitoringApi(DataSource data) {
this.data = data;
}

@Override
public ResponseEntity<String> monitoringHealthcheckGet() {
return ResponseEntityStub.create();
try (
var connection = data.getConnection();
var statement = connection.createStatement();
var result = statement.executeQuery("SELECT current_schema");
) {
if (!result.next()) {
throw new IllegalStateException();
}

var pong = result.getString(1);
return ResponseEntity.ok(pong);
} catch (SQLException | IllegalStateException exception) {
return ResponseEntity.internalServerError().build();
}
}
}
8 changes: 8 additions & 0 deletions matchmaker/server/src/main/resources/application-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server:
port: 8080
springdoc:
api-docs:
path: /openapi
swagger-ui:
url: /openapi/api.yml
path: /swagger-ui.html
Loading
Loading