Skip to content

Commit 90e493e

Browse files
committed
SDK-368: SDK should prompt for database uri again if an invalid uri was entered
1 parent 60f6f66 commit 90e493e

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -428,34 +428,36 @@ private void setupDatabaseForServer(Server server) throws MojoExecutionException
428428
}
429429

430430
if (server.isMySqlDb() || server.isPostgreSqlDb()) {
431-
String uri = getUriWithoutDb(server);
432431
boolean connectionEstablished = false;
433432
int maxAttempts = 3;
434433
int attempts = 0;
435434

436435
while (!connectionEstablished && attempts < maxAttempts) {
437436
attempts++;
437+
String uri = getUriWithoutDb(server);
438438
try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) {
439439
connector.checkAndCreate(server);
440440
wizard.showMessage("Connected to the database.");
441441
connectionEstablished = true;
442442
}
443443
catch (SQLException e) {
444-
if (e.getMessage().contains("Invalid database credentials")) {
444+
String message = e.getMessage();
445+
wizard.showMessage(String.format("Database connection failed (attempt %d of %d): %s", attempts, maxAttempts, message));
446+
if (message.contains("Invalid database credentials")) {
445447
if (attempts == maxAttempts) {
446-
throw new MojoExecutionException(
447-
String.format("Failed to connect to database after %d attempts. Please verify your credentials and try again.", maxAttempts),
448-
e
449-
);
448+
throw new MojoExecutionException(String.format("Failed to connect to database after %d attempts. " +
449+
"Please verify your credentials and try again.", maxAttempts), e);
450450
}
451451

452-
wizard.showMessage(String.format("Database connection failed (attempt %d of %d): %s", attempts, maxAttempts, e.getMessage()));
453-
String newUser = wizard.promptForValueIfMissingWithDefault("Please specify correct database username (-D%s)", dbUser, "dbUser", "root");
454-
String newPassword = wizard.promptForPasswordIfMissingWithDefault("Please specify correct database password (-D%s)", dbPassword, "dbPassword", "");
455-
456-
server.setDbUser(newUser);
457-
server.setDbPassword(newPassword);
452+
wizard.promptForDbCredentialsAgain(server, dbUser, dbPassword);
453+
continue;
454+
} else if (message.contains("Incorrect Database Uri")) {
455+
if (attempts == maxAttempts) {
456+
throw new MojoExecutionException(String.format("Failed to connect to database after %d attempts. " +
457+
"Please verify your database uri.", maxAttempts), e);
458+
}
458459

460+
wizard.promptForNewUriAndCredentials(server, dbUser, dbPassword, dbUri);
459461
continue;
460462
}
461463
throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e);

maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public DBConnector(String url, String user, String pass, String dbName) throws S
2727
} catch (SQLException e2) {
2828
if (e2.getMessage().contains("Access denied")) {
2929
throw new SQLException("Invalid database credentials. Please check your username and password.", e2);
30+
} else if (e2.getMessage().contains("No suitable driver found for")) {
31+
throw new SQLException("Incorrect Database Uri. Please provide a correct database uri.", e2);
3032
}
3133
throw e2;
3234
}

sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,51 @@ public void promptForDbCredentialsIfMissing(Server server) throws MojoExecutionE
947947
server.setDbPassword(dbPassword);
948948
}
949949

950+
@Override
951+
public void promptForDbCredentialsAgain(Server server, String dbUser, String dbPassword) throws MojoExecutionException {
952+
String newUser = promptForValueIfMissingWithDefault("Please specify correct database username (-D%s)", dbUser,
953+
"dbUser", "root");
954+
955+
String newPassword = promptForPasswordIfMissingWithDefault("Please specify correct database password (-D%s)", dbPassword,
956+
"dbPassword", "");
957+
958+
server.setDbUser(newUser);
959+
server.setDbPassword(newPassword);
960+
}
961+
962+
@Override
963+
public void promptForNewUriAndCredentials(Server server, String dbUser, String dbPassword, String dbUri) throws MojoExecutionException {
964+
showMessage("Prompting for new database URI and credentials...");
965+
String uriTemplate;
966+
if (server.isMySqlDb()) {
967+
uriTemplate = SDKConstants.URI_MYSQL;
968+
} else if (server.isPostgreSqlDb()) {
969+
uriTemplate = SDKConstants.URI_POSTGRESQL;
970+
} else {
971+
return;
972+
}
973+
974+
String newUri = promptForValueIfMissingWithDefault(
975+
"The distribution requires a " +
976+
(server.isMySqlDb() ? "MySQL" : "PostgreSQL") +
977+
" database. Please specify a valid database uri (-D%s)",
978+
dbUri, "dbUri", uriTemplate);
979+
980+
if (server.isMySqlDb()) {
981+
newUri = addMySQLParamsIfMissing(newUri);
982+
} else if (server.isPostgreSqlDb()) {
983+
newUri = addPostgreSQLParamsIfMissing(newUri);
984+
}
985+
986+
newUri = newUri.replace(DBNAME_URL_VARIABLE, server.getServerId());
987+
server.setDbUri(newUri);
988+
989+
String newUser = promptForValueIfMissingWithDefault("Please enter the database username (-D%s):", dbUser, "dbUser", "root");
990+
String newPassword = promptForPasswordIfMissingWithDefault("Please enter the database password (-D%s):", dbPassword, "dbPassword", "");
991+
server.setDbUser(newUser);
992+
server.setDbPassword(newPassword);
993+
}
994+
950995
/**
951996
* Get servers with recently used first
952997
*/

sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public interface Wizard {
5656

5757
String promptForExistingServerIdIfMissing(String serverId) throws MojoExecutionException;
5858

59+
void promptForNewUriAndCredentials(Server server, String dbUser, String dbPassword, String dbUri) throws MojoExecutionException;
60+
5961
List<String> getListOfServers() throws MojoExecutionException;
6062

6163
void showJdkErrorMessage(String jdk, String platform, String recommendedJdk, String pathToProps);
@@ -64,5 +66,5 @@ public interface Wizard {
6466

6567
void setAnswers(ArrayDeque<String> batchAnswers);
6668

67-
String promptForPasswordIfMissingWithDefault(String s, String dbPassword, String dbPassword1, String s1) throws MojoExecutionException;
69+
void promptForDbCredentialsAgain(Server server, String dbUser, String dbPassword) throws MojoExecutionException;
6870
}

0 commit comments

Comments
 (0)