From c915b0b9b1e5189e067ac23ce5a3fc7d41d5657b Mon Sep 17 00:00:00 2001 From: xis Date: Sun, 22 Oct 2023 12:52:25 +0200 Subject: [PATCH] Postgresql support added --- README.md | 1 + build.gradle | 1 + .../util/DriverManagerDataSourceConfig.kt | 57 +++++++++++++++++++ src/main/resources/application.yml | 12 ++-- 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/net/schowek/nextclouddlna/util/DriverManagerDataSourceConfig.kt diff --git a/README.md b/README.md index c7990f3..eb55bfe 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Available env variables with their default values that you can overwrite: | NEXTCLOUD_DLNA_INTERFACE | eth0 | interface the server will be listening on | | NEXTCLOUD_DLNA_FRIENDLY_NAME | Nextcloud-DLNA | friendly name of the DLNA service | | NEXTCLOUD_DATA_DIR | | nextcloud installation directory (that ends with /data) | +| NEXTCLOUD_DB_TYPE | mariadb | nextcloud database type (mysql, mariadb, postgresql) | | NEXTCLOUD_DB_HOST | localhost | nextcloud database host | | NEXTCLOUD_DB_PORT | 3306 | nextcloud database port | | NEXTCLOUD_DB_NAME | nextcloud | nextcloud database name | diff --git a/build.gradle b/build.gradle index c985f2e..f37090a 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.mariadb.jdbc:mariadb-java-client:3.2.0' + implementation 'org.postgresql:postgresql:42.6.0' implementation 'org.jupnp:org.jupnp:2.7.1' implementation 'org.jupnp:org.jupnp.support:2.7.1' diff --git a/src/main/kotlin/net/schowek/nextclouddlna/util/DriverManagerDataSourceConfig.kt b/src/main/kotlin/net/schowek/nextclouddlna/util/DriverManagerDataSourceConfig.kt new file mode 100644 index 0000000..69bab07 --- /dev/null +++ b/src/main/kotlin/net/schowek/nextclouddlna/util/DriverManagerDataSourceConfig.kt @@ -0,0 +1,57 @@ +package net.schowek.nextclouddlna.util + +import mu.KLogging +import net.schowek.nextclouddlna.util.NextcloudDBType.* +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Profile +import org.springframework.jdbc.datasource.DriverManagerDataSource + +@Configuration +@Profile("!integration") +@EnableConfigurationProperties(NextcloudDBConfigProperties::class) +class DriverManagerDataSourceConfig { + @Bean + fun driverManagerDataSource(props: NextcloudDBConfigProperties): DriverManagerDataSource { + logger.info { "Using Nextcloud DB connection parameters: $props" } + return DriverManagerDataSource().also { dataSource -> + when (props.type) { + MARIADB, MYSQL -> { + dataSource.setDriverClassName("org.mariadb.jdbc.Driver"); + dataSource.url = "jdbc:mariadb://${props.host}:${props.port}/${props.name}"; + } + + POSTGRES -> { + dataSource.setDriverClassName("org.mariadb.jdbc.Driver"); + dataSource.url = "jdbc:postgresql://${props.host}:${props.port}/${props.name}"; + dataSource.connectionProperties?.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect") + } + + else -> throw RuntimeException("Unsupported DB type") + } + dataSource.username = props.user; + dataSource.password = props.pass; + } + } + + companion object : KLogging() +} + +@Profile("!integration") +@ConfigurationProperties(prefix = "nextcloud.db") +data class NextcloudDBConfigProperties( + val type: NextcloudDBType, + val host: String, + val port: Int, + val name: String, + val user: String, + val pass: String +) + +enum class NextcloudDBType(val value: String) { + MYSQL("mysql"), + MARIADB("mariadb"), + POSTGRES("postgres") +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 0c8c146..978edef 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -5,13 +5,15 @@ server: nextcloud: filesDir: ${NEXTCLOUD_DATA_DIR} + db: + type: ${NEXTCLOUD_DB_TYPE:mariadb} + host: ${NEXTCLOUD_DB_HOST:localhost} + port: ${NEXTCLOUD_DB_PORT:3306} + name: ${NEXTCLOUD_DB_NAME:nextcloud} + user: ${NEXTCLOUD_DB_USER:nextcloud} + pass: ${NEXTCLOUD_DB_PASS:nextcloud} spring: - datasource: - url: "jdbc:mariadb://${NEXTCLOUD_DB_HOST:localhost}:${NEXTCLOUD_DB_PORT:3306}/${NEXTCLOUD_DB_NAME:nextcloud}" - username: ${NEXTCLOUD_DB_USER:nextcloud} - password: ${NEXTCLOUD_DB_PASS:nextcloud} - driver-class-name: org.mariadb.jdbc.Driver jpa: hibernate: ddl-auto: none