From c8781e8205697585a0318c28e17f590a3f5fa310 Mon Sep 17 00:00:00 2001 From: zyxxoo <1318247699@qq.com> Date: Mon, 30 May 2022 23:34:38 +0800 Subject: [PATCH] fix: duplicate key error --- .../hugegraph/backend/store/mysql/MysqlTables.java | 8 ++++---- .../backend/store/postgresql/PostgresqlTables.java | 13 +++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlTables.java b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlTables.java index edba1cfac3..5ce16422fe 100644 --- a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlTables.java +++ b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlTables.java @@ -102,10 +102,10 @@ public Meta(Map typesMapping) { } public void writeVersion(Session session, String driverVersion) { - String insert = String.format("INSERT INTO %s VALUES ('%s', '%s')", - this.table(), - formatKey(HugeKeys.VERSION), - driverVersion); + String versionColumn = formatKey(HugeKeys.VERSION); + String insert = String.format("INSERT IGNORE INTO %s VALUES " + + "('%s', '%s')", this.table(), + versionColumn, driverVersion); try { session.execute(insert); } catch (SQLException throwables) { diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlTables.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlTables.java index e9c1a954c4..f617f403ec 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlTables.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlTables.java @@ -82,8 +82,17 @@ public Meta() { } public void writeVersion(Session session, String version) { - MysqlTables.Meta table = (MysqlTables.Meta) this.template; - table.writeVersion(session, version); + String versionColumn = formatKey(HugeKeys.VERSION); + String insert = String.format("INSERT INTO %s VALUES ('%s', '%s') " + + "ON CONFLICT(%s) DO NOTHING;", + this.table(), versionColumn, version, + versionColumn); + try { + session.execute(insert); + } catch (SQLException throwables) { + throw new BackendException("Failed to insert driver version " + + "with '%s'", insert); + } } public String readVersion(Session session) {