Skip to content

Commit

Permalink
Merge pull request #962 from seraphinandrieux/add-user-with-flyaway
Browse files Browse the repository at this point in the history
Add mariadb into user migration with flyway
  • Loading branch information
pascalgrimaud authored Mar 8, 2022
2 parents a09c27f + f3deb6b commit 99dd486
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -155,7 +157,7 @@ public void addLoggerTest(Project project, String packageName, Level level) {

@Override
public Optional<String> getProperty(Project project, String key) {
Assert.notNull("project", project);
Assert.notNull(PROJECT_FIELD_ASSERT_NAME, project);
Assert.notBlank("key", key);

return FileUtils
Expand All @@ -171,16 +173,27 @@ public Optional<String> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ public interface SpringBootCommonService {

Optional<String> getProperty(Project project, String key);
boolean isSetWithMySQLOrMariaDBDatabase(Project project);
boolean isDatabaseUseSequences(Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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"),
Expand All @@ -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<String, Object> getFlywayProperties() {
TreeMap<String, Object> result = new TreeMap<>();

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,11 @@ void shouldNotCheckIfProjectIsSetWithMariaDbOrMySqlDatabaseWithoutProject() {
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("project");
}

@Test
void shouldNotCheckIfProjectIsDatabaseUseSequencesWithoutProject() {
assertThatThrownBy(() -> springBootCommonDomainService.isDatabaseUseSequences(null))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("project");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 99dd486

Please sign in to comment.