Skip to content

Commit 4ad56cb

Browse files
committed
fix(server): ensure graph clear API does not clear meta table
1 parent d7f35f1 commit 4ad56cb

3 files changed

Lines changed: 81 additions & 20 deletions

File tree

hugegraph-server/hugegraph-mysql/src/main/java/org/apache/hugegraph/backend/store/mysql/MysqlStore.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ public void truncate() {
222222
this.checkOpened();
223223

224224
this.truncateTables();
225-
this.init();
226225
LOG.debug("Store truncated: {}", this.store);
227226
}
228227

@@ -337,9 +336,6 @@ protected void clearTables() {
337336
protected void truncateTables() {
338337
Session session = this.sessions.session();
339338
for (MysqlTable table : this.tables()) {
340-
if (table instanceof MysqlTables.Meta || table instanceof MysqlTables.Counters){
341-
continue;
342-
}
343339
table.truncate(session);
344340
}
345341
}
@@ -509,7 +505,6 @@ public String storedVersion() {
509505
@Override
510506
protected Collection<MysqlTable> tables() {
511507
List<MysqlTable> tables = new ArrayList<>(super.tables());
512-
tables.add(this.meta);
513508
return tables;
514509
}
515510
}

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/mysql/BaseMysqlUnitTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
package org.apache.hugegraph.unit.mysql;
2121

2222
import org.apache.commons.configuration2.Configuration;
23+
import org.apache.hugegraph.backend.store.mysql.MysqlSessions;
2324
import org.apache.hugegraph.backend.store.mysql.MysqlStoreProvider;
2425
import org.apache.hugegraph.config.HugeConfig;
2526
import org.apache.hugegraph.testutil.Utils;
2627
import org.apache.hugegraph.unit.BaseUnitTest;
27-
import org.apache.hugegraph.unit.FakeObjects;
2828
import org.junit.After;
2929

3030
public class BaseMysqlUnitTest extends BaseUnitTest {
@@ -33,20 +33,19 @@ public class BaseMysqlUnitTest extends BaseUnitTest {
3333

3434
protected HugeConfig config;
3535
protected MysqlStoreProvider provider;
36+
protected MysqlSessions sessions;
3637

37-
public void setup() {
38-
try {
39-
Configuration conf = Utils.getConf();
40-
this.config = new HugeConfig(conf);
41-
} catch (Exception e) {
42-
this.config = FakeObjects.newConfig();
43-
}
38+
public void setup() throws Exception {
39+
Configuration conf = Utils.getConf();
40+
this.config = new HugeConfig(conf);
4441
this.provider = new MysqlStoreProvider();
4542
this.provider.open(GRAPH_NAME);
4643
this.provider.loadSystemStore(config).open(config);
4744
this.provider.loadGraphStore(config).open(config);
4845
this.provider.loadSchemaStore(config).open(config);
4946
this.provider.init();
47+
this.sessions = new MysqlSessions(config, GRAPH_NAME, this.provider.loadGraphStore(config).store());
48+
this.sessions.open();
5049
}
5150

5251
@After
@@ -55,7 +54,7 @@ public void down(){
5554
try {
5655
this.provider.close();
5756
} catch (Exception e) {
58-
// pass
57+
LOG.warn("Mysql provider close failed",e);
5958
}
6059
}
6160
}

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/mysql/MysqlTest.java

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,91 @@
2020
package org.apache.hugegraph.unit.mysql;
2121

2222
import org.apache.hugegraph.backend.store.BackendStore;
23+
import org.apache.hugegraph.backend.store.mysql.MysqlSessions;
24+
import org.apache.hugegraph.backend.store.mysql.ResultSetWrapper;
2325
import org.apache.hugegraph.testutil.Assert;
2426
import org.junit.Before;
2527
import org.junit.Test;
28+
import java.sql.PreparedStatement;
29+
import java.sql.SQLException;
2630

2731
public class MysqlTest extends BaseMysqlUnitTest{
2832
@Before
29-
public void setUp(){
33+
public void setUp() throws Exception {
3034
super.setup();
3135
}
3236

3337
@Test
34-
public void testMysqlMetaVersion(){
38+
public void testMysqlMetaVersion() throws SQLException {
3539
BackendStore systemStore = this.provider.loadSystemStore(config);
40+
41+
// Record system version before truncation
3642
String beforeVersion = systemStore.storedVersion();
43+
44+
// Get sessions for direct table operations
45+
MysqlSessions.Session session = this.sessions.session();
46+
session.open();
47+
48+
// Get the actual table names from the store
49+
String vertexTable = "g_v";
50+
String edgeOutTable = "g_oe";
51+
String edgeInTable = "g_ie";
52+
53+
// Insert test vertex data directly using simple SQL
54+
String insertVertex = String.format(
55+
"INSERT INTO %s (ID, LABEL, PROPERTIES, EXPIRED_TIME) VALUES ('test_vertex', 1, '{\"name\":\"test_vertex\"}', 0)",
56+
vertexTable
57+
);
58+
session.execute(insertVertex);
59+
60+
// Insert test edge data directly using simple SQL
61+
String insertEdgeOut = String.format(
62+
"INSERT INTO %s (OWNER_VERTEX, DIRECTION, LABEL, SUB_LABEL, SORT_VALUES, OTHER_VERTEX, PROPERTIES, EXPIRED_TIME) VALUES ('test_vertex', 0, 1, 0, '', 'target_vertex', '{\"weight\":1.0}', 0)",
63+
edgeOutTable
64+
);
65+
session.execute(insertEdgeOut);
66+
67+
String insertEdgeIn = String.format(
68+
"INSERT INTO %s (OWNER_VERTEX, DIRECTION, LABEL, SUB_LABEL, SORT_VALUES, OTHER_VERTEX, PROPERTIES, EXPIRED_TIME) VALUES ('target_vertex', 1, 1, 0, '', 'test_vertex', '{\"weight\":1.0}', 0)",
69+
edgeInTable
70+
);
71+
session.execute(insertEdgeIn);
72+
73+
// Verify data exists by querying
74+
String selectVertex = String.format("SELECT * FROM %s WHERE ID = 'test_vertex'", vertexTable);
75+
ResultSetWrapper vResult = session.select(selectVertex);
76+
Assert.assertTrue("vertex data should exist", vResult.next());
77+
vResult.close();
78+
79+
String selectEdgeOut = String.format("SELECT * FROM %s WHERE OWNER_VERTEX = 'test_vertex' AND DIRECTION = 0", edgeOutTable);
80+
ResultSetWrapper oeResult = session.select(selectEdgeOut);
81+
Assert.assertTrue("out edge data should exist", oeResult.next());
82+
oeResult.close();
83+
84+
String selectEdgeIn = String.format("SELECT * FROM %s WHERE OWNER_VERTEX = 'target_vertex' AND DIRECTION = 1", edgeInTable);
85+
ResultSetWrapper ieResult = session.select(selectEdgeIn);
86+
Assert.assertTrue("in edge data should exist", ieResult.next());
87+
ieResult.close();
88+
89+
// Execute truncate operation, clears all graph data but preserves system tables
3790
this.provider.truncate();
38-
String afterInitVersion = systemStore.storedVersion();
39-
Assert.assertNotNull(beforeVersion);
40-
Assert.assertNotNull(afterInitVersion);
41-
Assert.assertEquals(beforeVersion, afterInitVersion);
91+
92+
// Verify system version remains unchanged after truncation
93+
String afterVersion = systemStore.storedVersion();
94+
Assert.assertNotNull("System metadata version should exist", afterVersion);
95+
Assert.assertEquals("System metadata version should remain unchanged after truncation", beforeVersion, afterVersion);
96+
97+
// Verify data has been cleared
98+
vResult = session.select(selectVertex);
99+
Assert.assertFalse("vertex data should not exist", vResult.next());
100+
vResult.close();
101+
102+
oeResult = session.select(selectEdgeOut);
103+
Assert.assertFalse("out edge data should not exist", oeResult.next());
104+
oeResult.close();
105+
106+
ieResult = session.select(selectEdgeIn);
107+
Assert.assertFalse("in edge data should not exist", ieResult.next());
108+
ieResult.close();
42109
}
43110
}

0 commit comments

Comments
 (0)