diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/AbstractDataSourcePreparer.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/AbstractDataSourcePreparer.java index e4323adfe8f9b..9988ce6e3cabb 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/AbstractDataSourcePreparer.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/AbstractDataSourcePreparer.java @@ -49,7 +49,7 @@ public abstract class AbstractDataSourcePreparer implements DataSourcePreparer { private static final Pattern PATTERN_CREATE_TABLE = Pattern.compile("CREATE\\s+TABLE\\s+", Pattern.CASE_INSENSITIVE); @Override - public void prepareTargetSchemas(final PrepareTargetSchemasParameter param) throws SQLException { + public final void prepareTargetSchemas(final PrepareTargetSchemasParameter param) throws SQLException { DatabaseType targetDatabaseType = param.getTargetDatabaseType(); DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(targetDatabaseType).getDialectDatabaseMetaData(); if (!dialectDatabaseMetaData.isSchemaAvailable()) { @@ -77,6 +77,11 @@ private void executeCreateSchema(final PipelineDataSourceManager dataSourceManag Connection connection = dataSourceManager.getDataSource(targetDataSourceConfig).getConnection(); Statement statement = connection.createStatement()) { statement.execute(sql); + } catch (final SQLException ex) { + if (isSupportIfNotExistsOnCreateSchema()) { + throw ex; + } + log.warn("create schema failed", ex); } } diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/DataSourcePreparer.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/DataSourcePreparer.java index 3becc7f808950..3219b840767df 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/DataSourcePreparer.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/DataSourcePreparer.java @@ -45,4 +45,13 @@ public interface DataSourcePreparer extends DatabaseTypedSPI { * @throws SQLException SQL exception */ void prepareTargetTables(PrepareTargetTablesParameter param) throws SQLException; + + /** + * Is support if not exists on create schema SQL. + * + * @return supported or not + */ + default boolean isSupportIfNotExistsOnCreateSchema() { + return true; + } } diff --git a/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/prepare/datasource/OpenGaussDataSourcePreparer.java b/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/prepare/datasource/OpenGaussDataSourcePreparer.java index f1efbf108329f..eb0b467d5a71c 100644 --- a/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/prepare/datasource/OpenGaussDataSourcePreparer.java +++ b/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/prepare/datasource/OpenGaussDataSourcePreparer.java @@ -19,10 +19,9 @@ import com.google.common.base.Splitter; import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.data.pipeline.core.preparer.CreateTableConfiguration; import org.apache.shardingsphere.data.pipeline.core.datasource.PipelineDataSourceManager; +import org.apache.shardingsphere.data.pipeline.core.preparer.CreateTableConfiguration; import org.apache.shardingsphere.data.pipeline.core.preparer.datasource.AbstractDataSourcePreparer; -import org.apache.shardingsphere.data.pipeline.core.preparer.datasource.param.PrepareTargetSchemasParameter; import org.apache.shardingsphere.data.pipeline.core.preparer.datasource.param.PrepareTargetTablesParameter; import java.sql.Connection; @@ -36,17 +35,6 @@ public final class OpenGaussDataSourcePreparer extends AbstractDataSourcePrepare private static final String[] IGNORE_EXCEPTION_MESSAGE = {"multiple primary keys for table", "already exists"}; - @Override - public void prepareTargetSchemas(final PrepareTargetSchemasParameter param) { - try { - super.prepareTargetSchemas(param); - } catch (final SQLException ex) { - // openGauss CREATE SCHEMA doesn't support IF NOT EXISTS - // TODO Use actual data source to create schema, check whether schema exists or not - log.warn("create schema failed", ex); - } - } - @Override public void prepareTargetTables(final PrepareTargetTablesParameter param) throws SQLException { PipelineDataSourceManager dataSourceManager = param.getDataSourceManager(); @@ -74,6 +62,11 @@ protected void executeTargetTableSQL(final Connection targetConnection, final St } } + @Override + public boolean isSupportIfNotExistsOnCreateSchema() { + return false; + } + @Override public String getDatabaseType() { return "openGauss";