Skip to content

Commit

Permalink
silent exception when admin user already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Opoku committed Nov 17, 2024
1 parent b7d8a95 commit 6b039e7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -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'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -50,9 +53,13 @@ public void run(String... args) {
}

private void persistDefaultAdminUser() {
var adminDto = UserUtils.createUserDto(adminUsername, adminPassword, adminEmail, true);
Set<RoleType> adminRoleType = Collections.singleton(RoleType.ROLE_ADMIN);
try {
var adminDto = UserUtils.createUserDto(adminUsername, adminPassword, adminEmail, true);
Set<RoleType> adminRoleType = Collections.singleton(RoleType.ROLE_ADMIN);

userService.createUser(adminDto, adminRoleType);
userService.createUser(adminDto, adminRoleType);
} catch (UserAlreadyExistsException e) {
LOG.warn("Admin user already exists!");
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application-production.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b039e7

Please sign in to comment.