Skip to content

Commit

Permalink
Fixes #4136: apoc.load.jdbcUpdate doesn't work in apoc.periodic.repea…
Browse files Browse the repository at this point in the history
…t procedure
  • Loading branch information
vga91 committed Jul 25, 2024
1 parent c49fe8a commit 45a8221
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
63 changes: 60 additions & 3 deletions extended-it/src/test/java/apoc/load/MySQLJdbcTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package apoc.load;

import apoc.util.s3.MySQLContainerExtension;
import apoc.periodic.Periodic;
import apoc.util.TestUtil;
import apoc.util.Util;
import apoc.util.s3.MySQLContainerExtension;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.neo4j.graphdb.Result;
import org.neo4j.test.rule.DbmsRule;
import org.neo4j.test.rule.ImpermanentDbmsRule;

Expand All @@ -19,6 +21,7 @@
import java.util.Map;

import static apoc.util.TestUtil.testCall;
import static apoc.util.TestUtil.testCallEventually;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand All @@ -36,7 +39,7 @@ public static class MySQLJdbcLatestVersionTest {
@BeforeClass
public static void setUpContainer() {
mysql.start();
TestUtil.registerProcedure(db, Jdbc.class);
TestUtil.registerProcedure(db, Jdbc.class, Periodic.class);
}

@AfterClass
Expand All @@ -54,6 +57,11 @@ public void testLoadJdbc() {
public void testIssue3496() {
MySQLJdbcTest.testIssue3496(db, mysql);
}

@Test
public void testWithPeriodicRepeat() {
MySQLJdbcTest.testPeriodicRepeat(db, mysql);
}
}

public static class MySQLJdbcFiveVersionTest {
Expand All @@ -67,7 +75,7 @@ public static class MySQLJdbcFiveVersionTest {
@BeforeClass
public static void setUpContainer() {
mysql.start();
TestUtil.registerProcedure(db, Jdbc.class);
TestUtil.registerProcedure(db, Jdbc.class, Periodic.class);
}

@AfterClass
Expand All @@ -85,8 +93,57 @@ public void testLoadJdbc() {
public void testIssue3496() {
MySQLJdbcTest.testIssue3496(db, mysql);
}

@Test
public void testWithPeriodicRepeat() {
MySQLJdbcTest.testPeriodicRepeat(db, mysql);
}
}


private static void testPeriodicRepeat(DbmsRule db, MySQLContainerExtension mysql) {
String url = mysql.getJdbcUrl();

String sqlQuery = "insert ignore into merchandise_id (id, source) values ('112233', 'Example Data 112233')";
String query = """
call apoc.periodic.repeat(
'000. test',
'call apoc.load.jdbcUpdate(
$url,
$sqlQuery,
[],
{credentials: {user: $user, password: $password}}) YIELD row',
1,
{ params: $params }
);
""";

db.executeTransactionally(
query,
Util.map("params", Util.map(
"url", url,
"sqlQuery", sqlQuery,
"user", mysql.getUsername(),
"password", mysql.getPassword()
)),
Result::resultAsString
);

testCallEventually(db, """
WITH $url as url
CALL apoc.load.jdbc(url, "merchandise_id", [], {credentials: {user: $user, password: $password}}) YIELD row
RETURN count(*);
""",
Util.map(
"url", url,
"user", mysql.getUsername(),
"password", mysql.getPassword()
),
(row) -> assertEquals(2L, row.get("count(*)")),
3
);
}

private static void testLoadJdbc(DbmsRule db, MySQLContainerExtension mysql) {
// with the config {timezone: 'UTC'} and `preserveInstants=true&connectionTimeZone=SERVER` to make the result deterministic,
// since `TIMESTAMP` values are automatically converted from the session time zone to UTC for storage, and vice versa.
Expand Down
2 changes: 1 addition & 1 deletion extended/src/main/java/apoc/load/Jdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private Stream<RowResult> executeQuery(String urlOrKey, String tableOrSelect, Ma
}
}

@Procedure(mode = Mode.DBMS)
@Procedure(mode = Mode.READ)
@Description("apoc.load.jdbcUpdate('key or url','statement',[params],config) YIELD row - update relational database, from a SQL statement with optional parameters")
public Stream<RowResult> jdbcUpdate(@Name("jdbc") String urlOrKey, @Name("query") String query, @Name(value = "params", defaultValue = "[]") List<Object> params, @Name(value = "config",defaultValue = "{}") Map<String, Object> config) {
log.info( String.format( "Executing SQL update: %s", query ) );
Expand Down
2 changes: 2 additions & 0 deletions extended/src/test/resources/init_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@ INSERT INTO `countrylanguage` VALUES ('NLD','Turkish','F',0.8);
COMMIT;

SET AUTOCOMMIT=1;

CREATE TABLE merchandise_id (id varchar(255), source varchar(255) );

0 comments on commit 45a8221

Please sign in to comment.