Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[type:bug] fix namespace delete error #5913

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ ALTER TABLE `proxy_selector` ADD COLUMN `namespace_id` varchar(50) NOT NULL COMM

ALTER TABLE `alert_receiver` ADD COLUMN `namespace_id` varchar(50) NOT NULL COMMENT 'namespaceId' AFTER `levels`;

ALTER TABLE `auth_path` ADD COLUMN `namespace_id` varchar(50) NOT NULL COMMENT 'namespaceId' AFTER `path`;

UPDATE auth_path
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;

UPDATE selector
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;
Expand Down
8 changes: 7 additions & 1 deletion db/upgrade/2.7.0-upgrade-2.7.1-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@
-- limitations under the License.

-- this file works for MySQL.
INSERT INTO `plugin_handle` VALUES ('1722804548510507032', '19', 'handleType', 'handleType', 2, 3, 1, '{"required":"0","rule":""}', '2025-01-02 17:20:50.233', '2025-01-02 17:20:50.233');
INSERT INTO `plugin_handle` VALUES ('1722804548510507032', '19', 'handleType', 'handleType', 2, 3, 1, '{"required":"0","rule":""}', '2025-01-02 17:20:50.233', '2025-01-02 17:20:50.233');

ALTER TABLE `auth_path` ADD COLUMN `namespace_id` varchar(50) NOT NULL COMMENT 'namespaceId' AFTER `path`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls move to db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and add sql to other db files too, pls


UPDATE auth_path
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public interface AppAuthMapper extends ExistProvider {
*/
List<AppAuthDO> selectAllByNamespaceId(String namespaceId);

/**
* Find by namespace id list.
*
* @param namespaceIds the namespaceIds
* @return the list
*/
List<AppAuthDO> findByNamespaceIds(List<String> namespaceIds);

/**
* count application authority by query.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.apache.shenyu.admin.model.dto;

import jakarta.validation.constraints.NotBlank;
import org.apache.shenyu.admin.mapper.NamespaceMapper;
import org.apache.shenyu.admin.validation.annotation.Existed;

import java.io.Serializable;
import java.util.Objects;

Expand All @@ -33,6 +36,9 @@ public class AuthPathDTO implements Serializable {
@NotBlank
private String path;

@Existed(message = "namespaceId is not existed", provider = NamespaceMapper.class)
private String namespaceId;

private Boolean enabled;

/**
Expand Down Expand Up @@ -71,6 +77,24 @@ public void setPath(final String path) {
this.path = path;
}

/**
* Gets the value of namespaceId.
*
* @return the value of namespaceId
*/
public String getNamespaceId() {
return namespaceId;
}

/**
* Sets the namespaceId.
*
* @param namespaceId namespaceId
*/
public void setNamespaceId(final String namespaceId) {
this.namespaceId = namespaceId;
}

/**
* Gets the value of enabled.
*
Expand Down Expand Up @@ -98,7 +122,8 @@ public boolean equals(final Object o) {
return false;
}
AuthPathDTO that = (AuthPathDTO) o;
return Objects.equals(appName, that.appName) && Objects.equals(path, that.path) && Objects.equals(enabled, that.enabled);
return Objects.equals(appName, that.appName) && Objects.equals(path, that.path)
&& Objects.equals(enabled, that.enabled) && Objects.equals(namespaceId, that.namespaceId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ public final class AuthPathDO extends BaseDO {

private Boolean enabled;

private String namespaceId;

public AuthPathDO() {
}

public AuthPathDO(final String authId, final String appName, final String path, final Boolean enabled) {
public AuthPathDO(final String authId, final String appName, final String path, final Boolean enabled, final String namespaceId) {
this.authId = authId;
this.appName = appName;
this.path = path;
this.enabled = enabled;
this.namespaceId = namespaceId;
}

/**
Expand Down Expand Up @@ -119,22 +122,33 @@ public void setEnabled(final Boolean enabled) {
this.enabled = enabled;
}

/**
* set namespaceId.
*
* @param namespaceId namespaceId
*/
public void setNamespaceId(final String namespaceId) {
this.namespaceId = namespaceId;
}

/**
* Build AuthPathDO object with given params.
*
* @param path {@linkplain String}
* @param authId {@linkplain String}
* @param appName {@linkplain String}
* @param namespaceId {@linkplain String}
* @return {@linkplain AuthPathDO}
*/
public static AuthPathDO create(final String path, final String authId, final String appName) {
public static AuthPathDO create(final String path, final String authId, final String appName, final String namespaceId) {
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
return AuthPathDO.builder()
.id(UUIDUtils.getInstance().generateShortUuid())
.authId(authId)
.appName(appName)
.path(path)
.enabled(true)
.namespaceId(namespaceId)
.dateCreated(currentTime)
.dateUpdated(currentTime)
.build();
Expand All @@ -152,12 +166,13 @@ public boolean equals(final Object o) {
return false;
}
AuthPathDO that = (AuthPathDO) o;
return Objects.equals(authId, that.authId) && Objects.equals(appName, that.appName) && Objects.equals(path, that.path) && Objects.equals(enabled, that.enabled);
return Objects.equals(authId, that.authId) && Objects.equals(appName, that.appName) && Objects.equals(path, that.path)
&& Objects.equals(namespaceId, that.namespaceId) && Objects.equals(enabled, that.enabled);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), authId, appName, path, enabled);
return Objects.hash(super.hashCode(), authId, appName, path, enabled, namespaceId);
}

