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

SDK-368: SDK should prompt for the database logins again if invalid logins were entered #324

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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;
}
Loading