Skip to content

Commit

Permalink
Create flyway migration and database details (#57)
Browse files Browse the repository at this point in the history
* Added postgres, flyway, and testcontainers for testing environment

* Ditched spring data jpa, now using only jdbc

* Add TODOs for production database
  • Loading branch information
dvillavicencio authored Jan 29, 2024
1 parent 3d6f836 commit 83f9136
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 7 deletions.
18 changes: 14 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.3'
id 'org.asciidoctor.jvm.convert' version '3.3.2'
id 'org.flywaydb.flyway' version '10.0.0'
}

group = 'com.danielvm'
Expand Down Expand Up @@ -37,7 +38,15 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.session:spring-session-data-redis'
implementation 'org.postgresql:postgresql:42.5.1'
implementation 'io.r2dbc:r2dbc-postgresql:0.8.13.RELEASE'
implementation "org.flywaydb:flyway-core:10.6.0"
implementation 'org.flywaydb:flyway-database-postgresql:10.4.1'
implementation 'org.aspectj:aspectjrt:1.9.20'
implementation 'org.aspectj:aspectjweaver:1.8.9'
implementation 'org.projectlombok:lombok:1.18.30'
Expand All @@ -47,12 +56,14 @@ dependencies {
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'commons-io:commons-io:2.15.1'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-devtools'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'io.projectreactor:reactor-test:3.6.0'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
testImplementation 'org.flywaydb.flyway-test-extensions:flyway-spring-test:10.0.0'
testImplementation "org.testcontainers:postgresql:1.19.4"
testImplementation 'org.testcontainers:jdbc:1.19.4'
testImplementation 'org.testcontainers:r2dbc:1.19.4'
testImplementation "org.junit.jupiter:junit-jupiter-params:5.10.1"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
Expand Down Expand Up @@ -85,5 +96,4 @@ tasks.register('version') {

tasks.register('projectName') {
println project.name
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.danielvm.destiny2bot.config;

import org.flywaydb.core.Flyway;
import org.springframework.boot.autoconfigure.flyway.FlywayProperties;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({R2dbcProperties.class, FlywayProperties.class})
public class FlywayConfiguration {

@Bean(initMethod = "migrate")
public Flyway flyway(FlywayProperties flywayProperties, R2dbcProperties r2dbcProperties) {
return Flyway.configure()
.dataSource(
flywayProperties.getUrl(),
r2dbcProperties.getUsername(),
r2dbcProperties.getPassword())
.locations(flywayProperties.getLocations().get(0))
.baselineOnMigrate(true)
.load();
}
}
15 changes: 14 additions & 1 deletion src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ spring:
redis:
port: 6379
host: localhost
r2dbc:
url: r2dbc:postgresql://localhost:5432/postgres
username: postgres
password: mysecretpassword
h2:
console:
enabled: true
flyway:
enabled: true
url: jdbc:postgresql://localhost:5432/postgres
user: postgres
password: mysecretpassword
locations: classpath:db/migration

bungie:
api:
Expand All @@ -20,4 +33,4 @@ server:
application:
callback:
# Replace with your own callback url masking port 8080 to run locally
url: https://8cc5-2600-1700-4390-a930-bd4e-274b-69ec-53d4.ngrok-free.app
url: https://2d13-2600-1700-4390-a930-88d3-1a5f-5ca0-212e.ngrok-free.app
6 changes: 6 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ spring:
redis:
port: 6379
host: redis
flyway:
enabled: true
datasource:
url: # TODO: Define production parameters
username: # TODO: Define production parameters
password: #TODO: Define production parameters

server:
port: 443
Expand Down
44 changes: 44 additions & 0 deletions src/main/resources/db/migration/V1__Create_initial_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE TABLE IF NOT EXISTS bungie_user
(
discord_id BIGINT PRIMARY KEY,
discord_username VARCHAR(50) NOT NULL,
bungie_membership_id BIGINT UNIQUE NOT NULL,
bungie_access_token VARCHAR(250) UNIQUE NOT NULL,
bungie_refresh_token VARCHAR(250) UNIQUE NOT NULL,
bungie_token_expiration BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS bungie_user_character
(
character_id BIGINT PRIMARY KEY,
light_level INTEGER NOT NULL,
destiny_class VARCHAR(10) NOT NULL,
bungie_user_id BIGINT,
FOREIGN KEY (bungie_user_id) REFERENCES bungie_user (bungie_membership_id)
);

CREATE TABLE IF NOT EXISTS character_raid
(
instance_id BIGINT PRIMARY KEY,
raid_start_timestamp TIMESTAMP NOT NULL,
is_from_beginning BOOLEAN NOT NULL,
completed BOOLEAN NOT NULL,
raid_name VARCHAR(100) NOT NULL,
number_of_deaths INTEGER NOT NULL,
oponents_defeated INTEGER NOT NULL,
kill_death_assists DECIMAL NOT NULL,
raid_duration INTEGER NOT NULL,
user_character_id BIGINT,
FOREIGN KEY (user_character_id) REFERENCES bungie_user_character (character_id)
);

CREATE TABLE IF NOT EXISTS raid_participant
(
membership_id BIGINT PRIMARY KEY,
username VARCHAR(100) NOT NULL,
character_class VARCHAR(10) NOT NULL,
icon_path VARCHAR(100) NOT NULL,
completed BOOLEAN NOT NULL,
raid_instance BIGINT,
FOREIGN KEY (raid_instance) REFERENCES character_raid (instance_id)
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.danielvm.destiny2bot.dto.discord.Interaction;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.r2dbc.spi.ConnectionFactoryOptions;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.PublicKey;
Expand All @@ -26,6 +27,8 @@
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
import org.springframework.web.reactive.function.BodyInserters;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import software.pando.crypto.nacl.Crypto;
Expand All @@ -37,10 +40,20 @@
public abstract class BaseIntegrationTest {

@Container
static final GenericContainer<?> REDIS_CONTAINER = new GenericContainer<>("redis:5.0.3-alpine")
.withExposedPorts(6379);
public static final PostgreSQLContainer<?> POSTGRES_SQL_CONTAINER = new PostgreSQLContainer<>(
"postgres:16.1")
.withDatabaseName("riven_of_a_thousand_servers")
.withUsername("username")
.withPassword("password");

@Container
private static final GenericContainer<?> REDIS_CONTAINER = new GenericContainer<>(
"redis:5.0.3-alpine").withExposedPorts(6379);

private static final String MALICIOUS_PRIVATE_KEY = "CE4517095255B0C92D586AF9EEC27B998D68775363F9FE74341483FB3A657CEC";

private static final String VALID_PRIVATE_KEY = "F0EA3A0516695324C03ED552CD5A08A58CA1248172E8816C3BF235E52E75A7BF";

@LocalServerPort
protected int localServerPort;

Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ spring:
data:
redis:
port: 6379
r2dbc:
url: r2dbc:tc:postgresql:///riven_of_a_thousand_servers?postgres=16.1
username: username
password: password
flyway:
url: jdbc:tc:postgresql:16.1:///riven_of_a_thousand_servers
enabled: true

bungie:
api:
Expand Down

0 comments on commit 83f9136

Please sign in to comment.