Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from javaBin/db/setup-db
Browse files Browse the repository at this point in the history
Setup db
  • Loading branch information
tanettrimas authored Jan 20, 2024
2 parents c8ffca9 + e993767 commit 6ccc2c4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
15 changes: 15 additions & 0 deletions backend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies {
val slf4j_version = "2.0.9"
val kotlin_version = "1.9.20"
val logstash_version = "7.4"
val flyway_version = "10.6.0"
val hikari_version = "5.1.0"
val postgres_version = "42.7.1"

// Use the Kotlin JUnit 5 integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
Expand Down Expand Up @@ -57,6 +60,18 @@ dependencies {
// Kotlin Query
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
implementation("com.github.seratch:kotliquery:1.9.0")


// Database
// https://mvnrepository.com/artifact/org.flywaydb/flyway-core - database migrations
implementation("org.flywaydb:flyway-core:$flyway_version")
implementation("org.flywaydb:flyway-database-postgresql:$flyway_version")

// https://mvnrepository.com/artifact/com.zaxxer/HikariCP - connection pooling
implementation("com.zaxxer:HikariCP:$hikari_version")
// https://mvnrepository.com/artifact/org.postgresql/postgresql - database driver
implementation("org.postgresql:postgresql:$postgres_version")

}

// Apply a specific Java toolchain to ease working on different environments.
Expand Down
1 change: 1 addition & 0 deletions backend/app/src/main/kotlin/backend/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fun server() = embeddedServer(factory = Netty, port = 8080) {
}

private fun Application.createServer() {
runMigration()
configureAuth()
configureRouting()
}
Expand Down
26 changes: 26 additions & 0 deletions backend/app/src/main/kotlin/backend/Database.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package backend

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.flywaydb.core.Flyway
import org.postgresql.ds.PGSimpleDataSource

fun runMigration() {
val dataSource = HikariDataSource(HikariConfig().apply {
username = "postgres"
password = "example"
dataSourceClassName = PGSimpleDataSource::class.qualifiedName
addDataSourceProperty("databaseName", "workshop")
addDataSourceProperty("serverName", "localhost")
addDataSourceProperty("portNumber", "5432")
maximumPoolSize = 10
minimumIdle = 1
idleTimeout = 10001
connectionTimeout = 1000
maxLifetime = 30001
})
Flyway
.configure()
.dataSource(dataSource)
.load().migrate()
}
30 changes: 30 additions & 0 deletions backend/app/src/main/resources/db/migration/V1__initial.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
create table users
(
id int GENERATED ALWAYS AS IDENTITY primary key,
first_name varchar,
last_name varchar,
email varchar,
is_admin boolean
);

create table workshop
(
id int GENERATED ALWAYS AS IDENTITY primary key,
title varchar not null,
description varchar,
start_time timestamp with time zone not null,
end_time timestamp with time zone not null,
capacity int not null
);

create table workshop_registration
(
id int GENERATED ALWAYS AS IDENTITY primary key,
user_id int,
workshop_id int,
state int not null default 0,
created_at timestamp with time zone not null default current_timestamp,
updated_at timestamp with time zone not null default current_timestamp,
constraint fk_workshop foreign key (workshop_id) references workshop (id),
constraint fk_user foreign key (user_id) references users (id)
);
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
database:
container_name: database
image: postgres:16.0
restart: on-failure
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: workshop
volumes:
- pgdata:/var/lib/postgres/data

volumes:
pgdata:

0 comments on commit 6ccc2c4

Please sign in to comment.