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

fix psotgresql init failed due to not exist same name db with user #1293

Merged
merged 2 commits into from
Dec 14, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ cassandra.password=
#jdbc.url=jdbc:postgresql://localhost:5432/
#jdbc.username=postgres
#jdbc.password=
#jdbc.postgresql.connect_database=template1

# palo backend config
#palo.host=127.0.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.stream.IntStream;

import org.apache.http.client.utils.URIBuilder;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;

import com.baidu.hugegraph.backend.BackendException;
Expand Down Expand Up @@ -213,13 +214,7 @@ private Connection open(boolean autoReconnect) throws SQLException {

protected String buildUri(boolean withConnParams, boolean withDB,
boolean autoReconnect, Integer timeout) {
String url = this.config.get(MysqlOptions.JDBC_URL);
if (!url.endsWith("/")) {
url = String.format("%s/", url);
}
if (withDB) {
url = String.format("%s%s", url, this.database());
}
String url = this.buildUrlPrefix(withDB);

int maxTimes = this.config.get(MysqlOptions.JDBC_RECONNECT_MAX_TIMES);
int interval = this.config.get(MysqlOptions.JDBC_RECONNECT_INTERVAL);
Expand All @@ -241,6 +236,19 @@ protected String buildUri(boolean withConnParams, boolean withDB,
return builder.toString();
}

protected String buildUrlPrefix(boolean withDB) {
String url = this.config.get(MysqlOptions.JDBC_URL);
if (!url.endsWith("/")) {
url = String.format("%s/", url);
}
String database = withDB ? this.database() : this.connectDatabase();
return String.format("%s%s", url, database);
}

protected String connectDatabase() {
return Strings.EMPTY;
}

protected URIBuilder newConnectionURIBuilder() {
return new URIBuilder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

package com.baidu.hugegraph.backend.store.postgresql;

import static com.baidu.hugegraph.config.OptionChecker.disallowEmpty;

import com.baidu.hugegraph.backend.store.mysql.MysqlOptions;
import com.baidu.hugegraph.config.ConfigOption;

public class PostgresqlOptions extends MysqlOptions {

Expand All @@ -36,4 +39,13 @@ public static synchronized PostgresqlOptions instance() {
}
return instance;
}

public static final ConfigOption<String> POSTGRESQL_CONNECT_DATABASE =
new ConfigOption<>(
"jdbc.postgresql.connect_database",
"The database used to connect when init store, " +
"drop store or check store exist.",
disallowEmpty(),
"template1"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ protected URIBuilder newConnectionURIBuilder() {
return new URIBuilder().addParameter("loggerLevel", "OFF");
}

@Override
protected String connectDatabase() {
return this.config().get(PostgresqlOptions.POSTGRESQL_CONNECT_DATABASE);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to mysql


public static String escapeAndWrapString(String value) {
StringBuilder builder = new StringBuilder(8 + value.length());
builder.append('\'');
Expand Down