From 6b039e7a5fbbf2775b2307a430783c3dc0aee0f9 Mon Sep 17 00:00:00 2001 From: Eric Opoku Date: Sun, 17 Nov 2024 14:38:21 -0500 Subject: [PATCH] silent exception when admin user already exists --- build.gradle | 4 ++-- gradle/wrapper/gradle-wrapper.properties | 2 +- .../backend/bootstrap/DatabaseSeeder.java | 15 +++++++++++---- .../resources/application-production.properties | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 6aefcec..61b779e 100644 --- a/build.gradle +++ b/build.gradle @@ -114,7 +114,7 @@ testing { implementation 'io.findify:s3mock_2.13:0.2.6' implementation 'com.icegreen:greenmail-junit5:2.1.0' implementation 'com.jparams:to-string-verifier:1.4.8' - implementation 'nl.jqno.equalsverifier:equalsverifier:3.17.1' + implementation 'nl.jqno.equalsverifier:equalsverifier:3.17.3' implementation 'org.springframework.security:spring-security-test' implementation 'org.springframework.boot:spring-boot-starter-test' } @@ -128,7 +128,7 @@ testing { implementation sourceSets.test.output implementation 'org.springframework.boot:spring-boot-testcontainers' - implementation 'com.amazonaws:aws-java-sdk-core:1.12.765' + implementation 'com.amazonaws:aws-java-sdk-core:1.12.778' implementation 'org.testcontainers:junit-jupiter' implementation 'org.testcontainers:postgresql' } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e8bd283..cdc580f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/com/developersboard/backend/bootstrap/DatabaseSeeder.java b/src/main/java/com/developersboard/backend/bootstrap/DatabaseSeeder.java index c102445..d0e69aa 100644 --- a/src/main/java/com/developersboard/backend/bootstrap/DatabaseSeeder.java +++ b/src/main/java/com/developersboard/backend/bootstrap/DatabaseSeeder.java @@ -5,23 +5,26 @@ import com.developersboard.backend.service.user.UserService; import com.developersboard.constant.EnvConstants; import com.developersboard.enums.RoleType; +import com.developersboard.exception.user.UserAlreadyExistsException; import com.developersboard.shared.util.UserUtils; import java.util.Arrays; import java.util.Collections; import java.util.Set; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; /** - * A convenient class to initializes and save user data on application start. + * A convenient class to initialize and save user data on application start. * * @author George Anguah * @version 1.0 * @since 1.0 */ +@Slf4j @Component @RequiredArgsConstructor public class DatabaseSeeder implements CommandLineRunner { @@ -50,9 +53,13 @@ public void run(String... args) { } private void persistDefaultAdminUser() { - var adminDto = UserUtils.createUserDto(adminUsername, adminPassword, adminEmail, true); - Set adminRoleType = Collections.singleton(RoleType.ROLE_ADMIN); + try { + var adminDto = UserUtils.createUserDto(adminUsername, adminPassword, adminEmail, true); + Set adminRoleType = Collections.singleton(RoleType.ROLE_ADMIN); - userService.createUser(adminDto, adminRoleType); + userService.createUser(adminDto, adminRoleType); + } catch (UserAlreadyExistsException e) { + LOG.warn("Admin user already exists!"); + } } } diff --git a/src/main/resources/application-production.properties b/src/main/resources/application-production.properties index 9b1cd9d..0645912 100644 --- a/src/main/resources/application-production.properties +++ b/src/main/resources/application-production.properties @@ -11,7 +11,7 @@ spring.datasource.hikari.schema=spring_boot_starter # = JPA / HIBERNATE # =============================== # Here we only want to validate that the schema is updated and valid. -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=validate spring.sql.init.mode=always # =============================== # LOGGING