Skip to content

Commit

Permalink
Rename MetaDataManagerPersistService.alterSchema() (#34469)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Jan 24, 2025
1 parent fefcec5 commit 8ed79ed
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
alterSchemaMetaDataPOJO.getDroppedViews().add(viewName);
}
sqlStatement.getViewDefinition().ifPresent(optional -> alterSchemaMetaDataPOJO.getAlteredViews().add(new ShardingSphereView(viewName, optional)));
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
String viewName = TableRefreshUtils.getTableName(sqlStatement.getView().getTableName().getIdentifier(), sqlStatement.getDatabaseType());
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName);
alterSchemaMetaDataPOJO.getAlteredViews().add(new ShardingSphereView(viewName, sqlStatement.getViewDefinition()));
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
for (SimpleTableSegment each : sqlStatement.getViews()) {
alterSchemaMetaDataPOJO.getDroppedViews().add(each.getTableName().getIdentifier().getValue());
}
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
newTable.putIndex(new ShardingSphereIndex(renameIndexName, new LinkedList<>(), false));
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), actualSchemaName);
alterSchemaMetaDataPOJO.getAlteredTables().add(newTable);
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

private Optional<String> findLogicTableName(final ShardingSphereSchema schema, final String indexName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
newTable.putIndex(new ShardingSphereIndex(indexName, new LinkedList<>(), false));
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName);
alterSchemaMetaDataPOJO.getAlteredTables().add(newTable);
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
newTable.removeIndex(each.getIndexName().getIdentifier().getValue());
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), actualSchemaName);
alterSchemaMetaDataPOJO.getAlteredTables().add(newTable);
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
} else {
alterSchemaMetaDataPOJO.getAlteredTables().add(getTable(database, logicDataSourceNames, schemaName, tableName, props));
}
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

private ShardingSphereTable getTable(final ShardingSphereDatabase database, final Collection<String> logicDataSourceNames, final String schemaName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
alterSchemaMetaDataPOJO.getAlteredTables().add(getTable(database, logicDataSourceNames, schemaName,
TableRefreshUtils.getTableName(each.getRenameTable().getTableName().getIdentifier(), databaseType), props));
alterSchemaMetaDataPOJO.getDroppedTables().add(each.getTable().getTableName().getIdentifier().getValue());
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
alterSchemaMetaDataPOJO.getAlteredTables().add(schema.getTable(viewName));
alterSchemaMetaDataPOJO.getAlteredViews().add(schema.getView(viewName));
}
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

