From f3deb6b0a9ba276f228e8d974c656c18c4d6b2fd Mon Sep 17 00:00:00 2001 From: seraphinandrieux Date: Tue, 8 Mar 2022 17:51:12 +0100 Subject: [PATCH] Add mariadb into user migration with flyway --- .../domain/SpringBootCommonDomainService.java | 21 +++++++++++++---- .../domain/SpringBootCommonService.java | 1 + .../dbmigration/flyway/domain/Flyway.java | 2 +- .../flyway/domain/FlywayDomainService.java | 23 ++++++++++++------- .../SpringBootCommonDomainServiceTest.java | 7 ++++++ .../domain/FlywayDomainServiceTest.java | 18 +++++++-------- 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainService.java index f26e52819db..ff664fa9080 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainService.java @@ -9,12 +9,14 @@ import java.util.Optional; import tech.jhipster.lite.common.domain.FileUtils; import tech.jhipster.lite.error.domain.Assert; +import tech.jhipster.lite.generator.project.domain.DatabaseType; import tech.jhipster.lite.generator.project.domain.Project; import tech.jhipster.lite.generator.project.domain.ProjectRepository; public class SpringBootCommonDomainService implements SpringBootCommonService { public static final String SOURCE = "server/springboot/common"; + private static final String PROJECT_FIELD_ASSERT_NAME = "project"; private final ProjectRepository projectRepository; @@ -155,7 +157,7 @@ public void addLoggerTest(Project project, String packageName, Level level) { @Override public Optional getProperty(Project project, String key) { - Assert.notNull("project", project); + Assert.notNull(PROJECT_FIELD_ASSERT_NAME, project); Assert.notBlank("key", key); return FileUtils @@ -171,16 +173,27 @@ public Optional getProperty(Project project, String key) { @Override public boolean isSetWithMySQLOrMariaDBDatabase(Project project) { - Assert.notNull("project", project); + Assert.notNull(PROJECT_FIELD_ASSERT_NAME, project); return isMariaDBDatabase(project) || isMySQLDatabase(project); } + @Override + public boolean isDatabaseUseSequences(Project project) { + Assert.notNull(PROJECT_FIELD_ASSERT_NAME, project); + + return hasSpecificDatabaseProperty(project, DatabaseType.POSTGRESQL); + } + private boolean isMySQLDatabase(Project project) { - return getProperty(project, "spring.datasource.url").filter(value -> value.contains("mysql")).isPresent(); + return hasSpecificDatabaseProperty(project, DatabaseType.MYSQL); } private boolean isMariaDBDatabase(Project project) { - return getProperty(project, "spring.datasource.url").filter(value -> value.contains("mariadb")).isPresent(); + return hasSpecificDatabaseProperty(project, DatabaseType.MARIADB); + } + + private boolean hasSpecificDatabaseProperty(Project project, DatabaseType databaseType) { + return getProperty(project, "spring.datasource.url").filter(value -> value.contains(databaseType.id())).isPresent(); } private void addLoggerToConfiguration( diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonService.java index 61bad047e14..22a84f99fac 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonService.java @@ -24,4 +24,5 @@ public interface SpringBootCommonService { Optional getProperty(Project project, String key); boolean isSetWithMySQLOrMariaDBDatabase(Project project); + boolean isDatabaseUseSequences(Project project); } diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/Flyway.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/Flyway.java index 740ac515909..2e98cceee15 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/Flyway.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/Flyway.java @@ -16,7 +16,7 @@ public static Dependency flywayDependency() { return Dependency.builder().groupId("org.flywaydb").artifactId("flyway-core").version("\\${flyway.version}").build(); } - public static Dependency additionalFlywayMysqlDependency() { + public static Dependency additionalFlywayMysqlAndMariaDBDependency() { return Dependency.builder().groupId("org.flywaydb").artifactId("flyway-mysql").version("\\${flyway.version}").build(); } } diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/FlywayDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/FlywayDomainService.java index 1eda072b816..ea4de538f13 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/FlywayDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/FlywayDomainService.java @@ -1,7 +1,7 @@ package tech.jhipster.lite.generator.server.springboot.dbmigration.flyway.domain; -import static tech.jhipster.lite.common.domain.FileUtils.getPath; -import static tech.jhipster.lite.generator.project.domain.Constants.MAIN_RESOURCES; +import static tech.jhipster.lite.common.domain.FileUtils.*; +import static tech.jhipster.lite.generator.project.domain.Constants.*; import static tech.jhipster.lite.generator.server.springboot.dbmigration.flyway.domain.Flyway.*; import java.time.Clock; @@ -57,10 +57,9 @@ public void addFlywayDependency(Project project) { } ); buildToolService.addDependency(project, Flyway.flywayDependency()); - springBootCommonService - .getProperty(project, "spring.datasource.url") - .filter(value -> value.contains("mysql")) - .ifPresent(value -> buildToolService.addDependency(project, Flyway.additionalFlywayMysqlDependency())); + if (springBootCommonService.isSetWithMySQLOrMariaDBDatabase(project)) { + buildToolService.addDependency(project, Flyway.additionalFlywayMysqlAndMariaDBDependency()); + } } @Override @@ -81,7 +80,7 @@ public void addProperties(Project project) { @Override public void addUserAuthorityChangelog(Project project) { - String sqlFileSource = "V00000000000000__create_user_authority_tables" + (isDatabaseUseSequences(project) ? "_postgresql.sql" : ".sql"); + String sqlFileSource = buildSqlFileSource(project); projectRepository.add( project, getPath(SQL_INIT_FILE_SOURCE, "resources/user"), @@ -91,6 +90,14 @@ public void addUserAuthorityChangelog(Project project) { ); } + private String buildSqlFileSource(Project project) { + String sqlFileSourcePrefix = "V00000000000000__create_user_authority_tables"; + if (isDatabaseUseSequences(project)) { + return sqlFileSourcePrefix + "_postgresql.sql"; + } + return sqlFileSourcePrefix + ".sql"; + } + private TreeMap getFlywayProperties() { TreeMap result = new TreeMap<>(); @@ -107,6 +114,6 @@ private String buildFileNameWithTimestamp(String fileNameFormat, int idxScript) } private boolean isDatabaseUseSequences(Project project) { - return springBootCommonService.getProperty(project, "spring.datasource.url").filter(value -> value.contains("postgresql")).isPresent(); + return springBootCommonService.isDatabaseUseSequences(project); } } diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainServiceTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainServiceTest.java index 1a445824df1..3ea611f8b79 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainServiceTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/common/domain/SpringBootCommonDomainServiceTest.java @@ -238,4 +238,11 @@ void shouldNotCheckIfProjectIsSetWithMariaDbOrMySqlDatabaseWithoutProject() { .isExactlyInstanceOf(MissingMandatoryValueException.class) .hasMessageContaining("project"); } + + @Test + void shouldNotCheckIfProjectIsDatabaseUseSequencesWithoutProject() { + assertThatThrownBy(() -> springBootCommonDomainService.isDatabaseUseSequences(null)) + .isExactlyInstanceOf(MissingMandatoryValueException.class) + .hasMessageContaining("project"); + } } diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/FlywayDomainServiceTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/FlywayDomainServiceTest.java index 632a5572179..16d2999bda1 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/FlywayDomainServiceTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/dbmigration/flyway/domain/FlywayDomainServiceTest.java @@ -1,11 +1,10 @@ package tech.jhipster.lite.generator.server.springboot.dbmigration.flyway.domain; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; -import static tech.jhipster.lite.TestUtils.tmpProject; -import static tech.jhipster.lite.generator.server.springboot.dbmigration.flyway.domain.Flyway.DEFAULT_FLYWAY_ENABLED; -import static tech.jhipster.lite.generator.server.springboot.dbmigration.flyway.domain.Flyway.DEFAULT_FLYWAY_LOCATIONS; +import static tech.jhipster.lite.TestUtils.*; +import static tech.jhipster.lite.generator.server.springboot.dbmigration.flyway.domain.Flyway.*; import java.time.Clock; import java.time.Instant; @@ -53,7 +52,7 @@ void shouldInit() { Project project = tmpProject(); when(buildToolService.getVersion(any(Project.class), anyString())).thenReturn(Optional.of("0.0.0")); - when(springBootCommonService.getProperty(project, "spring.datasource.url")).thenReturn(Optional.empty()); + when(springBootCommonService.isSetWithMySQLOrMariaDBDatabase(project)).thenReturn(false); // When flywayDomainService.init(project); @@ -91,12 +90,12 @@ void shouldNotAddFlywayDependency() { } @Test - void shouldInitWithAdditionalFlywayMysqlDependency() { + void shouldInitWithAdditionalFlywayMysqlMariaDBDependency() { // Given Project project = tmpProject(); when(buildToolService.getVersion(any(Project.class), anyString())).thenReturn(Optional.of("0.0.0")); - when(springBootCommonService.getProperty(project, "spring.datasource.url")).thenReturn(Optional.of("jdbc:mysql://localhost:3306/myDb")); + when(springBootCommonService.isSetWithMySQLOrMariaDBDatabase(project)).thenReturn(true); // When flywayDomainService.init(project); @@ -140,8 +139,7 @@ void shouldAddUserAuthorityChangelogWithSequence() { // Given Project project = tmpProject(); - when(springBootCommonService.getProperty(project, "spring.datasource.url")) - .thenReturn(Optional.of("jdbc:postgresql://localhost:3306/myDb")); + when(springBootCommonService.isDatabaseUseSequences(project)).thenReturn(true); // When flywayDomainService.addUserAuthorityChangelog(project); @@ -162,7 +160,7 @@ void shouldAddUserAuthorityChangelogWithoutSequence() { // Given Project project = tmpProject(); - when(springBootCommonService.getProperty(project, "spring.datasource.url")).thenReturn(Optional.of("jdbc:mysql://localhost:3306/myDb")); + when(springBootCommonService.isDatabaseUseSequences(project)).thenReturn(false); // When flywayDomainService.addUserAuthorityChangelog(project);