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 24, 2024
1 parent c49fe8a commit ba834c0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
54 changes: 51 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 @@ -28,15 +31,16 @@ public class MySQLJdbcTest extends AbstractJdbcTest {
public static class MySQLJdbcLatestVersionTest {

@ClassRule
public static MySQLContainerExtension mysql = new MySQLContainerExtension("mysql:8.0.31");
public static MySQLContainerExtension mysql = new MySQLContainerExtension("mysql:8.0.31")
.withInitScript("init_mysql_script.sql");

@ClassRule
public static DbmsRule db = new ImpermanentDbmsRule();

@BeforeClass
public static void setUpContainer() {
mysql.start();
TestUtil.registerProcedure(db, Jdbc.class);
TestUtil.registerProcedure(db, Jdbc.class, Periodic.class);
}

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

@Test
public void testWithPeriodicRepeat() {
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
);
}
}

public static class MySQLJdbcFiveVersionTest {
Expand Down
2 changes: 2 additions & 0 deletions extended-it/src/test/resources/init_mysql_script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
USE test;
CREATE TABLE merchandise_id (id varchar(255), source varchar(255) );
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

0 comments on commit ba834c0

Please sign in to comment.