Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom Liquibase credentials #39361

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Adjust the new Liquibase tests a bit
Not sure exactly what was going wrong but I had to create two users to
make things work and also make sure everything was done in one go.
  • Loading branch information
gsmet committed Apr 17, 2024
commit 0258098f5a69be915a6b74d6a14b08422d6ed7fc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -33,7 +33,7 @@ public class LiquibaseFunctionalityResource {

@Inject
@DataSource("second")
AgroalDataSource dataSource;
AgroalDataSource secondDataSource;

@GET
@Path("update")
Expand All @@ -46,6 +46,7 @@ public String doUpdateAuto() {
liquibaseFactory.createLabels());
List<ChangeSetStatus> changeSets = Objects.requireNonNull(status,
"ChangeSetStatus is null! Database update was not applied");

return changeSets.stream()
.filter(ChangeSetStatus::getPreviouslyRan)
.map(ChangeSetStatus::getChangeSet)
Expand All @@ -65,29 +66,22 @@ public String updateWithDedicatedUser() {
liquibaseSecondFactory.createLabels());
List<ChangeSetStatus> changeSets = Objects.requireNonNull(status,
"ChangeSetStatus is null! Database update was not applied");
return changeSets.stream()
.filter(ChangeSetStatus::getPreviouslyRan)
.map(ChangeSetStatus::getChangeSet)
.map(ChangeSet::getId)
.collect(Collectors.joining(","));

try (Connection connection = secondDataSource.getConnection()) {
try (Statement s = connection.createStatement()) {
ResultSet rs = s.executeQuery("SELECT CREATEDBY FROM QUARKUS_TABLE WHERE ID = 1");
if (rs.next()) {
return rs.getString("CREATEDBY");
}
return null;
}
}
} catch (Exception ex) {
throw new WebApplicationException(ex.getMessage(), ex);
}

}

@GET
@Path("created-by")
public String returnCreatedByUser() throws SQLException {
try (Connection connection = dataSource.getConnection()) {
ResultSet s = connection.createStatement().executeQuery("SELECT CREATEDBY FROM QUARKUS_TABLE WHERE ID = 1");
if (s.next()) {
return s.getString("CREATEDBY");
}
return null;
}
}

private void assertCommandScopeResolvesProperly() {
try {
new CommandScope("dropAll");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
quarkus.datasource.db-kind=h2
quarkus.datasource.username=sa
quarkus.datasource.password=sa
quarkus.datasource.jdbc.url=jdbc:h2:mem:test
quarkus.datasource.jdbc.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1

# Second datasource
quarkus.datasource.second.db-kind=h2
quarkus.datasource.second.username=sa
quarkus.datasource.second.password=sa
quarkus.datasource.second.username=readonly
quarkus.datasource.second.password=readonly
quarkus.datasource.second.jdbc.url=jdbc:h2:mem:second;INIT=RUNSCRIPT FROM 'src/main/resources/db/second/initdb.sql'

# Liquibase config properties
Expand All @@ -18,7 +18,7 @@ quarkus.liquibase.database-change-log-lock-table-name=changelog_lock
quarkus.liquibase.database-change-log-table-name=changelog

# Config for second datasource with different user / password
quarkus.liquibase.second.username=usr
quarkus.liquibase.second.username=admin
quarkus.liquibase.second.password=pass
quarkus.liquibase.second.change-log=db/second/changeLog.xml
quarkus.liquibase.second.clean-at-start=false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
CREATE USER IF NOT EXISTS usr PASSWORD 'pass' ADMIN;
GRANT ALL ON SCHEMA PUBLIC TO usr;
CREATE USER IF NOT EXISTS admin PASSWORD 'pass' ADMIN;
GRANT ALL ON SCHEMA PUBLIC TO admin;

CREATE USER IF NOT EXISTS readonly PASSWORD 'readonly' ADMIN;
GRANT SELECT ON SCHEMA PUBLIC TO readonly;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import io.quarkus.test.junit.QuarkusTest;

Expand All @@ -22,13 +20,9 @@ public void testLiquibaseQuarkusFunctionality() {

@Test
@DisplayName("Migrates a schema correctly using dedicated username and password from config properties")
@DisabledOnOs(value = OS.WINDOWS, disabledReason = "Our Windows CI does not have Docker installed properly")
public void testLiquibaseUsingDedicatedUsernameAndPassword() {
when().get("/liquibase/updateWithDedicatedUser").then().body(is(
"create-quarkus-table,insert-into-quarkus-table"));

when().get("/liquibase/created-by").then().body(is(
"USR"));
"ADMIN"));
}

static void doTestLiquibaseQuarkusFunctionality(boolean isIncludeAllExpectedToWork) {
Expand Down