private ShardingSphereSchema getSchema(final ShardingSphereDatabase database, final Collection<String> logicDataSourceNames,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName, logicDataSourceNames);
alterSchemaMetaDataPOJO.getAlteredTables().add(actualTableMetaData.get());
alterSchemaMetaDataPOJO.getAlteredViews().add(new ShardingSphereView(viewName, sqlStatement.getViewDefinition()));
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void refresh(final MetaDataManagerPersistService metaDataManagerPersistSe
alterSchemaMetaDataPOJO.getDroppedTables().add(viewName);
alterSchemaMetaDataPOJO.getDroppedViews().add(viewName);
});
metaDataManagerPersistService.alterSchemaMetaData(alterSchemaMetaDataPOJO);
metaDataManagerPersistService.alterSchema(alterSchemaMetaDataPOJO);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public interface MetaDataManagerPersistService {
*/
void createSchema(String databaseName, String schemaName);

/**
* Alter schema.
*
* @param alterSchemaMetaDataPOJO alter schema metadata pojo
*/
void alterSchema(AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO);

/**
* Alter schema name.
*
Expand All @@ -73,13 +80,6 @@ public interface MetaDataManagerPersistService {
*/
void dropSchema(String databaseName, Collection<String> schemaNames);

/**
* Alter schema metadata.
*
* @param alterSchemaMetaDataPOJO alter schema metadata pojo
*/
void alterSchemaMetaData(AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO);

/**
* Register storage units.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ public void createSchema(final String databaseName, final String schemaName) {
metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, schemaName);
}

@Override
public void alterSchema(final AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO) {
String databaseName = alterSchemaMetaDataPOJO.getDatabaseName();
String schemaName = alterSchemaMetaDataPOJO.getSchemaName();
DatabaseMetaDataPersistFacade databaseMetaDataFacade = metaDataPersistService.getDatabaseMetaDataFacade();
databaseMetaDataFacade.getTable().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredTables());
databaseMetaDataFacade.getView().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredViews());
alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataFacade.getTable().drop(databaseName, schemaName, each));
alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataFacade.getView().delete(databaseName, schemaName, each));
}

@Override
public void alterSchemaName(final AlterSchemaPOJO alterSchemaPOJO) {
String databaseName = alterSchemaPOJO.getDatabaseName();
Expand All @@ -105,17 +116,6 @@ public void dropSchema(final String databaseName, final Collection<String> schem
schemaNames.forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getSchema().drop(databaseName, each));
}

@Override
public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO) {
String databaseName = alterSchemaMetaDataPOJO.getDatabaseName();
String schemaName = alterSchemaMetaDataPOJO.getSchemaName();
DatabaseMetaDataPersistFacade databaseMetaDataFacade = metaDataPersistService.getDatabaseMetaDataFacade();
databaseMetaDataFacade.getTable().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredTables());
databaseMetaDataFacade.getView().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredViews());
alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> databaseMetaDataFacade.getTable().drop(databaseName, schemaName, each));
alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> databaseMetaDataFacade.getView().delete(databaseName, schemaName, each));
}

@SneakyThrows
@Override
public void registerStorageUnits(final String databaseName, final Map<String, DataSourcePoolProperties> toBeRegisteredProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO;
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.metadata.MetaDataContextManager;
import org.apache.shardingsphere.mode.state.database.ListenerAssistedType;
import org.apache.shardingsphere.mode.state.database.ListenerAssistedPersistService;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
import org.apache.shardingsphere.mode.state.database.ListenerAssistedPersistService;
import org.apache.shardingsphere.mode.state.database.ListenerAssistedType;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -97,7 +97,14 @@ void assertCreateSchema() {
}

@Test
void assertAlterNotEmptySchema() {
void assertAlterSchema() {
metaDataManagerPersistService.alterSchema(new AlterSchemaMetaDataPOJO("foo_db", "foo_schema", Collections.singleton("foo_ds")));
verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Collections.emptyList());
verify(metaDataPersistService.getDatabaseMetaDataFacade().getView()).persist("foo_db", "foo_schema", Collections.emptyList());
}

@Test
void assertAlterNotEmptySchemaName() {
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
assertAlterSchemaName(schema);
verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema(), times(0)).add("foo_db", "bar_schema");
Expand Down Expand Up @@ -125,13 +132,6 @@ void assertDropSchema() {
verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema()).drop("foo_db", "foo_schema");
}

@Test
void assertAlterSchemaMetaData() {
metaDataManagerPersistService.alterSchemaMetaData(new AlterSchemaMetaDataPOJO("foo_db", "foo_schema", Collections.singleton("foo_ds")));
verify(metaDataPersistService.getDatabaseMetaDataFacade().getTable()).persist("foo_db", "foo_schema", Collections.emptyList());
verify(metaDataPersistService.getDatabaseMetaDataFacade().getView()).persist("foo_db", "foo_schema", Collections.emptyList());
}

@Test
void assertAlterSingleRuleConfiguration() {
Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(Arrays.asList(new SingleRuleConfiguration(), mock(RuleConfiguration.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ public void createSchema(final String databaseName, final String schemaName) {
metaDataPersistService.getDatabaseMetaDataFacade().getSchema().add(databaseName, schemaName);
}

@Override
public void alterSchema(final AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO) {
String databaseName = alterSchemaMetaDataPOJO.getDatabaseName();
String schemaName = alterSchemaMetaDataPOJO.getSchemaName();
ShardingSphereMetaData metaData = metaDataContextManager.getMetaDataContexts().getMetaData();
ShardingSphereDatabase database = metaData.getDatabase(databaseName);
addDataNode(database, alterSchemaMetaDataPOJO.getLogicDataSourceName(), schemaName, alterSchemaMetaDataPOJO.getAlteredTables(), alterSchemaMetaDataPOJO.getAlteredViews());
removeDataNode(database, schemaName, alterSchemaMetaDataPOJO.getDroppedTables(), alterSchemaMetaDataPOJO.getDroppedViews());
metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getAllDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED));
metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredTables());
metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredViews());
alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getTable().drop(databaseName, schemaName, each));
alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getView().delete(databaseName, schemaName, each));
}

@Override
public void alterSchemaName(final AlterSchemaPOJO alterSchemaPOJO) {
ShardingSphereMetaData metaData = metaDataContextManager.getMetaDataContexts().getMetaData();
Expand Down Expand Up @@ -203,21 +218,6 @@ public void dropSchema(final String databaseName, final Collection<String> schem
metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getAllDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED));
}

@Override
public void alterSchemaMetaData(final AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO) {
String databaseName = alterSchemaMetaDataPOJO.getDatabaseName();
String schemaName = alterSchemaMetaDataPOJO.getSchemaName();
ShardingSphereMetaData metaData = metaDataContextManager.getMetaDataContexts().getMetaData();
ShardingSphereDatabase database = metaData.getDatabase(databaseName);
addDataNode(database, alterSchemaMetaDataPOJO.getLogicDataSourceName(), schemaName, alterSchemaMetaDataPOJO.getAlteredTables(), alterSchemaMetaDataPOJO.getAlteredViews());
removeDataNode(database, schemaName, alterSchemaMetaDataPOJO.getDroppedTables(), alterSchemaMetaDataPOJO.getDroppedViews());
metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getAllDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED));
metaDataPersistService.getDatabaseMetaDataFacade().getTable().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredTables());
metaDataPersistService.getDatabaseMetaDataFacade().getView().persist(databaseName, schemaName, alterSchemaMetaDataPOJO.getAlteredViews());
alterSchemaMetaDataPOJO.getDroppedTables().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getTable().drop(databaseName, schemaName, each));
alterSchemaMetaDataPOJO.getDroppedViews().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade().getView().delete(databaseName, schemaName, each));
}

@Override
public void registerStorageUnits(final String databaseName, final Map<String, DataSourcePoolProperties> toBeRegisteredProps) throws SQLException {
SwitchingResource switchingResource = metaDataContextManager.getResourceSwitchManager().switchByRegisterStorageUnit(metaDataContextManager.getMetaDataContexts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ void assertCreateSchema() {
verify(metaDataPersistService.getDatabaseMetaDataFacade().getSchema()).add("foo_db", "foo_schema");
}

@Test
void assertAlterSchema() {
DatabaseMetaDataPersistFacade databaseMetaDataFacade = mock(DatabaseMetaDataPersistFacade.class, RETURNS_DEEP_STUBS);
when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade);
metaDataManagerPersistService.alterSchema(new AlterSchemaMetaDataPOJO("foo_db", "foo_schema", Collections.singleton("foo_ds")));
verify(databaseMetaDataFacade.getTable()).persist("foo_db", "foo_schema", new LinkedList<>());
}

@Test
void assertAlterSchemaNameWithEmptyAlteredSchema() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
Expand Down Expand Up @@ -141,14 +149,6 @@ void assertDropSchema() {
verify(database).dropSchema(any());
}

@Test
void assertAlterSchemaMetaData() {
DatabaseMetaDataPersistFacade databaseMetaDataFacade = mock(DatabaseMetaDataPersistFacade.class, RETURNS_DEEP_STUBS);
when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade);
metaDataManagerPersistService.alterSchemaMetaData(new AlterSchemaMetaDataPOJO("foo_db", "foo_schema", Collections.singleton("foo_ds")));
verify(databaseMetaDataFacade.getTable()).persist("foo_db", "foo_schema", new LinkedList<>());
}

@Test
void assertAlterSingleRuleConfiguration() throws SQLException {
RuleConfiguration singleRuleConfig = new SingleRuleConfiguration();
Expand Down

0 comments on commit 8ed79ed

Please sign in to comment.