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

fix(migration): missing user organization relation migration #291

Merged
merged 1 commit into from
Sep 10, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@
public interface UserOrganizationRepository extends JpaRepository<UserOrganizationEntity, Long>,
JpaSpecificationExecutor<UserOrganizationEntity> {
boolean existsByOrganizationId(Long organizationId);

boolean existsByOrganizationIdAndUserId(Long organizationId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public class DefaultOrganizationResourceMigrator implements OrganizationResource
@Autowired
private VerticalPermissionValidator verticalPermissionValidator;

@Autowired
private UserOrganizationMigrator userOrganizationMigrator;

@Override
public void migrate(@NonNull User user) {
if (Objects.isNull(user)) {
Expand All @@ -92,6 +95,7 @@ public void migrate(@NonNull User user) {
user.getOrganizationId(), user.getId());
ruleApplyingMigrator.migrate(user.getOrganizationId());
}
userOrganizationMigrator.migrate(user);
return null;
} catch (Exception ex) {
transactionStatus.setRollbackOnly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.oceanbase.odc.metadb.connection.ConnectionConfigRepository;
import com.oceanbase.odc.metadb.iam.OrganizationRepository;
import com.oceanbase.odc.metadb.iam.RoleRepository;
import com.oceanbase.odc.metadb.iam.UserOrganizationEntity;
import com.oceanbase.odc.metadb.iam.UserOrganizationRepository;
import com.oceanbase.odc.service.common.migrate.DefaultValueEncoderFactory;
import com.oceanbase.odc.service.common.migrate.DefaultValueGeneratorFactory;
Expand Down Expand Up @@ -89,25 +88,21 @@ public class TeamOrganizationMigrator {

@Transactional(rollbackFor = Exception.class)
public void migrate(@NonNull User user) {
UserOrganizationEntity userOrganizationEntity = new UserOrganizationEntity();
userOrganizationEntity.setOrganizationId(user.getOrganizationId());
userOrganizationEntity.setUserId(user.getId());
UserOrganizationEntity saved = userOrganizationRepository.saveAndFlush(userOrganizationEntity);
/**
* init resources for this TEAM organization
*/
ResourceInitializer initializer = new ResourceInitializer(
getResourceConfig(dataSource, saved.getUserId(), saved.getOrganizationId()));
getResourceConfig(dataSource, user.getId(), user.getOrganizationId()));
try {
initializer.init();
migrateDataSource(saved.getOrganizationId());
migrateRole(saved.getOrganizationId());
migrateDataSource(user.getOrganizationId());
migrateRole(user.getOrganizationId());
} catch (Exception e) {
throw new RuntimeException("init organization resource failed, organizationId=" + saved.getOrganizationId(),
throw new RuntimeException("init organization resource failed, organizationId=" + user.getOrganizationId(),
e);
}
log.info("create TEAM organization and resources for user succeed, userId={}, organizationId={}",
saved.getUserId(), saved.getOrganizationId());
user.getId(), user.getOrganizationId());
}

private ResourceConfig getResourceConfig(DataSource dataSource, Long userId, Long organizationId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed 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 com.oceanbase.odc.service.collaboration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.oceanbase.odc.core.authority.util.SkipAuthorize;
import com.oceanbase.odc.metadb.iam.UserOrganizationEntity;
import com.oceanbase.odc.metadb.iam.UserOrganizationRepository;
import com.oceanbase.odc.service.iam.model.User;

import lombok.extern.slf4j.Slf4j;

/**
* @Author: Lebie
* @Date: 2023/9/10 00:59
* @Description: []
*/
@Slf4j
@Service
@Validated
@SkipAuthorize
public class UserOrganizationMigrator {
@Autowired
private UserOrganizationRepository userOrganizationRepository;

@Transactional(rollbackFor = Exception.class)
public void migrate(User user) {
if (!userOrganizationRepository.existsByOrganizationIdAndUserId(user.getOrganizationId(), user.getId())) {
UserOrganizationEntity userOrganizationEntity = new UserOrganizationEntity();
userOrganizationEntity.setOrganizationId(user.getOrganizationId());
userOrganizationEntity.setUserId(user.getId());
userOrganizationRepository.saveAndFlush(userOrganizationEntity);
}
}
}