/**
Expand All @@ -179,6 +194,8 @@ public static final class AuthPathDOBuilder {

private Boolean enabled;

private String namespaceId;

private String id;

private Timestamp dateCreated;
Expand Down Expand Up @@ -243,6 +260,17 @@ public AuthPathDOBuilder id(final String id) {
return this;
}

/**
* namespaceId.
*
* @param namespaceId namespaceId
* @return AppAuthDOBuilder
*/
public AuthPathDOBuilder namespaceId(final String namespaceId) {
this.namespaceId = namespaceId;
return this;
}

/**
* dateCreated.
*
Expand Down Expand Up @@ -277,6 +305,7 @@ public AuthPathDO build() {
authPathDO.setPath(path);
authPathDO.setEnabled(enabled);
authPathDO.setId(id);
authPathDO.setNamespaceId(namespaceId);
authPathDO.setDateCreated(dateCreated);
authPathDO.setDateUpdated(dateUpdated);
return authPathDO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public ShenyuAdminResult applyCreate(final AuthApplyDTO authApplyDTO) {
if (Boolean.TRUE.equals(appAuthDO.getOpen())) {
List<AuthPathDO> collect = authApplyDTO.getPathList()
.stream()
.map(path -> AuthPathDO.create(path, appAuthDO.getId(), authApplyDTO.getAppName()))
.map(path -> AuthPathDO.create(path, appAuthDO.getId(), authApplyDTO.getAppName(), appAuthDO.getNamespaceId()))
.collect(Collectors.toList());
authPathMapper.batchSave(collect);
data.setPathDataList(collect.stream().map(authPathDO ->
Expand Down Expand Up @@ -163,7 +163,7 @@ public ShenyuAdminResult applyUpdate(final AuthApplyDTO authApplyDTO) {
}
List<AuthPathDO> collect = authApplyDTO.getPathList()
.stream()
.map(path -> AuthPathDO.create(path, appAuthDO.getId(), authApplyDTO.getAppName()))
.map(path -> AuthPathDO.create(path, appAuthDO.getId(), authApplyDTO.getAppName(), appAuthDO.getNamespaceId()))
.collect(Collectors.toList());
authPathMapper.batchSave(collect);
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public ShenyuAdminResult updateDetail(final AppAuthDTO appAuthDTO) {

List<AuthPathDO> authPathDOList = authPathDTOList.stream()
.filter(Objects::nonNull)
.map(dto -> AuthPathDO.create(dto.getPath(), appAuthDTO.getId(), appName))
.map(dto -> AuthPathDO.create(dto.getPath(), appAuthDTO.getId(), appName, appAuthDTO.getNamespaceId()))
.collect(Collectors.toList());
authPathMapper.batchSave(authPathDOList);
}
Expand All @@ -223,7 +223,7 @@ public ShenyuAdminResult updateDetailPath(final AuthPathWarpDTO authPathWarpDTO)

List<AuthPathDO> collect = authPathDTOList.stream()
.filter(Objects::nonNull)
.map(authPathDTO -> AuthPathDO.create(authPathDTO.getPath(), appAuthDO.getId(), authPathDTO.getAppName()))
.map(authPathDTO -> AuthPathDO.create(authPathDTO.getPath(), appAuthDO.getId(), authPathDTO.getAppName(), appAuthDO.getNamespaceId()))
.collect(Collectors.toList());
authPathMapper.batchSave(collect);
}
Expand Down Expand Up @@ -309,7 +309,7 @@ public ConfigImportResult importData(final List<AppAuthDTO> authDataList) {
if (CollectionUtils.isNotEmpty(authPathDTOList)) {
List<AuthPathDO> authPathDOS = authPathDTOList
.stream()
.map(param -> AuthPathDO.create(param.getPath(), authId, param.getAppName()))
.map(param -> AuthPathDO.create(param.getPath(), authId, param.getAppName(), param.getNamespaceId()))
.collect(Collectors.toList());
authPathMapper.batchSave(authPathDOS);
}
Expand Down Expand Up @@ -371,7 +371,7 @@ public ConfigImportResult importData(final String namespace, final List<AppAuthD
if (CollectionUtils.isNotEmpty(authPathDTOList)) {
List<AuthPathDO> authPathDOS = authPathDTOList
.stream()
.map(param -> AuthPathDO.create(param.getPath(), authId, param.getAppName()))
.map(param -> AuthPathDO.create(param.getPath(), authId, param.getAppName(), param.getNamespaceId()))
.collect(Collectors.toList());
authPathMapper.batchSave(authPathDOS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.exception.ShenyuAdminException;
import org.apache.shenyu.admin.mapper.NamespaceMapper;
import org.apache.shenyu.admin.mapper.SelectorMapper;
import org.apache.shenyu.admin.mapper.RuleMapper;
import org.apache.shenyu.admin.mapper.AuthPathMapper;
import org.apache.shenyu.admin.mapper.DiscoveryMapper;
import org.apache.shenyu.admin.mapper.MetaDataMapper;
import org.apache.shenyu.admin.mapper.NamespaceMapper;
import org.apache.shenyu.admin.mapper.DiscoveryMapper;
import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper;
import org.apache.shenyu.admin.mapper.RuleMapper;
import org.apache.shenyu.admin.mapper.SelectorMapper;
import org.apache.shenyu.admin.mapper.AppAuthMapper;
import org.apache.shenyu.admin.model.dto.NamespaceDTO;
import org.apache.shenyu.admin.model.entity.AuthPathDO;
import org.apache.shenyu.admin.model.entity.DiscoveryDO;
import org.apache.shenyu.admin.model.entity.MetaDataDO;
import org.apache.shenyu.admin.model.entity.NamespaceDO;
import org.apache.shenyu.admin.model.entity.RuleDO;
import org.apache.shenyu.admin.model.entity.SelectorDO;
import org.apache.shenyu.admin.model.entity.RuleDO;
import org.apache.shenyu.admin.model.entity.MetaDataDO;
import org.apache.shenyu.admin.model.entity.AppAuthDO;
import org.apache.shenyu.admin.model.entity.DiscoveryDO;
import org.apache.shenyu.admin.model.event.namespace.NamespaceCreatedEvent;
import org.apache.shenyu.admin.model.page.CommonPager;
import org.apache.shenyu.admin.model.page.PageResultUtils;
Expand Down Expand Up @@ -80,6 +81,8 @@ public class NamespaceServiceImpl implements NamespaceService {

private final NamespacePluginRelMapper namespacePluginRelMapper;

private final AppAuthMapper appAuthMapper;


public NamespaceServiceImpl(final NamespaceMapper namespaceMapper,
final NamespaceUserService namespaceUserService,
Expand All @@ -89,7 +92,7 @@ public NamespaceServiceImpl(final NamespaceMapper namespaceMapper,
final RuleMapper ruleMapper,
final AuthPathMapper authPathMapper,
final MetaDataMapper metaDataMapper,
final DiscoveryMapper discoveryMapper) {
final DiscoveryMapper discoveryMapper, final AppAuthMapper appAuthMapper) {
this.namespaceMapper = namespaceMapper;
this.namespaceUserService = namespaceUserService;
this.namespaceEventPublisher = namespaceEventPublisher;
Expand All @@ -99,6 +102,7 @@ public NamespaceServiceImpl(final NamespaceMapper namespaceMapper,
this.authPathMapper = authPathMapper;
this.metaDataMapper = metaDataMapper;
this.discoveryMapper = discoveryMapper;
this.appAuthMapper = appAuthMapper;
}

@Override
Expand Down Expand Up @@ -152,9 +156,9 @@ public String delete(final List<String> ids) {
if (CollectionUtils.isNotEmpty(metaDataDOList)) {
throw new ShenyuAdminException("metaData exist under those namespace!");
}
List<AuthPathDO> authPathDOList = authPathMapper.findByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(authPathDOList)) {
throw new ShenyuAdminException("authPath exist under those namespace!");
List<AppAuthDO> appAuthDOList = appAuthMapper.findByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(appAuthDOList)) {
throw new ShenyuAdminException("appAuth exist under those namespace!");
}
List<DiscoveryDO> discoveryDOList = discoveryMapper.selectAllByNamespaceIds(namespaceIdList);
if (CollectionUtils.isNotEmpty(discoveryDOList)) {
Expand Down
10 changes: 10 additions & 0 deletions shenyu-admin/src/main/resources/mappers/app-auth-sqlmap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@
</foreach>
</select>

<select id="findByNamespaceIds" parameterType="java.lang.String" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM app_auth
WHERE namespace_id IN
<foreach item="namespaceId" collection="namespaceIds" open="(" separator="," close=")">
#{namespaceId, jdbcType=VARCHAR}
</foreach>
</select>

<select id="findByAppKey" parameterType="java.lang.String" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<result column="auth_id" jdbcType="VARCHAR" property="authId"/>
<result column="app_name" jdbcType="VARCHAR" property="appName"/>
<result column="path" jdbcType="VARCHAR" property="path"/>
<result column="namespace_id" jdbcType="VARCHAR" property="namespaceId"/>
<result column="enabled" jdbcType="TINYINT" property="enabled"/>
<result column="date_created" jdbcType="TIMESTAMP" property="dateCreated"/>
<result column="date_updated" jdbcType="TIMESTAMP" property="dateUpdated"/>
Expand Down