You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2024/04/10 09:13:39 UTC
(shardingsphere) branch master updated: Fix doc of export & import database configuration (#30828)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 499e8870402 Fix doc of export & import database configuration (#30828)
499e8870402 is described below
commit 499e8870402c39cfd2a1e31decdd0f737c538ed5
Author: Raigor <ra...@gmail.com>
AuthorDate: Wed Apr 10 17:13:31 2024 +0800
Fix doc of export & import database configuration (#30828)
* Fix doc of export & import database configuration
* Fix LoadSingleTableExecutorTest
* Fix ImportDatabaseConfigurationExecutorTest
* Fix ImportMetaDataExecutorTest
* Remove useless test yaml
---
.../syntax/ral/export-database-configuration.cn.md | 76 +++++-----------------
.../syntax/ral/export-database-configuration.en.md | 74 +++++----------------
.../syntax/ral/import-database-configuration.cn.md | 18 ++---
.../syntax/ral/import-database-configuration.en.md | 18 ++---
.../syntax/ral/show-rules-used-storage-unit.cn.md | 2 +-
.../syntax/ral/show-rules-used-storage-unit.en.md | 2 +-
.../update/LoadSingleTableExecutorTest.java | 3 +
.../ExportDatabaseConfigurationExecutor.java | 2 +-
.../YamlDatabaseConfigurationImportExecutor.java | 29 ++++-----
.../ImportDatabaseConfigurationExecutorTest.java | 12 +---
.../ral/updatable/ImportMetaDataExecutorTest.java | 18 ++---
.../conf/import/database-empty-data-source.yaml | 18 -----
12 files changed, 74 insertions(+), 198 deletions(-)
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/export-database-configuration.cn.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/export-database-configuration.cn.md
index 04e839009a2..84f5dc5e851 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/export-database-configuration.cn.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/export-database-configuration.cn.md
@@ -1,11 +1,11 @@
+++
title = "EXPORT DATABASE CONFIGURATION"
-weight = 12
+weight = 13
+++
### 描述
-`EXPORT DATABASE CONFIGURATION` 语法用于将 `database` 中的存储单元和规则配置导出为 `YAML` 格式。
+`EXPORT DATABASE CONFIGURATION` 语法用于将逻辑库中的存储单元和规则配置导出为 `YAML` 格式。
### 语法
@@ -29,26 +29,16 @@ filePath ::=
### 补充说明
-- 未指定 `databaseName` 时,默认是当前使用的 `DATABASE。` 如果也未使用 `DATABASE` 则会提示 `No database selected`。
-
-- 未指定 `filePath` 时,会将存储单元和规则配置导出至屏幕。
+- 未指定 `databaseName` 时,导出当前使用的逻辑库;如果也未使用逻辑库则提示 `No database selected`;
+- 未指定 `filePath` 时,会将导出的信息通过结果集输出;
+- 指定 `filePath` 时,会自动创建文件,若文件已存在,会被覆盖。
### 示例
-- 导出指定逻辑库的存储单元和规则配置到指定路径
-
-```sql
-EXPORT DATABASE CONFIGURATION FROM sharding_db TO FILE "/xxx/config_sharding_db.yaml";
-```
-
-- 导出指定逻辑库的存储单元和规则配置到屏幕
+- 导出当前逻辑库的配置信息
```sql
-EXPORT DATABASE CONFIGURATION FROM sharding_db;
-```
-
-```sql
-mysql> EXPORT DATABASE CONFIGURATION FROM sharding_db;
+mysql> EXPORT DATABASE CONFIGURATION;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
| result [...]
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
@@ -56,7 +46,7 @@ mysql> EXPORT DATABASE CONFIGURATION FROM sharding_db;
dataSources:
ds_1:
password: 123456
- url: jdbc:mysql://127.0.0.1:3306/migration_ds_0
+ url: jdbc:mysql://127.0.0.1:3306/db0
username: root
minPoolSize: 1
connectionTimeoutMilliseconds: 30000
@@ -77,53 +67,21 @@ dataSources:
rules:
|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
-1 row in set (0.00 sec)
-```
-
-- 导出当前逻辑库的存储单元和规则配置到指定路径
-
-```sql
-EXPORT DATABASE CONFIGURATION TO FILE "/xxx/config_sharding_db.yaml";
+1 row in set (0.01 sec)
```
-- 导出当前逻辑库的存储单元和规则配置到屏幕
+- 导出指定逻辑库的配置信息,并输出到文件
```sql
-EXPORT DATABASE CONFIGURATION;
+mysql> EXPORT DATABASE CONFIGURATION FROM sharding_db TO FILE '/xxx/config_sharding_db.yaml';
++-------------------------------------------------------------------------+
+| result |
++-------------------------------------------------------------------------+
+| Successfully exported to: '/xxx/config_sharding_db.yaml' |
++-------------------------------------------------------------------------+
+1 row in set (0.02 sec)
```
-```sql
-mysql> EXPORT DATABASE CONFIGURATION;
-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
-| result [...]
-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
-| databaseName: sharding_db
-dataSources:
- ds_1:
- password: 123456
- url: jdbc:mysql://127.0.0.1:3306/migration_ds_0
- username: root
- minPoolSize: 1
- connectionTimeoutMilliseconds: 30000
- maxLifetimeMilliseconds: 2100000
- readOnly: false
- idleTimeoutMilliseconds: 60000
- maxPoolSize: 50
- ds_2:
- password: 123456
- url: jdbc:mysql://127.0.0.1:3306/db1
- username: root
- minPoolSize: 1
- connectionTimeoutMilliseconds: 30000
- maxLifetimeMilliseconds: 2100000
- readOnly: false
- idleTimeoutMilliseconds: 60000
- maxPoolSize: 50
-rules:
- |
-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
-1 row in set (0.00 sec)
-```
### 保留字
`EXPORT`、`DATABASE`、`CONFIGURATION`、`FROM`、`TO`、`FILE`
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/export-database-configuration.en.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/export-database-configuration.en.md
index e6a8a24ba7b..9d9ca48671b 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/export-database-configuration.en.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/export-database-configuration.en.md
@@ -1,6 +1,6 @@
+++
title = "EXPORT DATABASE CONFIGURATION"
-weight = 12
+weight = 13
+++
### Description
@@ -29,25 +29,16 @@ filePath ::=
### Supplement
-- When `databaseName` is not specified, the default is the currently used `DATABASE`. If `DATABASE` is not used, `No database selected` will be prompted.
+- When `databaseName` is not specified, the currently used logical database will be exported; if no database is used, `No database selected` will be prompted;
+- When `filePath` is not specified, the exported information will be output through the result set;
+- When `filePath` is specified, the file will be automatically created. If the file already exists, it will be overwritten.
-- When `filePath` is not specified, the storage units and rule configurations will export to screen.
### Example
-- Export storage units and rule configurations from specified database to specified file path
+- Export currently used logical database
```sql
-EXPORT DATABASE CONFIGURATION FROM sharding_db TO FILE "/xxx/config_sharding_db.yaml";
-```
-
-- Export storage units and rule configurations from specified database to screen
-
-```sql
-EXPORT DATABASE CONFIGURATION FROM sharding_db;
-```
-
-```sql
-mysql> EXPORT DATABASE CONFIGURATION FROM sharding_db;
+mysql> EXPORT DATABASE CONFIGURATION;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
| result [...]
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
@@ -55,7 +46,7 @@ mysql> EXPORT DATABASE CONFIGURATION FROM sharding_db;
dataSources:
ds_1:
password: 123456
- url: jdbc:mysql://127.0.0.1:3306/migration_ds_0
+ url: jdbc:mysql://127.0.0.1:3306/db0
username: root
minPoolSize: 1
connectionTimeoutMilliseconds: 30000
@@ -76,52 +67,19 @@ dataSources:
rules:
|
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
-1 row in set (0.00 sec)
-```
-
-- Export storage units and rule configurations from current database to specified file path
-
-```sql
-EXPORT DATABASE CONFIGURATION TO FILE "/xxx/config_sharding_db.yaml";
+1 row in set (0.01 sec)
```
-- Export storage units and rule configurations from current database to screen
-
-```sql
-EXPORT DATABASE CONFIGURATION;
-```
+- Export the specified logical database and output it to file
```sql
-mysql> EXPORT DATABASE CONFIGURATION;
-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
-| result [...]
-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
-| databaseName: sharding_db
-dataSources:
- ds_1:
- password: 123456
- url: jdbc:mysql://127.0.0.1:3306/migration_ds_0
- username: root
- minPoolSize: 1
- connectionTimeoutMilliseconds: 30000
- maxLifetimeMilliseconds: 2100000
- readOnly: false
- idleTimeoutMilliseconds: 60000
- maxPoolSize: 50
- ds_2:
- password: 123456
- url: jdbc:mysql://127.0.0.1:3306/db1
- username: root
- minPoolSize: 1
- connectionTimeoutMilliseconds: 30000
- maxLifetimeMilliseconds: 2100000
- readOnly: false
- idleTimeoutMilliseconds: 60000
- maxPoolSize: 50
-rules:
- |
-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...]
-1 row in set (0.00 sec)
+mysql> EXPORT DATABASE CONFIGURATION FROM sharding_db TO FILE '/xxx/config_sharding_db.yaml';
++-------------------------------------------------------------------------+
+| result |
++-------------------------------------------------------------------------+
+| Successfully exported to: '/xxx/config_sharding_db.yaml' |
++-------------------------------------------------------------------------+
+1 row in set (0.02 sec)
```
### Reserved word
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/import-database-configuration.cn.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/import-database-configuration.cn.md
index 4891cd59bd9..22716545d49 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/import-database-configuration.cn.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/import-database-configuration.cn.md
@@ -1,11 +1,11 @@
+++
title = "IMPORT DATABASE CONFIGURATION"
-weight = 13
+weight = 14
+++
### 描述
-`IMPORT DATABASE CONFIGURATION` 语法用于将 `YAML` 中的配置导入到指定逻辑库中。
+`IMPORT DATABASE CONFIGURATION` 语法用于从 `YAML` 中的配置导入逻辑库。
### 语法
@@ -26,20 +26,12 @@ filePath ::=
### 补充说明
-- 未指定 `databaseName` 时,默认是当前使用的 `DATABASE` 如果也未使用 `DATABASE` 则会提示 `No database selected`。
-
-- `IMPORT DATABASE CONFIGURATION` 语法仅支持对空逻辑库进行导入操作。
+- 当元数据中已存在同名逻辑库时,无法导入;
+- 当 YAML 中 `databaseName` 为空时,无法导入;
+- 当 YAML 中 `dataSources` 为空时,只导入空的逻辑库。
### 示例
-- 将 `YAML` 中的配置导入到指定逻辑库中
-
-```sql
-IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml";
-```
-
-- 将 `YAML` 中的配置导入到当前逻辑库中
-
```sql
IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml";
```
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/import-database-configuration.en.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/import-database-configuration.en.md
index 985b53d8790..57c4da33f70 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/import-database-configuration.en.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/import-database-configuration.en.md
@@ -1,11 +1,11 @@
+++
title = "IMPORT DATABASE CONFIGURATION"
-weight = 13
+weight = 14
+++
### Description
-The `IMPORT DATABASE CONFIGURATION` syntax is used to import `YAML` configuration to specified database.
+The `IMPORT DATABASE CONFIGURATION` syntax is used to import a database from the configuration in `YAML`.
### Syntax
@@ -26,20 +26,12 @@ filePath ::=
### Supplement
-- When `databaseName` is not specified, the default is the currently used `DATABASE`. If `DATABASE` is not used, `No database selected` will be prompted.
-
-- The `IMPORT DATABASE CONFIGURATION` syntax only supports import operations on empty database.
+- When a database with the same name already exists in the metadata, it cannot be imported;
+- When `databaseName` in YAML is empty, it cannot be imported;
+- When `dataSources` in YAML is empty, only empty database will be imported.
### Example
-- Import the configuration in `YAML` into the specified database
-
-```sql
-IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml";
-```
-
-- Import the configuration in `YAML` into the current database
-
```sql
IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml";
```
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/show-rules-used-storage-unit.cn.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/show-rules-used-storage-unit.cn.md
index 70c71a0b8bd..90927948332 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/show-rules-used-storage-unit.cn.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/show-rules-used-storage-unit.cn.md
@@ -1,6 +1,6 @@
+++
title = "SHOW RULES USED STORAGE UNIT"
-weight = 14
+weight = 12
+++
### 描述
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/show-rules-used-storage-unit.en.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/show-rules-used-storage-unit.en.md
index 5ffe8b2c074..5e577881eb2 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/show-rules-used-storage-unit.en.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/ral/show-rules-used-storage-unit.en.md
@@ -1,6 +1,6 @@
+++
title = "SHOW RULES USED STORAGE UNIT"
-weight = 14
+weight = 12
+++
### Description
diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java
index cc0e9b0ae37..d1f385935b7 100644
--- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java
+++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java
@@ -44,6 +44,7 @@ import java.util.LinkedList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -68,6 +69,8 @@ class LoadSingleTableExecutorTest {
void assertCheckWithDuplicatedTables() {
when(database.getName()).thenReturn("foo_db");
when(schema.containsTable("foo")).thenReturn(true);
+ when(database.getRuleMetaData().getAttributes(DataSourceMapperRuleAttribute.class)).thenReturn(Collections.emptyList());
+ when(database.getResourceMetaData().getNotExistedDataSources(any())).thenReturn(Collections.emptyList());
LoadSingleTableStatement sqlStatement = new LoadSingleTableStatement(Collections.singletonList(new SingleTableSegment("ds_0", null, "foo")));
executor.setDatabase(database);
assertThrows(TableExistsException.class, () -> executor.checkBeforeUpdate(sqlStatement));
diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutor.java
index b4c4de6434e..b95585bb757 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutor.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutor.java
@@ -50,7 +50,7 @@ public final class ExportDatabaseConfigurationExecutor implements DistSQLQueryEx
}
String filePath = sqlStatement.getFilePath().get();
ExportUtils.exportToFile(filePath, exportedData);
- return Collections.singleton(new LocalDataQueryResultRow(String.format("Successfully exported to:'%s'", filePath)));
+ return Collections.singleton(new LocalDataQueryResultRow(String.format("Successfully exported to: '%s'", filePath)));
}
@Override
diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
index 603b0ad91ba..841f1eaa5ec 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.util;
+import com.google.common.base.Strings;
import org.apache.shardingsphere.distsql.handler.validate.DistSQLDataSourcePoolPropertiesValidator;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationCheckEngine;
@@ -28,8 +29,8 @@ import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourceP
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.core.external.sql.ShardingSphereSQLException;
-import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
-import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.EmptyStorageUnitException;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.DatabaseCreateExistsException;
+import org.apache.shardingsphere.infra.exception.kernel.metadata.MissingRequiredDatabaseException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsOperateException;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -47,7 +48,6 @@ import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceCo
import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
import org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyDataSourceConfigurationSwapper;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.infra.exception.kernel.metadata.MissingRequiredDatabaseException;
import javax.sql.DataSource;
import java.sql.SQLException;
@@ -78,11 +78,13 @@ public final class YamlDatabaseConfigurationImportExecutor {
public void importDatabaseConfiguration(final YamlProxyDatabaseConfiguration yamlConfig) {
String databaseName = yamlConfig.getDatabaseName();
checkDatabase(databaseName);
- checkDataSources(databaseName, yamlConfig.getDataSources());
addDatabase(databaseName);
- addDataSources(databaseName, yamlConfig.getDataSources());
+ if (null == yamlConfig.getDataSources() || yamlConfig.getDataSources().isEmpty()) {
+ return;
+ }
+ importDataSources(databaseName, yamlConfig.getDataSources());
try {
- addRules(databaseName, yamlConfig.getRules());
+ importRules(databaseName, yamlConfig.getRules());
} catch (final ShardingSphereSQLException ex) {
dropDatabase(databaseName);
throw ex;
@@ -90,15 +92,8 @@ public final class YamlDatabaseConfigurationImportExecutor {
}
private void checkDatabase(final String databaseName) {
- ShardingSpherePreconditions.checkNotNull(databaseName, MissingRequiredDatabaseException::new);
- if (ProxyContext.getInstance().databaseExists(databaseName)) {
- ShardingSpherePreconditions.checkState(ProxyContext.getInstance().getContextManager().getDatabase(databaseName).getResourceMetaData().getStorageUnits().isEmpty(),
- () -> new UnsupportedSQLOperationException(String.format("Database `%s` exists and is not empty,overwrite is not supported", databaseName)));
- }
- }
-
- private void checkDataSources(final String databaseName, final Map<String, YamlProxyDataSourceConfiguration> dataSources) {
- ShardingSpherePreconditions.checkState(!dataSources.isEmpty(), () -> new EmptyStorageUnitException(databaseName));
+ ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(databaseName), MissingRequiredDatabaseException::new);
+ ShardingSpherePreconditions.checkState(!ProxyContext.getInstance().databaseExists(databaseName), () -> new DatabaseCreateExistsException(databaseName));
}
private void addDatabase(final String databaseName) {
@@ -108,7 +103,7 @@ public final class YamlDatabaseConfigurationImportExecutor {
contextManager.getMetaDataContexts().getMetaData().addDatabase(databaseName, protocolType, contextManager.getMetaDataContexts().getMetaData().getProps());
}
- private void addDataSources(final String databaseName, final Map<String, YamlProxyDataSourceConfiguration> yamlDataSourceMap) {
+ private void importDataSources(final String databaseName, final Map<String, YamlProxyDataSourceConfiguration> yamlDataSourceMap) {
Map<String, DataSourcePoolProperties> propsMap = new LinkedHashMap<>(yamlDataSourceMap.size(), 1F);
for (Entry<String, YamlProxyDataSourceConfiguration> entry : yamlDataSourceMap.entrySet()) {
DataSourceConfiguration dataSourceConfig = dataSourceConfigSwapper.swap(entry.getValue());
@@ -128,7 +123,7 @@ public final class YamlDatabaseConfigurationImportExecutor {
}
}
- private void addRules(final String databaseName, final Collection<YamlRuleConfiguration> yamlRuleConfigs) {
+ private void importRules(final String databaseName, final Collection<YamlRuleConfiguration> yamlRuleConfigs) {
if (null == yamlRuleConfigs || yamlRuleConfigs.isEmpty()) {
return;
}
diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationExecutorTest.java
index d59482d0f36..04bf0fee7e6 100644
--- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationExecutorTest.java
+++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationExecutorTest.java
@@ -24,8 +24,8 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
-import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.EmptyStorageUnitException;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.DatabaseCreateExistsException;
+import org.apache.shardingsphere.infra.exception.kernel.metadata.MissingRequiredDatabaseException;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.DuplicateRuleException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
@@ -36,7 +36,6 @@ import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundExce
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.infra.exception.kernel.metadata.MissingRequiredDatabaseException;
import org.apache.shardingsphere.proxy.backend.util.YamlDatabaseConfigurationImportExecutor;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDriver;
@@ -102,7 +101,7 @@ class ImportDatabaseConfigurationExecutorTest {
void assertImportExistedDatabase() {
String databaseName = "sharding_db";
when(ProxyContext.getInstance().databaseExists(databaseName)).thenReturn(true);
- assertThrows(UnsupportedSQLOperationException.class, () -> assertExecute(databaseName, "/conf/import/database-sharding.yaml"));
+ assertThrows(DatabaseCreateExistsException.class, () -> assertExecute(databaseName, "/conf/import/database-sharding.yaml"));
}
@Test
@@ -110,11 +109,6 @@ class ImportDatabaseConfigurationExecutorTest {
assertThrows(MissingRequiredDatabaseException.class, () -> assertExecute("sharding_db", "/conf/import/database-empty-database-name.yaml"));
}
- @Test
- void assertImportEmptyDataSource() {
- assertThrows(EmptyStorageUnitException.class, () -> assertExecute("sharding_db", "/conf/import/database-empty-data-source.yaml"));
- }
-
@Test
void assertImportDuplicatedLogicTable() {
assertThrows(DuplicateRuleException.class, () -> assertExecute("sharding_db", "/conf/import/database-duplicated-logic-table.yaml"));
diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java
index 3e9a7455c34..2d1218cd81e 100644
--- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java
+++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java
@@ -22,8 +22,7 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
-import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
-import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.EmptyStorageUnitException;
+import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.DatabaseCreateExistsException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
@@ -44,6 +43,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
+import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -51,6 +51,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
@@ -77,25 +78,26 @@ class ImportMetaDataExecutorTest {
}
@Test
- void assertCheckImportEmptyMetaData() {
+ void assertImportEmptyMetaData() throws SQLException {
init(null);
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- assertThrows(EmptyStorageUnitException.class, () -> executor.executeUpdate(
- new ImportMetaDataStatement(null, Objects.requireNonNull(ImportMetaDataExecutorTest.class.getResource(featureMap.get(EMPTY))).getPath()), contextManager));
+ executor.executeUpdate(new ImportMetaDataStatement(null, Objects.requireNonNull(ImportMetaDataExecutorTest.class.getResource(featureMap.get(EMPTY))).getPath()), contextManager);
+ assertNotNull(contextManager.getDatabase("empty_metadata"));
}
@Test
- void assertImportMetaDataFromJsonValue() {
+ void assertImportMetaDataFromJsonValue() throws SQLException {
init(EMPTY);
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- assertThrows(NullPointerException.class, () -> executor.executeUpdate(new ImportMetaDataStatement(METADATA_VALUE, null), contextManager));
+ executor.executeUpdate(new ImportMetaDataStatement(METADATA_VALUE, null), contextManager);
+ assertNotNull(contextManager.getDatabase("sharding_db"));
}
@Test
void assertImportExistedMetaDataFromFile() {
init(EMPTY);
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- assertThrows(UnsupportedSQLOperationException.class, () -> executor.executeUpdate(
+ assertThrows(DatabaseCreateExistsException.class, () -> executor.executeUpdate(
new ImportMetaDataStatement(null, Objects.requireNonNull(ImportMetaDataExecutorTest.class.getResource(featureMap.get(EMPTY))).getPath()), contextManager));
}
diff --git a/proxy/backend/core/src/test/resources/conf/import/database-empty-data-source.yaml b/proxy/backend/core/src/test/resources/conf/import/database-empty-data-source.yaml
deleted file mode 100644
index 1b5e0296bdf..00000000000
--- a/proxy/backend/core/src/test/resources/conf/import/database-empty-data-source.yaml
+++ /dev/null
@@ -1,18 +0,0 @@
-#
-# 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.
-#
-
-databaseName: sharding_db