Skip to content

Commit

Permalink
Add DataSourcePreparer.isSupportIfNotExistsOnCreateSchema() (#29412)
Browse files Browse the repository at this point in the history
* Add DataSourcePreparer.isSupportIfNotExists()

* Add DataSourcePreparer.isSupportIfNotExistsOnCreateSchema()
  • Loading branch information
terrymanu authored Dec 15, 2023
1 parent dc41851 commit 6f0df83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -74,6 +62,11 @@ protected void executeTargetTableSQL(final Connection targetConnection, final St
}
}

@Override
public boolean isSupportIfNotExistsOnCreateSchema() {
return false;
}

@Override
public String getDatabaseType() {
return "openGauss";
Expand Down

0 comments on commit 6f0df83

Please sign in to comment.