Skip to content

Commit

Permalink
🗃️ Add database access to Komunumo closes #668
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <marcus@fihlon.swiss>
  • Loading branch information
McPringle committed Oct 12, 2024
1 parent 82dea5f commit 8e240b6
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ The server runs on port 8080 by default. If you don't like it, change it:
PORT=8080
```

### Database

*Komunumo* needs a database to store the business data. By default, *Komunumo* comes with [MariaDB](https://mariadb.org/) drivers. MariaDB is recommended because we are using it during development, and it is highly tested with *Komunumo*. All free and open source JDBC compatible databases are supported, but you need to configure the JDBC driver dependencies accordingly. Please make sure that your database is using a Unicode character set to avoid problems storing data containing Unicode characters.

The `DB_USER` is used to access the *Komunumo* database including automatic schema migrations and needs `ALL PRIVILEGES`.

```
DB_URL=jdbc:mariadb://localhost:3306/komunumo?serverTimezone\=Europe/Zurich&allowMultiQueries=true
DB_USER=johndoe
DB_PASS=verysecret
```

The database schema will be migrated automatically by *Komunumo*.

#### Important MySQL and MariaDB configuration

MySQL and MariaDB have a possible silent truncation problem with the `GROUP_CONCAT` command. To avoid this it is necessary, to configure these two databases to allow multi queries. Just add `allowMultiQueries=true` to the JDBC database URL like in this example (you may need to scroll the example code to the right):

```
DB_URL=jdbc:mariadb://localhost:3306/komunumo?serverTimezone\=Europe/Zurich&allowMultiQueries=true
```

## Build

### Maven
Expand Down
89 changes: 89 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@

<properties>
<java.version>21</java.version>
<jooq.version>3.19.13</jooq.version>
<maven.version>3.9.9</maven.version>
<vaadin.version>24.4.13</vaadin.version>
</properties>
Expand Down Expand Up @@ -140,6 +141,27 @@
<version>4.8.6</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -156,6 +178,17 @@
<version>2.1.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.20.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
<version>1.20.2</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -304,6 +337,62 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<id>jooq-codegen</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbc>
<driver>org.testcontainers.jdbc.ContainerDatabaseDriver</driver>
<url>jdbc:tc:mariadb:lts:///ignored?allowMultiQueries=true&amp;TC_TMPFS=/testtmpfs:rw&amp;TC_INITSCRIPT=file:${basedir}/src/main/resources/db/migration/V1_0_0__initialization.sql</url>
<user>test</user>
<password>test</password>
</jdbc>
<generator>
<database>
<name>org.jooq.meta.mariadb.MariaDBDatabase</name>
<inputSchema>test</inputSchema>
<outputSchemaToDefault>true</outputSchemaToDefault>
<forcedTypes>
<forcedType>
<name>BOOLEAN</name>
<includeTypes>(?i:TINYINT\(1\))</includeTypes>
</forcedType>
</forcedTypes>
<properties>
<property>
<key>sort</key>
<value>semantic</value>
</property>
</properties>
</database>
<generate>
<validationAnnotations>true</validationAnnotations>
<javaTimeTypes>true</javaTimeTypes>
</generate>
<target>
<packageName>org.komunumo.data.db</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta-extensions</artifactId>
<version>${jooq.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/komunumo/configuration/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Komunumo - Open Source Community Manager
* Copyright (C) Marcus Fihlon and the individual contributors to Komunumo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.komunumo.configuration;

import org.jetbrains.annotations.NotNull;

import java.util.Map;

public final class Configuration {

@SuppressWarnings("java:S1172")
public Configuration(@NotNull final Map<String, String> configuration) { }

}
37 changes: 37 additions & 0 deletions src/main/java/org/komunumo/data/service/ConfigurationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Komunumo - Open Source Community Manager
* Copyright (C) Marcus Fihlon and the individual contributors to Komunumo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.komunumo.data.service;

import org.komunumo.configuration.Configuration;
import org.komunumo.data.service.getter.DSLContextGetter;

import java.util.Collections;
import java.util.HashMap;

import static org.komunumo.data.db.tables.Configuration.CONFIGURATION;

interface ConfigurationService extends DSLContextGetter {

default Configuration loadConfigurationFromDatabase() {
final var configurationData = new HashMap<String, String>();
dsl().selectFrom(CONFIGURATION)
.forEach(configurationRecord -> configurationData.put(configurationRecord.getConfKey(), configurationRecord.getConfValue()));
return new Configuration(Collections.unmodifiableMap(configurationData));
}

}
57 changes: 57 additions & 0 deletions src/main/java/org/komunumo/data/service/DatabaseService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Komunumo - Open Source Community Manager
* Copyright (C) Marcus Fihlon and the individual contributors to Komunumo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.komunumo.data.service;

import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;
import org.komunumo.configuration.Configuration;
import org.komunumo.data.service.getter.ConfigurationGetter;
import org.komunumo.data.service.getter.DSLContextGetter;
import org.springframework.stereotype.Service;

@Service
public class DatabaseService implements DSLContextGetter, ConfigurationGetter, ConfigurationService {

private final DSLContext dsl;

private Configuration configuration;

public DatabaseService(@NotNull final DSLContext dsl) {
this.dsl = dsl;
this.configuration = loadConfigurationFromDatabase();
}

/**
* Get the {@link Configuration}.
* @return the {@link Configuration}
*/
@Override
public Configuration configuration() {
return configuration;
}

/**
* Get the {@link DSLContext} to access the database.
* @return the {@link DSLContext}
*/
@Override
public DSLContext dsl() {
return dsl;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Komunumo - Open Source Community Manager
* Copyright (C) Marcus Fihlon and the individual contributors to Komunumo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.komunumo.data.service.getter;

import org.komunumo.configuration.Configuration;

public interface ConfigurationGetter {

Configuration configuration();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Komunumo - Open Source Community Manager
* Copyright (C) Marcus Fihlon and the individual contributors to Komunumo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.komunumo.data.service.getter;

import org.jooq.DSLContext;

public interface DSLContextGetter {

DSLContext dsl();

}
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
server.port=${PORT:8080}
logging.level.org.atmosphere = warn

spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}
spring.flyway.placeholderReplacement=false

# Launch the default browser when starting the application in development mode
vaadin.launch-browser=false
6 changes: 6 additions & 0 deletions src/main/resources/db/migration/V1_0_0__initialization.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE `configuration` (
`conf_key` VARCHAR(255) NOT NULL,
`conf_value` MEDIUMTEXT NOT NULL DEFAULT '',

PRIMARY KEY (`conf_key`)
);
4 changes: 4 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.datasource.url=jdbc:tc:mariadb:lts:///test?allowMultiQueries=true
spring.datasource.username=test
spring.datasource.password=test
spring.flyway.placeholderReplacement=false

0 comments on commit 8e240b6

Please sign in to comment.