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

[hDJkPkOQ] Add QueryType validation in apoc.trigger.add #3421

Merged
merged 2 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion core/src/main/java/apoc/trigger/TriggerNewProcedures.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.util.stream.Stream;

import static org.neo4j.configuration.GraphDatabaseSettings.SYSTEM_DATABASE_NAME;
import static org.neo4j.graphdb.QueryExecutionType.QueryType.READ_ONLY;
import static org.neo4j.graphdb.QueryExecutionType.QueryType.READ_WRITE;
import static org.neo4j.graphdb.QueryExecutionType.QueryType.WRITE;


public class TriggerNewProcedures {
Expand Down Expand Up @@ -54,7 +57,8 @@ public Stream<TriggerInfo> install(@Name("databaseName") String databaseName, @N

// TODO - to be deleted in 5.x, because in a cluster, not all DBMS host all the databases on them,
// so we have to assume that the leader of the system database doesn't have access to this user database
Util.validateQuery(ApocConfig.apocConfig().getDatabase(databaseName), statement);
Util.validateQuery(ApocConfig.apocConfig().getDatabase(databaseName), statement,
READ_ONLY, WRITE, READ_WRITE);

Map<String,Object> params = (Map)config.getOrDefault("params", Collections.emptyMap());
TriggerInfo removed = TriggerHandlerNewProcedures.install(databaseName, name, statement, selector, params);
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/java/apoc/trigger/TriggerNewProceduresTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,18 @@ public void testEventualConsistency() {
map("name", UUID.randomUUID().toString()) );
}

@Test
public void testTriggerInstallWithSchemaOperation() {
String query = "CREATE INDEX periodicIdx FOR (n:Bar) ON (n.first_name, n.last_name)";
try {
testCall(sysDb, "CALL apoc.trigger.install('neo4j', 'triggerSchema', 'CREATE INDEX periodicIdx FOR (n:Bar) ON (n.first_name, n.last_name)', {phase: 'before'})",
Map.of("query", query),
(row) -> fail("Should fail because of unsupported schema operation"));
} catch (RuntimeException e) {
final String expected = "Failed to invoke procedure `apoc.trigger.install`: " +
"Caused by: java.lang.RuntimeException: Supported query types for the operation are [READ_ONLY, WRITE, READ_WRITE]";
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there anyway that this could be a more user friendly error message?

"The trigger statement must contain READ_ONLY, WRITE, or READ_WRITE query."

assertEquals(expected, e.getMessage());
}
}

}