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

Refactor the soft delete design #3866

Merged
merged 25 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9378a66
refactor: soft delete design, add `DeletedAt` column
nisiyong May 29, 2021
ba6b836
refactor: replace deletedAt with DataChange_DeletedTime
nisiyong Jul 31, 2021
a3c2ee7
refactor: add DataChange_DeletedTime for BaseEntity, update all @SQLD…
nisiyong Jul 31, 2021
ed541df
refactor: add deletedAt column base on seconds
nisiyong Aug 1, 2021
13b8977
refactor: add DeletedAt field for BaseEntity, update all soft delete …
nisiyong Aug 1, 2021
75e6db1
refactor: add DML for each table and add UNIQUE INDEX(WIP)
nisiyong Aug 1, 2021
066d30a
refactor: update Delete timestamp based on milliseconds
nisiyong Aug 4, 2021
56bae06
fix: add custom SQL function NOW() with args to adapt the @Query stat…
nisiyong Aug 5, 2021
4d027a4
test: update application.properties
nisiyong Aug 9, 2021
708f8a5
fix: deletedAt NPE
nisiyong Aug 9, 2021
2b007e0
test: create alias for H2 function
nisiyong Aug 9, 2021
f230cc1
fix: license check
nisiyong Aug 9, 2021
e56d273
fix: move import.sql to the test package
nisiyong Aug 11, 2021
4871fbc
fix: move delta sql from v180-v190 to v1_9_0-v1_10_0
nisiyong Sep 4, 2021
64a1df5
feat: add UNIQUE CONSTRAINT INDEX for each table
nisiyong Sep 4, 2021
d7ec1f9
docs: update the CHANGES.md
nisiyong Sep 4, 2021
3e62f15
fix: update Item unique key to support multi empty keys
nisiyong Sep 21, 2021
c112121
fix: add Cluster Column in unique keys on ApolloConfigDB.ServerConfig
nisiyong Sep 21, 2021
2a5b718
fix: move sql to v190-v200 and polish DML
nisiyong Mar 10, 2022
82cbdb5
feat: remove unique index DDL on `GrayReleaseRule` and `Item`
nisiyong Mar 12, 2022
2690297
feat: update sql scripts for docker-quick-stark and flyway
nisiyong Mar 12, 2022
c74cba3
fix: set copyright 2022
nisiyong Mar 14, 2022
de69f3d
Merge branch 'master' into refactor-soft-delete
nobodyiam Mar 16, 2022
25616e6
Merge branch 'master' into refactor-soft-delete
lepdou Mar 17, 2022
ed480c5
Merge branch 'master' into refactor-soft-delete
Anilople Mar 18, 2022
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
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Apollo 2.0.0
* [optimize import/export config](https://github.com/apolloconfig/apollo/pull/4231)
* [Configure publish and rollback modal boxes to add scrollbars](https://github.com/apolloconfig/apollo/pull/4251)
* [fix import config bug](https://github.com/apolloconfig/apollo/pull/4262)
* [Refactor the soft delete design](https://github.com/apolloconfig/apollo/pull/3866)
* [Fix the potential data inconsistency issue](https://github.com/apolloconfig/apollo/pull/4256)

------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)
4 changes: 4 additions & 0 deletions apollo-adminservice/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ spring:
enabled: false
zookeeper:
enabled: false
jpa:
properties:
hibernate:
metadata_builder_contributor: com.ctrip.framework.apollo.common.jpa.SqlFunctionsMetadataBuilderContributor

ctrip:
appid: 100003172
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
spring.datasource.url = jdbc:h2:mem:~/apolloconfigdb;mode=mysql;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.metadata_builder_contributor=com.ctrip.framework.apollo.common.jpa.SqlFunctionsMetadataBuilderContributor
spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true
spring.main.allow-bean-definition-overriding=true
Expand Down
16 changes: 16 additions & 0 deletions apollo-adminservice/src/test/resources/import.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--
-- Copyright 2022 Apollo Authors
--
-- 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.
--
CREATE ALIAS IF NOT EXISTS UNIX_TIMESTAMP FOR "com.ctrip.framework.apollo.common.jpa.H2Function.unixTimestamp";
4 changes: 4 additions & 0 deletions apollo-assembly/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ spring:
enabled: false
session:
store-type: none
jpa:
properties:
hibernate:
metadata_builder_contributor: com.ctrip.framework.apollo.common.jpa.SqlFunctionsMetadataBuilderContributor

logging:
file:
Expand Down
1 change: 1 addition & 0 deletions apollo-assembly/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
spring.datasource.url = jdbc:h2:mem:~/apolloconfigdb;mode=mysql;DB_CLOSE_ON_EXIT=FALSE
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.metadata_builder_contributor=com.ctrip.framework.apollo.common.jpa.SqlFunctionsMetadataBuilderContributor
spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true
apollo.portal.env= local
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@Entity
@Table(name = "AccessKey")
@SQLDelete(sql = "Update AccessKey set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update AccessKey set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class AccessKey extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@Entity
@Table(name = "Audit")
@SQLDelete(sql = "Update Audit set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update Audit set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class Audit extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
@Entity
@Table(name = "Cluster")
@SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update Cluster set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class Cluster extends BaseEntity implements Comparable<Cluster> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

@Entity
@Table(name = "Commit")
@SQLDelete(sql = "Update Commit set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update Commit set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class Commit extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@Entity
@Table(name = "GrayReleaseRule")
@SQLDelete(sql = "Update GrayReleaseRule set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update GrayReleaseRule set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class GrayReleaseRule extends BaseEntity{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

@Entity
@Table(name = "Item")
@SQLDelete(sql = "Update Item set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update Item set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class Item extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@Entity
@Table(name = "Namespace")
@SQLDelete(sql = "Update Namespace set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update Namespace set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class Namespace extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@Entity
@Table(name = "Privilege")
@SQLDelete(sql = "Update Privilege set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update Privilege set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class Privilege extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
@Entity
@Table(name = "Release")
@SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update Release set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class Release extends BaseEntity {
@Column(name = "ReleaseKey", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
@Entity
@Table(name = "ReleaseHistory")
@SQLDelete(sql = "Update ReleaseHistory set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update ReleaseHistory set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class ReleaseHistory extends BaseEntity {
@Column(name = "AppId", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
@Entity
@Table(name = "ServerConfig")
@SQLDelete(sql = "Update ServerConfig set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update ServerConfig set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class ServerConfig extends BaseEntity {
@Column(name = "Key", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public interface AppNamespaceRepository extends PagingAndSortingRepository<AppNa
List<AppNamespace> findFirst500ByIdGreaterThanOrderByIdAsc(long id);

@Modifying
@Query("UPDATE AppNamespace SET IsDeleted=1,DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
@Query("UPDATE AppNamespace SET IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?2 WHERE AppId=?1")
int batchDeleteByAppId(String appId, String operator);

@Modifying
@Query("UPDATE AppNamespace SET IsDeleted=1,DataChange_LastModifiedBy = ?3 WHERE AppId=?1 and Name = ?2")
@Query("UPDATE AppNamespace SET IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?3 WHERE AppId=?1 and Name = ?2")
int delete(String appId, String namespaceName, String operator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ List<Commit> findByAppIdAndClusterNameAndNamespaceNameAndDataChangeLastModifiedT
String appId, String clusterName, String namespaceName, Date dataChangeLastModifiedTime, Pageable pageable);

@Modifying
@Query("update Commit set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
@Query("update Commit set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
int batchDelete(String appId, String clusterName, String namespaceName, String operator);

List<Commit> findByAppIdAndClusterNameAndNamespaceNameAndChangeSetsLikeOrderByIdDesc(String appId, String clusterName, String namespaceName,String changeSets, Pageable page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {
Item findFirst1ByNamespaceIdOrderByLineNumDesc(Long namespaceId);

@Modifying
@Query("update Item set isdeleted=1,DataChange_LastModifiedBy = ?2 where namespaceId = ?1")
@Query("update Item set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?2 where namespaceId = ?1")
int deleteByNamespaceId(long namespaceId, String operator);

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface NamespaceRepository extends PagingAndSortingRepository<Namespac
Namespace findByAppIdAndClusterNameAndNamespaceName(String appId, String clusterName, String namespaceName);

@Modifying
@Query("update Namespace set isdeleted=1,DataChange_LastModifiedBy = ?3 where appId=?1 and clusterName=?2")
@Query("update Namespace set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?3 where appId=?1 and clusterName=?2")
int batchDelete(String appId, String clusterName, String operator);

List<Namespace> findByAppIdAndNamespaceNameOrderByIdAsc(String appId, String namespaceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Page<ReleaseHistory> findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(Stri
Page<ReleaseHistory> findByReleaseIdAndOperationInOrderByIdDesc(long releaseId, Set<Integer> operations, Pageable pageable);

@Modifying
@Query("update ReleaseHistory set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
@Query("update ReleaseHistory set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
int batchDelete(String appId, String clusterName, String namespaceName, String operator);

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Release findFirstByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderBy
List<Release> findByIdIn(Set<Long> releaseIds);

@Modifying
@Query("update Release set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
@Query("update Release set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
int batchDelete(String appId, String clusterName, String namespaceName, String operator);

// For release history conversion program, need to delete after conversion it done
Expand Down
1 change: 1 addition & 0 deletions apollo-biz/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
spring.datasource.url = jdbc:h2:mem:~/apolloconfigdb;mode=mysql;DB_CLOSE_ON_EXIT=FALSE
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.metadata_builder_contributor=com.ctrip.framework.apollo.common.jpa.SqlFunctionsMetadataBuilderContributor
spring.h2.console.enabled = true
spring.h2.console.settings.web-allow-others=true
spring.cloud.consul.enabled=false
Expand Down
16 changes: 16 additions & 0 deletions apollo-biz/src/test/resources/import.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--
-- Copyright 2022 Apollo Authors
--
-- 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.
--
CREATE ALIAS IF NOT EXISTS UNIX_TIMESTAMP FOR "com.ctrip.framework.apollo.common.jpa.H2Function.unixTimestamp";
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

@Entity
@Table(name = "App")
@SQLDelete(sql = "Update App set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update App set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class App extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

@Entity
@Table(name = "AppNamespace")
@SQLDelete(sql = "Update AppNamespace set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update AppNamespace set IsDeleted = 1, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000) where Id = ?")
@Where(clause = "isDeleted = 0")
public class AppNamespace extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;

import java.time.Instant;
import java.util.Date;

import javax.persistence.Column;
Expand All @@ -44,6 +45,9 @@ public abstract class BaseEntity {
@Column(name = "IsDeleted", columnDefinition = "Bit default '0'")
protected boolean isDeleted = false;

@Column(name = "DeletedAt", columnDefinition = "Bigint default '0'")
protected long deletedAt;

@Column(name = "DataChange_CreatedBy", nullable = false)
private String dataChangeCreatedBy;

Expand All @@ -56,52 +60,62 @@ public abstract class BaseEntity {
@Column(name = "DataChange_LastTime")
private Date dataChangeLastModifiedTime;

public String getDataChangeCreatedBy() {
return dataChangeCreatedBy;
public long getId() {
return id;
}

public Date getDataChangeCreatedTime() {
return dataChangeCreatedTime;
public void setId(long id) {
this.id = id;
}

public String getDataChangeLastModifiedBy() {
return dataChangeLastModifiedBy;
public boolean isDeleted() {
return isDeleted;
}

public Date getDataChangeLastModifiedTime() {
return dataChangeLastModifiedTime;
public void setDeleted(boolean deleted) {
isDeleted = deleted;
// also set deletedAt value as epoch millisecond
this.deletedAt = System.currentTimeMillis();
}

public long getId() {
return id;
public long getDeletedAt() {
return deletedAt;
}

public boolean isDeleted() {
return isDeleted;
public void setDeletedAt(long deletedAt) {
this.deletedAt = deletedAt;
}

public String getDataChangeCreatedBy() {
return dataChangeCreatedBy;
}

public void setDataChangeCreatedBy(String dataChangeCreatedBy) {
this.dataChangeCreatedBy = dataChangeCreatedBy;
}

public Date getDataChangeCreatedTime() {
return dataChangeCreatedTime;
}

public void setDataChangeCreatedTime(Date dataChangeCreatedTime) {
this.dataChangeCreatedTime = dataChangeCreatedTime;
}

public void setDataChangeLastModifiedBy(String dataChangeLastModifiedBy) {
this.dataChangeLastModifiedBy = dataChangeLastModifiedBy;
public String getDataChangeLastModifiedBy() {
return dataChangeLastModifiedBy;
}

public void setDataChangeLastModifiedTime(Date dataChangeLastModifiedTime) {
this.dataChangeLastModifiedTime = dataChangeLastModifiedTime;
public void setDataChangeLastModifiedBy(String dataChangeLastModifiedBy) {
this.dataChangeLastModifiedBy = dataChangeLastModifiedBy;
}

public void setDeleted(boolean deleted) {
isDeleted = deleted;
public Date getDataChangeLastModifiedTime() {
return dataChangeLastModifiedTime;
}

public void setId(long id) {
this.id = id;
public void setDataChangeLastModifiedTime(Date dataChangeLastModifiedTime) {
this.dataChangeLastModifiedTime = dataChangeLastModifiedTime;
}

@PrePersist
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022 Apollo Authors
*
* 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.ctrip.framework.apollo.common.jpa;

/**
* @author nisiyong
*/
public class H2Function {

public static long unixTimestamp(java.sql.Timestamp timestamp) {
return timestamp.getTime() / 1000L;
}
}
Loading