Skip to content

Commit

Permalink
SDK-368: SDK should prompt for the database logins again if invalid l…
Browse files Browse the repository at this point in the history
…ogins were entered (#324)
  • Loading branch information
mherman22 authored Jan 8, 2025
1 parent 0280368 commit eabdcbd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
37 changes: 31 additions & 6 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,37 @@ private void setupDatabaseForServer(Server server) throws MojoExecutionException

if (server.isMySqlDb() || server.isPostgreSqlDb()) {
String uri = getUriWithoutDb(server);
try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) {
connector.checkAndCreate(server);
wizard.showMessage("Connected to the database.");
}
catch (SQLException e) {
throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e);
boolean connectionEstablished = false;
int maxAttempts = 3;
int attempts = 0;

while (!connectionEstablished && attempts < maxAttempts) {
attempts++;
try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) {
connector.checkAndCreate(server);
wizard.showMessage("Connected to the database.");
connectionEstablished = true;
}
catch (SQLException e) {
if (e.getMessage().contains("Invalid database credentials")) {
if (attempts == maxAttempts) {
throw new MojoExecutionException(
String.format("Failed to connect to database after %d attempts. Please verify your credentials and try again.", maxAttempts),
e
);
}

wizard.showMessage(String.format("Database connection failed (attempt %d of %d): %s", attempts, maxAttempts, e.getMessage()));
String newUser = wizard.promptForValueIfMissingWithDefault("Please specify correct database username (-D%s)", dbUser, "dbUser", "root");
String newPassword = wizard.promptForPasswordIfMissingWithDefault("Please specify correct database password (-D%s)", dbPassword, "dbPassword", "");

server.setDbUser(newUser);
server.setDbPassword(newPassword);

continue;
}
throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e);
}
}

if (hasDbTables(server)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ public DBConnector(String url, String user, String pass, String dbName) throws S
try {
this.connection = DriverManager.getConnection(url, user, pass);
} catch (SQLException e) {
this.connection = DriverManager.getConnection(url, user, pass);
try {
this.connection = DriverManager.getConnection(url, user, pass);
} catch (SQLException e2) {
if (e2.getMessage().contains("Access denied")) {
throw new SQLException("Invalid database credentials. Please check your username and password.", e2);
}
throw e2;
}
}
this.dbName = dbName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ public interface Wizard {
boolean promptForConfirmDistroUpgrade(UpgradeDifferential upgradeDifferential) throws MojoExecutionException;

void setAnswers(ArrayDeque<String> batchAnswers);

String promptForPasswordIfMissingWithDefault(String s, String dbPassword, String dbPassword1, String s1) throws MojoExecutionException;
}

0 comments on commit eabdcbd

Please sign in to comment.