diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/MetaDataLoader.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/MetaDataLoader.java index dd326098d6b15..10b8bb73bd822 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/MetaDataLoader.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/MetaDataLoader.java @@ -95,7 +95,7 @@ private static Collection loadByDefault(final MetaDataLoaderMate for (String each : material.getActualTableNames()) { TableMetaDataLoader.load(material.getDataSource(), each, material.getStorageType()).ifPresent(tableMetaData::add); } - return Collections.singletonList(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaData)); + return Collections.singleton(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaData)); } private static void merge(final Map schemaMetaDataMap, final Collection addedSchemaMetaDataList) { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java index d861bdf34d5dd..99f8d4309d056 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java @@ -130,12 +130,10 @@ public void reloadSchema(final ShardingSphereDatabase database, final String sch ShardingSphereSchema reloadedSchema = loadSchema(database, schemaName, dataSourceName); if (reloadedSchema.getTables().isEmpty()) { database.dropSchema(schemaName); - persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().dropSchema(database.getName(), - schemaName); + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().dropSchema(database.getName(), schemaName); } else { database.addSchema(schemaName, reloadedSchema); - persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService() - .compareAndPersist(database.getName(), schemaName, reloadedSchema); + persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().compareAndPersist(database.getName(), schemaName, reloadedSchema); } } catch (final SQLException ex) { log.error("Reload meta data of database: {} schema: {} with data source: {} failed", database.getName(), schemaName, dataSourceName, ex); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java index 5c0c1848653e5..b39cb56cc1186 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java @@ -74,8 +74,7 @@ public class MetaDataContextManager { private final RuleConfigurationPersistDecorateEngine ruleConfigPersistDecorateEngine; - public MetaDataContextManager(final AtomicReference metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, - final PersistRepository repository) { + public MetaDataContextManager(final AtomicReference metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository) { this.metaDataContexts = metaDataContexts; this.computeNodeInstanceContext = computeNodeInstanceContext; resourceSwitchManager = new ResourceSwitchManager(); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManager.java index 17742956a7e14..97b852601dc25 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManager.java @@ -55,7 +55,7 @@ public SchemaMetaDataManager(final AtomicReference metaDataCon /** * Add database. * - * @param databaseName database name + * @param databaseName to be added database name */ public synchronized void addDatabase(final String databaseName) { if (metaDataContexts.get().getMetaData().containsDatabase(databaseName)) { @@ -69,7 +69,7 @@ public synchronized void addDatabase(final String databaseName) { /** * Drop database. * - * @param databaseName database name + * @param databaseName to be dropped database name */ public synchronized void dropDatabase(final String databaseName) { if (!metaDataContexts.get().getMetaData().containsDatabase(databaseName)) { @@ -81,8 +81,8 @@ public synchronized void dropDatabase(final String databaseName) { /** * Add schema. * - * @param databaseName database name - * @param schemaName schema name + * @param databaseName to be added database name + * @param schemaName to be added schema name */ public synchronized void addSchema(final String databaseName, final String schemaName) { ShardingSphereMetaData metaData = metaDataContexts.get().getMetaData(); @@ -97,14 +97,11 @@ public synchronized void addSchema(final String databaseName, final String schem /** * Drop schema. * - * @param databaseName database name - * @param schemaName schema name + * @param databaseName to be dropped database name + * @param schemaName to be dropped schema name */ public synchronized void dropSchema(final String databaseName, final String schemaName) { ShardingSphereMetaData metaData = metaDataContexts.get().getMetaData(); - if (!metaData.containsDatabase(databaseName)) { - return; - } ShardingSphereDatabase database = metaData.getDatabase(databaseName); if (!database.containsSchema(schemaName)) { return; @@ -118,17 +115,17 @@ public synchronized void dropSchema(final String databaseName, final String sche * * @param databaseName database name * @param schemaName schema name - * @param toBeDeletedTableName to be deleted table name - * @param toBeDeletedViewName to be deleted view name + * @param toBeChangedTable to be changed table + * @param toBeChangedView to be changed view */ - public synchronized void alterSchema(final String databaseName, final String schemaName, final String toBeDeletedTableName, final String toBeDeletedViewName) { + public synchronized void alterSchema(final String databaseName, final String schemaName, final ShardingSphereTable toBeChangedTable, final ShardingSphereView toBeChangedView) { ShardingSphereMetaData metaData = metaDataContexts.get().getMetaData(); - if (!metaData.containsDatabase(databaseName) || !metaData.getDatabase(databaseName).containsSchema(schemaName)) { + if (!metaData.getDatabase(databaseName).containsSchema(schemaName)) { return; } - Optional.ofNullable(toBeDeletedTableName).ifPresent(optional -> dropTable(databaseName, schemaName, optional)); - Optional.ofNullable(toBeDeletedViewName).ifPresent(optional -> dropView(databaseName, schemaName, optional)); - if (!Strings.isNullOrEmpty(toBeDeletedTableName) || !Strings.isNullOrEmpty(toBeDeletedViewName)) { + Optional.ofNullable(toBeChangedTable).ifPresent(optional -> alterTable(databaseName, schemaName, optional)); + Optional.ofNullable(toBeChangedView).ifPresent(optional -> alterView(databaseName, schemaName, optional)); + if (null != toBeChangedTable || null != toBeChangedView) { metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); } } @@ -138,33 +135,21 @@ public synchronized void alterSchema(final String databaseName, final String sch * * @param databaseName database name * @param schemaName schema name - * @param toBeChangedTable to be changed table - * @param toBeChangedView to be changed view + * @param toBeDeletedTableName to be deleted table name + * @param toBeDeletedViewName to be deleted view name */ - public synchronized void alterSchema(final String databaseName, final String schemaName, final ShardingSphereTable toBeChangedTable, final ShardingSphereView toBeChangedView) { + public synchronized void alterSchema(final String databaseName, final String schemaName, final String toBeDeletedTableName, final String toBeDeletedViewName) { ShardingSphereMetaData metaData = metaDataContexts.get().getMetaData(); - if (!metaData.containsDatabase(databaseName) || !metaData.getDatabase(databaseName).containsSchema(schemaName)) { + if (!metaData.getDatabase(databaseName).containsSchema(schemaName)) { return; } - Optional.ofNullable(toBeChangedTable).ifPresent(optional -> alterTable(databaseName, schemaName, optional)); - Optional.ofNullable(toBeChangedView).ifPresent(optional -> alterView(databaseName, schemaName, optional)); - if (null != toBeChangedTable || null != toBeChangedView) { + Optional.ofNullable(toBeDeletedTableName).ifPresent(optional -> dropTable(databaseName, schemaName, optional)); + Optional.ofNullable(toBeDeletedViewName).ifPresent(optional -> dropView(databaseName, schemaName, optional)); + if (!Strings.isNullOrEmpty(toBeDeletedTableName) || !Strings.isNullOrEmpty(toBeDeletedViewName)) { metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED)); } } - private void dropTable(final String databaseName, final String schemaName, final String toBeDeletedTableName) { - metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchema(schemaName).removeTable(toBeDeletedTableName); - metaDataContexts.get().getMetaData().getDatabase(databaseName) - .getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.remove(schemaName, toBeDeletedTableName)); - } - - private void dropView(final String databaseName, final String schemaName, final String toBeDeletedViewName) { - metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchema(schemaName).removeView(toBeDeletedViewName); - metaDataContexts.get().getMetaData().getDatabase(databaseName) - .getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.remove(schemaName, toBeDeletedViewName)); - } - private void alterTable(final String databaseName, final String schemaName, final ShardingSphereTable beBoChangedTable) { ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName); if (TableRefreshUtils.isSingleTable(beBoChangedTable.getName(), database)) { @@ -180,4 +165,16 @@ private void alterView(final String databaseName, final String schemaName, final } database.getSchema(schemaName).putView(beBoChangedView.getName(), beBoChangedView); } + + private void dropTable(final String databaseName, final String schemaName, final String toBeDeletedTableName) { + metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchema(schemaName).removeTable(toBeDeletedTableName); + metaDataContexts.get().getMetaData().getDatabase(databaseName) + .getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.remove(schemaName, toBeDeletedTableName)); + } + + private void dropView(final String databaseName, final String schemaName, final String toBeDeletedViewName) { + metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchema(schemaName).removeView(toBeDeletedViewName); + metaDataContexts.get().getMetaData().getDatabase(databaseName) + .getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.remove(schemaName, toBeDeletedViewName)); + } } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index 530190f0851c8..cc137afb4806c 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -34,10 +34,7 @@ import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute; @@ -46,8 +43,6 @@ import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.apache.shardingsphere.test.util.PropertiesBuilder; -import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -57,7 +52,6 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import java.sql.Types; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -66,16 +60,11 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -94,7 +83,6 @@ void setUp() { ShardingSphereDatabase database = mockDatabase(); when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true); when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(database); - when(metaDataContexts.getMetaData().getDatabase("foo_db").containsSchema("foo_schema")).thenReturn(true); when(metaDataContexts.getMetaData().getDatabases().values()).thenReturn(Collections.singleton(database)); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getInstance()).thenReturn(new ComputeNodeInstance(new ProxyInstanceMetaData("foo_id", 3307), Collections.emptyList())); @@ -106,11 +94,12 @@ private ShardingSphereDatabase mockDatabase() { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(result.getName()).thenReturn("foo_db"); when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - ShardingSphereRule rule = mock(ShardingSphereRule.class); MutableDataNodeRuleAttribute ruleAttribute = mock(MutableDataNodeRuleAttribute.class); when(ruleAttribute.findTableDataNode("foo_schema", "foo_tbl")).thenReturn(Optional.of(mock(DataNode.class))); + ShardingSphereRule rule = mock(ShardingSphereRule.class); when(rule.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute)); when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + when(result.containsSchema("foo_schema")).thenReturn(true); when(result.getSchemas()).thenReturn(new HashMap<>(Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME)))); StorageUnit storageUnit = mock(StorageUnit.class, RETURNS_DEEP_STUBS); when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); @@ -122,16 +111,6 @@ private ShardingSphereDatabase mockDatabase() { return result; } - @Test - void assertGetDataSourceMap() { - ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("foo_ds", new MockedDataSource())); - ShardingSphereDatabase database = - new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, mock(RuleMetaData.class), Collections.emptyMap()); - when(metaDataContexts.getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(database); - when(metaDataContexts.getMetaData().containsDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(true); - assertThat(contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).size(), is(1)); - } - @Test void assertRenewMetaDataContexts() { MetaDataContexts contexts = mock(MetaDataContexts.class); @@ -140,88 +119,8 @@ void assertRenewMetaDataContexts() { } @Test - void assertAddDatabase() { - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addDatabase("new_db"); - verify(metaDataContexts.getMetaData()).addDatabase(eq("new_db"), any(DatabaseType.class), any(ConfigurationProperties.class)); - } - - @Test - void assertAddExistedDatabase() { - when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addDatabase("foo_db"); - verify(metaDataContexts.getMetaData(), times(0)).addDatabase(eq("foo_db"), any(DatabaseType.class), any(ConfigurationProperties.class)); - } - - @Test - void assertDropDatabase() { - when(metaDataContexts.getMetaData().getDatabase("foo_db").getName()).thenReturn("foo_db"); - when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().dropDatabase("foo_db"); - verify(metaDataContexts.getMetaData()).dropDatabase("foo_db"); - } - - @Test - void assertDropNotExistedDatabase() { - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().dropDatabase("not_existed_db"); - verify(metaDataContexts.getMetaData(), times(0)).dropDatabase("not_existed_db"); - } - - @Test - void assertAddSchema() { - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addSchema("foo_db", "bar_schema"); - verify(metaDataContexts.getMetaData().getDatabase("foo_db")).addSchema(anyString(), any(ShardingSphereSchema.class)); - } - - @Test - void assertAddExistedSchema() { - when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").containsSchema("foo_schema")).thenReturn(true); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addSchema("foo_db", "foo_schema"); - verify(metaDataContexts.getMetaData().getDatabase("foo_db"), times(0)).addSchema(anyString(), any(ShardingSphereSchema.class)); - } - - @Test - void assertAlterSchemaForTableAltered() { - ShardingSphereSchema toBeAlteredSchema = createToBeAlteredSchema(); - when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", toBeAlteredSchema)); - when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(toBeAlteredSchema); - ShardingSphereColumn toBeChangedColumn = new ShardingSphereColumn("foo_col", Types.VARCHAR, false, false, false, true, false, false); - ShardingSphereTable toBeChangedTable = new ShardingSphereTable("foo_tbl", Collections.singleton(toBeChangedColumn), Collections.emptyList(), Collections.emptyList()); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema("foo_db", "foo_schema", toBeChangedTable, null); - ShardingSphereTable table = contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getSchema("foo_schema").getTables().get("foo_tbl"); - assertThat(table.getColumnValues().size(), is(1)); - assertTrue(table.containsColumn("foo_col")); - } - - @Test - void assertAlterSchemaForViewAltered() { - ShardingSphereSchema toBeAlteredSchema = createToBeAlteredSchema(); - when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", toBeAlteredSchema)); - when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(toBeAlteredSchema); - ShardingSphereView toBeChangedView = new ShardingSphereView("foo_view", "select `foo_view`.`foo_view`.`id` AS `id` from `foo_view`.`foo_view`"); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema("foo_db", "foo_schema", null, toBeChangedView); - ShardingSphereView view = contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getSchema("foo_schema").getView("foo_view"); - assertThat(view.getName(), is("foo_view")); - assertThat(view.getViewDefinition(), is("select `foo_view`.`foo_view`.`id` AS `id` from `foo_view`.`foo_view`")); - } - - @Test - void assertAlterSchemaForTableDropped() { - when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", createToBeAlteredSchema())); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema("foo_db", "foo_schema", "foo_tbl", null); - assertFalse(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getSchema("foo_schema").getTables().containsKey("foo_tbl")); - } - - @Test - void assertAlterSchemaForViewDropped() { - when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", createToBeAlteredSchema())); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema("foo_db", "foo_schema", "foo_view", null); - assertFalse(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getSchema("foo_schema").getViews().containsKey("foo_view")); - } - - private ShardingSphereSchema createToBeAlteredSchema() { - ShardingSphereTable beforeChangedTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - ShardingSphereView beforeChangedView = new ShardingSphereView("foo_tbl", ""); - return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("foo_tbl", beforeChangedTable), Collections.singletonMap("foo_view", beforeChangedView)); + void assertGetDatabase() { + assertNotNull(contextManager.getDatabase("foo_db")); } @Test @@ -229,25 +128,18 @@ void assertGetDatabaseWithNull() { assertThrows(NoDatabaseSelectedException.class, () -> contextManager.getDatabase(null)); } - @Test - void assertGetDatabaseWithEmptyString() { - assertThrows(NoDatabaseSelectedException.class, () -> contextManager.getDatabase("")); - } - @Test void assertGetDatabaseWhenNotExisted() { assertThrows(UnknownDatabaseException.class, () -> contextManager.getDatabase("bar_db")); } @Test - void assertGetDatabase() { - assertNotNull(contextManager.getDatabase("foo_db")); - } - - @Test - void assertAlterProperties() { - contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterProperties(PropertiesBuilder.build(new Property("foo", "foo_value"))); - assertThat(contextManager.getMetaDataContexts().getMetaData().getProps().getProps().getProperty("foo"), is("foo_value")); + void assertGetStorageUnits() { + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("foo_ds", new MockedDataSource())); + ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, mock(RuleMetaData.class), Collections.emptyMap()); + when(metaDataContexts.getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(database); + when(metaDataContexts.getMetaData().containsDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(true); + assertThat(contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).size(), is(1)); } @Test diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManagerTest.java new file mode 100644 index 0000000000000..cd3eb5c1e7686 --- /dev/null +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/manager/SchemaMetaDataManagerTest.java @@ -0,0 +1,215 @@ +/* + * 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.mode.metadata.manager; + +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.spi.PersistRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.sql.Types; +import java.util.Collections; +import java.util.concurrent.atomic.AtomicReference; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class SchemaMetaDataManagerTest { + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private MetaDataContexts metaDataContexts; + + private SchemaMetaDataManager schemaMetaDataManager; + + @BeforeEach + void setUp() { + ShardingSphereDatabase database = mockDatabase(); + when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true); + when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(database); + when(metaDataContexts.getMetaData().getDatabases().values()).thenReturn(Collections.singleton(database)); + schemaMetaDataManager = new SchemaMetaDataManager(new AtomicReference<>(metaDataContexts), mock(PersistRepository.class)); + } + + private ShardingSphereDatabase mockDatabase() { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getName()).thenReturn("foo_db"); + when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(result.containsSchema("foo_schema")).thenReturn(true); + return result; + } + + @Test + void assertAddNotExistedDatabase() { + schemaMetaDataManager.addDatabase("new_db"); + verify(metaDataContexts.getMetaData()).addDatabase(eq("new_db"), any(DatabaseType.class), any(ConfigurationProperties.class)); + } + + @Test + void assertAddExistedDatabase() { + when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true); + schemaMetaDataManager.addDatabase("foo_db"); + verify(metaDataContexts.getMetaData(), times(0)).addDatabase(eq("foo_db"), any(DatabaseType.class), any(ConfigurationProperties.class)); + } + + @Test + void assertDropExistedDatabase() { + when(metaDataContexts.getMetaData().getDatabase("foo_db").getName()).thenReturn("foo_db"); + when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true); + schemaMetaDataManager.dropDatabase("foo_db"); + verify(metaDataContexts.getMetaData()).dropDatabase("foo_db"); + } + + @Test + void assertDropNotExistedDatabase() { + schemaMetaDataManager.dropDatabase("not_existed_db"); + verify(metaDataContexts.getMetaData(), times(0)).dropDatabase("not_existed_db"); + } + + @Test + void assertAddNotExistedSchema() { + schemaMetaDataManager.addSchema("foo_db", "bar_schema"); + verify(metaDataContexts.getMetaData().getDatabase("foo_db")).addSchema(anyString(), any(ShardingSphereSchema.class)); + } + + @Test + void assertAddExistedSchema() { + when(metaDataContexts.getMetaData().getDatabase("foo_db").containsSchema("foo_schema")).thenReturn(true); + schemaMetaDataManager.addSchema("foo_db", "foo_schema"); + verify(metaDataContexts.getMetaData().getDatabase("foo_db"), times(0)).addSchema(anyString(), any(ShardingSphereSchema.class)); + } + + @Test + void assertDropNotExistedSchema() { + schemaMetaDataManager.dropSchema("foo_db", "bar_schema"); + verify(metaDataContexts.getMetaData().getDatabase("foo_db"), times(0)).dropSchema(anyString()); + } + + @Test + void assertDropExistedSchema() { + schemaMetaDataManager.dropSchema("foo_db", "foo_schema"); + verify(metaDataContexts.getMetaData().getDatabase("foo_db")).dropSchema("foo_schema"); + } + + @Test + void assertAlterNotExistedSchema() { + ShardingSphereSchema toBeAlteredSchema = createToBeAlteredSchema(); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", toBeAlteredSchema)); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(toBeAlteredSchema); + schemaMetaDataManager.alterSchema("foo_db", "bar_schema", null, (ShardingSphereView) null); + verify(metaDataContexts.getMetaData().getDatabase("foo_db"), times(0)).getSchema(any()); + } + + @Test + void assertAlterSchemaForNothingAltered() { + ShardingSphereSchema toBeAlteredSchema = createToBeAlteredSchema(); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", toBeAlteredSchema)); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(toBeAlteredSchema); + schemaMetaDataManager.alterSchema("foo_db", "foo_schema", null, (ShardingSphereView) null); + verify(metaDataContexts.getMetaData().getDatabase("foo_db"), times(0)).getSchema(any()); + } + + @Test + void assertAlterSchemaForTableAltered() { + ShardingSphereSchema toBeAlteredSchema = createToBeAlteredSchema(); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", toBeAlteredSchema)); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(toBeAlteredSchema); + ShardingSphereColumn toBeChangedColumn = new ShardingSphereColumn("foo_col", Types.VARCHAR, false, false, false, true, false, false); + ShardingSphereTable toBeChangedTable = new ShardingSphereTable("foo_tbl", Collections.singleton(toBeChangedColumn), Collections.emptyList(), Collections.emptyList()); + schemaMetaDataManager.alterSchema("foo_db", "foo_schema", toBeChangedTable, null); + ShardingSphereTable table = metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").getTables().get("foo_tbl"); + assertThat(table.getColumnValues().size(), is(1)); + assertTrue(table.containsColumn("foo_col")); + } + + @Test + void assertAlterSchemaForViewAltered() { + ShardingSphereSchema toBeAlteredSchema = createToBeAlteredSchema(); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", toBeAlteredSchema)); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(toBeAlteredSchema); + ShardingSphereView toBeChangedView = new ShardingSphereView("foo_view", "select `foo_view`.`foo_view`.`id` AS `id` from `foo_view`.`foo_view`"); + schemaMetaDataManager.alterSchema("foo_db", "foo_schema", null, toBeChangedView); + ShardingSphereView view = metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").getView("foo_view"); + assertThat(view.getName(), is("foo_view")); + assertThat(view.getViewDefinition(), is("select `foo_view`.`foo_view`.`id` AS `id` from `foo_view`.`foo_view`")); + } + + @Test + void assertAlterNotExistedSchemaForTableDropped() { + ShardingSphereSchema toBeAlteredSchema = createToBeAlteredSchema(); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", toBeAlteredSchema)); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(toBeAlteredSchema); + schemaMetaDataManager.alterSchema("foo_db", "bar_schema", "", ""); + verify(metaDataContexts.getMetaData().getDatabase("foo_db"), times(0)).getSchema(any()); + } + + @Test + void assertAlterSchemaForNothingTableDropped() { + ShardingSphereSchema toBeAlteredSchema = createToBeAlteredSchema(); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", toBeAlteredSchema)); + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema")).thenReturn(toBeAlteredSchema); + schemaMetaDataManager.alterSchema("foo_db", "foo_schema", "", ""); + verify(metaDataContexts.getMetaData().getGlobalRuleMetaData(), times(0)).getRules(); + } + + @Test + void assertAlterSchemaForTableDropped() { + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", createToBeAlteredSchema())); + schemaMetaDataManager.alterSchema("foo_db", "foo_schema", "foo_tbl", null); + assertFalse(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").getTables().containsKey("foo_tbl")); + } + + @Test + void assertAlterSchemaForViewDropped() { + when(metaDataContexts.getMetaData().getDatabase("foo_db").getSchemas()).thenReturn(Collections.singletonMap("foo_schema", createToBeAlteredSchema())); + schemaMetaDataManager.alterSchema("foo_db", "foo_schema", "foo_view", null); + assertFalse(metaDataContexts.getMetaData().getDatabase("foo_db").getSchema("foo_schema").getViews().containsKey("foo_view")); + } + + private ShardingSphereSchema createToBeAlteredSchema() { + ShardingSphereTable beforeChangedTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); + ShardingSphereView beforeChangedView = new ShardingSphereView("foo_tbl", ""); + return new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("foo_tbl", beforeChangedTable), Collections.singletonMap("foo_view", beforeChangedView)); + } +}