Skip to content

Commit

Permalink
next attempt.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-simons committed Nov 30, 2024
1 parent b2a02e1 commit b8b22e7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ final class DefaultCypherResource implements CypherResource {

private static final Pattern USE_DATABASE_PATTERN = Pattern.compile(USE_DATABASE_EXPRESSION);

private static final Pattern CALL_PATTERN = Pattern.compile("(?ims)(?<!`)([^`\\s*]\\s*+CALL\\s*+(?<importClause>[^{]++)?\\{.*}\\s*+(?<transactionClause>IN(?<concurrency>.+?)TRANSACTIONS)?)(?!`)");
private static final Pattern PATTERN_CALL_IMPORT_CLAUSE = Pattern.compile("\\(.+\\)");
private static final Pattern CALL_PATTERN = Pattern.compile("(?ims)(?<!`)([^`\\s*]\\s*+CALL\\s*+(?:\\(.+?\\)\\s*+)?\\{.*}\\s*+(?<transactionClause>IN(?<concurrency>.+?)TRANSACTIONS)?)(?!`)");
private static final Pattern PATTERN_CALL_CONCURRENCY = Pattern.compile("(?ims)(-\\d+|\\d+)?\\s*CONCURRENT");

private static final Pattern USING_PERIODIC_PATTERN = Pattern.compile("(?ims)(?<!`)(([^`\\s*]|^)\\s*+USING\\s+PERIODIC\\s+COMMIT\\s+)(?!`)");
Expand Down Expand Up @@ -359,16 +358,11 @@ static TransactionMode getTransactionMode(String statement) {
return TransactionMode.MANAGED;
}

var importClause = callMatcher.group("importClause");
var transactionClause = callMatcher.group("transactionClause");

if (importClause != null && !PATTERN_CALL_IMPORT_CLAUSE.matcher(importClause.trim()).matches()) {
throw new MigrationsException("Invalid statement: " + statement);
}

if (transactionClause == null) {
return TransactionMode.MANAGED;
}

var concurrency = callMatcher.group("concurrency");
if (concurrency.isBlank() || PATTERN_CALL_CONCURRENCY.matcher(concurrency.trim()).matches()) {
return TransactionMode.IMPLICIT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,13 @@ void shouldDetectImplicitTransactionNeeds(String query) {
"CREATE (a:`USING PERIODIC COMMIT `) RETURN a",
"CREATE (a:`CALL {WITH WHATEVER} IN TRANSACTIONS`) RETURN a",
"CREATE (a: ` CALL {WITH WHATEVER} IN TRANSACTIONS`) RETURN a",
"MATCH (n) CALL {blub}concurrent IN TRANSACTIONS RETURN n"
"MATCH (n) CALL {blub}concurrent IN TRANSACTIONS RETURN n",
"""
WITH apoc.date.currentTimestamp() AS start
CALL apoc.util.sleep(4000)
WITH start, apoc.date.currentTimestamp() AS end
RETURN datetime({epochmillis: start}) AS start, datetime({epochmillis: end}) AS end;
"""
})
void shouldDetectManagedTransactionNeeds(String query) {

Expand All @@ -376,7 +382,6 @@ void shouldDetectManagedTransactionNeeds(String query) {

@ParameterizedTest
@ValueSource(strings = {
"MATCH (n) CALL thisisInvalidCypherIKnow {blub} IN TRANSACTIONS RETURN n",
"MATCH (n) CALL {blub} IN concurrently TRANSACTIONS RETURN n"
})
void weEvenMightThrowWhenWeDoHalfwayParsingAnyway(String query) {
Expand Down

0 comments on commit b8b22e7

Please sign in to comment.