Skip to content

Commit

Permalink
feat: support drop cascade (#841)
Browse files Browse the repository at this point in the history
* feat: support drop cascade

* test: start adding tests

* fix: drop table should not remove foreign keys by default

* test: add integration test

* chore: address review comments
  • Loading branch information
olavloite committed Oct 14, 2023
1 parent e2e326b commit 0ffa659
Show file tree
Hide file tree
Showing 7 changed files with 1,470 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
public class PGExceptionFactory {
private static final Pattern RELATION_NOT_FOUND_PATTERN =
Pattern.compile("relation .+ does not exist");
private static final Pattern CANNOT_DROP_TABLE_WITH_INDICES_PATTERN =
Pattern.compile("Cannot drop table test with indices");
private static final Pattern ONLY_RESTRICT_BEHAVIOR =
Pattern.compile("Only <RESTRICT> behavior is supported by <DROP> statement\\.");

private PGExceptionFactory() {}

Expand Down Expand Up @@ -86,6 +90,20 @@ public static PGException toPGException(SpannerException spannerException) {
return PGException.newBuilder(extractMessage(spannerException))
.setSQLState(SQLState.UndefinedTable)
.build();
} else if (spannerException.getErrorCode() == ErrorCode.FAILED_PRECONDITION
&& CANNOT_DROP_TABLE_WITH_INDICES_PATTERN.matcher(spannerException.getMessage()).find()) {
return PGException.newBuilder(extractMessage(spannerException))
.setSQLState(SQLState.FeatureNotSupported)
.setHints(
"Execute 'set spanner.support_drop_cascade=true' to enable dropping tables with indices")
.build();
} else if (spannerException.getErrorCode() == ErrorCode.INVALID_ARGUMENT
&& ONLY_RESTRICT_BEHAVIOR.matcher(spannerException.getMessage()).find()) {
return PGException.newBuilder(extractMessage(spannerException))
.setSQLState(SQLState.FeatureNotSupported)
.setHints(
"Execute 'set spanner.support_drop_cascade=true' to enable 'drop {table|schema} cascade' statements.")
.build();
}
return newPGException(extractMessage(spannerException));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,14 @@ public boolean isForceAutocommit() {
return getBoolSetting("spanner", "force_autocommit", false);
}

/**
* Returns whether DROP ... CASCADE statements are supported. This also includes implicit
* cascading operations, such as dropping indexes of tables that are being dropped.
*/
public boolean isSupportDropCascade() {
return getBoolSetting("spanner", "support_drop_cascade", false);
}

/**
* Returns whether statements with an OFFSET clause that uses a parameter should be automatically
* appended with a LIMIT clause. The LIMIT clause will use the literal Long.MAX_VALUE for unbound
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ void execute() {

private static final ImmutableMap<String, LocalStatement> EMPTY_LOCAL_STATEMENTS =
ImmutableMap.of();
private static final StatementResult NO_RESULT = new NoResult();
static final StatementResult NO_RESULT = new NoResult();
private static final StatementResult ROLLBACK_RESULT = new NoResult("ROLLBACK");
private static final Statement ROLLBACK = Statement.of("ROLLBACK");

Expand Down
Loading

0 comments on commit 0ffa659

Please sign in to comment.