This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 3983cc8cc8a Add TargetTableNotEmptyWhenPrepareMigrationException
(#20976)
3983cc8cc8a is described below
commit 3983cc8cc8ad415f6d0dcbc57894760369a8e539
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Sep 14 19:03:37 2022 +0800
Add TargetTableNotEmptyWhenPrepareMigrationException (#20976)
* Add TargetTableNotEmptyWhenPrepareMigrationException
* Add TargetTableNotEmptyWhenPrepareMigrationException
---
.../user-manual/error-code/sql-error-code.cn.md | 3 +-
.../user-manual/error-code/sql-error-code.en.md | 3 +-
.../datasource/AbstractDataSourceChecker.java | 8 +++---
...TableNotEmptyWhenPrepareMigrationException.java | 33 ++++++++++++++++++++++
.../impl/OracleDDLStatementSQLVisitor.java | 2 +-
.../datasource/AbstractDataSourceCheckerTest.java | 3 +-
6 files changed, 44 insertions(+), 8 deletions(-)
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index af6843f255e..aab47263026 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -95,7 +95,8 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| HY000 | 18080 | Can not find pipeline job \`%s\` |
| HY000 | 18081 | Job has already started |
| HY000 | 18082 | Sharding count of job \`%s\` is 0 |
-| HY000 | 18083 | Can not split range for table \`%s\`, reason: %s" |
+| HY000 | 18083 | Can not split range for table \`%s\`, reason: %s |
+| HY000 | 18084 | Target table \`%s\` is not empty |
| HY000 | 18090 | Importer job write data failed |
| HY000 | 18091 | Can not poll event because of binlog sync channel
already closed |
| HY000 | 18092 | Task \`%s\` execute failed |
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md
b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index ee600f344e7..2f8c5384817 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -95,7 +95,8 @@ SQL error codes provide by standard `SQL State`, `Vendor
Code` and `Reason`, whi
| HY000 | 18080 | Can not find pipeline job \`%s\` |
| HY000 | 18081 | Job has already started |
| HY000 | 18082 | Sharding count of job \`%s\` is 0 |
-| HY000 | 18083 | Can not split range for table \`%s\`, reason: %s" |
+| HY000 | 18083 | Can not split range for table \`%s\`, reason: %s |
+| HY000 | 18084 | Target table \`%s\` is not empty |
| HY000 | 18090 | Importer job write data failed |
| HY000 | 18091 | Can not poll event because of binlog sync channel
already closed |
| HY000 | 18092 | Task \`%s\` execute failed |
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/datasource/AbstractDataSourceChecker.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/datasource/AbstractDataSourceChecker.java
index dd23d918592..da7c6977e3f 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/datasource/AbstractDataSourceChecker.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/datasource/AbstractDataSourceChecker.java
@@ -20,9 +20,11 @@ package
org.apache.shardingsphere.data.pipeline.core.check.datasource;
import lombok.extern.slf4j.Slf4j;
import
org.apache.shardingsphere.data.pipeline.api.config.TableNameSchemaNameMapping;
import
org.apache.shardingsphere.data.pipeline.core.exception.job.PipelineJobPrepareFailedException;
+import
org.apache.shardingsphere.data.pipeline.core.exception.job.TargetTableNotEmptyWhenPrepareMigrationException;
import
org.apache.shardingsphere.data.pipeline.core.sqlbuilder.PipelineSQLBuilderFactory;
import
org.apache.shardingsphere.data.pipeline.spi.check.datasource.DataSourceChecker;
import
org.apache.shardingsphere.data.pipeline.spi.sqlbuilder.PipelineSQLBuilder;
+import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -62,14 +64,12 @@ public abstract class AbstractDataSourceChecker implements
DataSourceChecker {
private void checkEmpty(final DataSource dataSource, final
TableNameSchemaNameMapping tableNameSchemaNameMapping, final Collection<String>
logicTableNames) throws SQLException {
for (String each : logicTableNames) {
String sql =
getSQLBuilder().buildCheckEmptySQL(tableNameSchemaNameMapping.getSchemaName(each),
each);
- log.info("checkEmpty, sql={}", sql);
+ log.info("Check whether table is empty, SQL: {}", sql);
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement =
connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery()) {
- if (resultSet.next()) {
- throw new
PipelineJobPrepareFailedException(String.format("Target table `%s` is not
empty, sql: %s.", each, sql));
- }
+ ShardingSpherePreconditions.checkState(!resultSet.next(), new
TargetTableNotEmptyWhenPrepareMigrationException(each));
}
}
}
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/TargetTableNotEmptyWhenPrepareMigrationException.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/TargetTableNotEmptyWhenPrepareMigrationException.java
new file mode 100644
index 00000000000..e4cd1cea989
--- /dev/null
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/TargetTableNotEmptyWhenPrepareMigrationException.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.exception.job;
+
+import
org.apache.shardingsphere.data.pipeline.core.exception.PipelineSQLException;
+import
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+
+/**
+ * Target table not empty when prepare migration exception.
+ */
+public final class TargetTableNotEmptyWhenPrepareMigrationException extends
PipelineSQLException {
+
+ private static final long serialVersionUID = -8462039913248251254L;
+
+ public TargetTableNotEmptyWhenPrepareMigrationException(final String
tableName) {
+ super(XOpenSQLState.GENERAL_ERROR, 84, "Target table `%s` is not empty
before migration", tableName);
+ }
+}
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDDLStatementSQLVisitor.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDDLStatementSQLVisitor.java
index e1dded98060..69a88304981 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDDLStatementSQLVisitor.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDDLStatementSQLVisitor.java
@@ -999,7 +999,7 @@ public final class OracleDDLStatementSQLVisitor extends
OracleStatementSQLVisito
public ASTNode visitDropProcedure(final DropProcedureContext ctx) {
return new OracleDropProcedureStatement();
}
-
+
@Override
public ASTNode visitDropIndexType(final DropIndexTypeContext ctx) {
return new OracleDropIndexTypeStatement();
diff --git
a/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/check/datasource/AbstractDataSourceCheckerTest.java
b/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/check/datasource/AbstractDataSourceCheckerTest.java
index 87ee746fc59..c8cdaa03f72 100644
---
a/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/check/datasource/AbstractDataSourceCheckerTest.java
+++
b/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/check/datasource/AbstractDataSourceCheckerTest.java
@@ -20,6 +20,7 @@ package
org.apache.shardingsphere.data.pipeline.core.check.datasource;
import lombok.SneakyThrows;
import
org.apache.shardingsphere.data.pipeline.api.config.TableNameSchemaNameMapping;
import
org.apache.shardingsphere.data.pipeline.core.exception.job.PipelineJobPrepareFailedException;
+import
org.apache.shardingsphere.data.pipeline.core.exception.job.TargetTableNotEmptyWhenPrepareMigrationException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -100,7 +101,7 @@ public final class AbstractDataSourceCheckerTest {
dataSourceChecker.checkTargetTable(dataSources, new
TableNameSchemaNameMapping(Collections.emptyMap()),
Collections.singletonList("t_order"));
}
- @Test(expected = PipelineJobPrepareFailedException.class)
+ @Test(expected = TargetTableNotEmptyWhenPrepareMigrationException.class)
public void assertCheckTargetTableFailed() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(connection.prepareStatement("SELECT * FROM t_order LIMIT
1")).thenReturn(preparedStatement);