Skip to content

Commit

Permalink
feat(MTR): remove tenantid from documents (#3329)
Browse files Browse the repository at this point in the history
Multi-tenancy removal for tables:
* document
* document_mapping
* arch_document_mapping

relates to https://bonitasoft.atlassian.net/browse/BPM-347
  • Loading branch information
educhastenier authored Jan 22, 2025
1 parent 296fb86 commit 30ac8f3
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public class DocumentQueryTest {
public void getLightDocument_should_read_previously_saved_document() {
// given
repository.add(
SLightDocument.builder().id(99L).tenantId(3L).author(11L).hasContent(true).fileName("myFile.pdf")
SLightDocument.builder().id(99L).author(11L).hasContent(true).fileName("myFile.pdf")
.mimeType("application/pdf").author(22L)
.build());

//when
final SLightDocument document = repository.getById(SLightDocument.class, 99L, 3L);
final SLightDocument document = repository.getById(SLightDocument.class, 99L);

// //then
assertThat(document.getFileName()).isEqualTo("myFile.pdf");
Expand All @@ -60,12 +60,12 @@ public void getDocumentWithContent_should_retrieve_previously_saved_document() {
// given
final byte[] binaryDocContent = "someBinaryContent".getBytes();
repository.add(
SDocument.builder().id(666L).tenantId(2L).hasContent(true).fileName("myFile.pdf")
SDocument.builder().id(666L).hasContent(true).fileName("myFile.pdf")
.mimeType("application/pdf").content(binaryDocContent)
.build());

//when
final SDocument document = repository.getById(SDocument.class, 666L, 2L);
final SDocument document = repository.getById(SDocument.class, 666L);

// //then
assertThat(document.getFileName()).isEqualTo("myFile.pdf");
Expand All @@ -76,13 +76,13 @@ public void getDocumentWithContent_should_retrieve_previously_saved_document() {
@Test
public void getDocumentMapping_should_retrieve_previously_saved_document_mapping() {
// given:
repository.add(SDocumentMapping.builder().id(14L).tenantId(4L)
repository.add(SDocumentMapping.builder().id(14L)
.name("myDocMapping").description("doc mapping description").documentId(111L)
.index(9).processInstanceId(987987987987L).version("2.0")
.build());

// when:
SDocumentMapping documentMapping = repository.getById(SDocumentMapping.class, 14L, 4L);
SDocumentMapping documentMapping = repository.getById(SDocumentMapping.class, 14L);

// then:
assertThat(documentMapping.getName()).isEqualTo("myDocMapping");
Expand All @@ -97,11 +97,11 @@ public void getDocumentMapping_should_retrieve_previously_saved_document_mapping
public void getSMappedDocumentOfProcessWithName_should_retrieve_previously_saved_mapped_document() {
// given:
final SLightDocument docContent = SLightDocument.builder()
.id(666L).tenantId(1L).hasContent(true).fileName("myFile.pdf").mimeType("application/pdf")
.id(666L).hasContent(true).fileName("myFile.pdf").mimeType("application/pdf")
.build();

repository.add(docContent);
repository.add(SMappedDocument.builder().id(14L).tenantId(1L)
repository.add(SMappedDocument.builder().id(14L)
.name("myMappedDocument").description("doc desc")
.index(-1).processInstanceId(444888444488844L).version("1.7")
.document(docContent)
Expand All @@ -125,11 +125,11 @@ public void getSMappedDocumentOfProcessWithName_should_retrieve_previously_saved
public void getSAMappedDocumentOfProcessWithName_should_retrieve_previously_saved_archived_mapped_document() {
// given:
final SLightDocument docContent = SLightDocument.builder()
.id(915L).tenantId(1L).hasContent(true).fileName("myFile.txt").mimeType("test/plain")
.id(915L).hasContent(true).fileName("myFile.txt").mimeType("test/plain")
.build();

repository.add(docContent);
repository.add(SAMappedDocument.builder().id(32L).tenantId(1L)
repository.add(SAMappedDocument.builder().id(32L)
.name("archivedMappedDoc").description("doc desc")
.index(-1).processInstanceId(PROCESS_INSTANCE_ID).version("1.7")
.document(docContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.MappedSuperclass;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.bonitasoft.engine.persistence.PersistentObject;
import org.bonitasoft.engine.persistence.PersistentObjectId;
import org.bonitasoft.engine.persistence.PlatformPersistentObject;

/**
* @author Emmanuel Duchastenier
Expand All @@ -33,11 +31,8 @@
@Data
@SuperBuilder
@MappedSuperclass
@IdClass(PersistentObjectId.class)
public class AbstractSDocument implements PersistentObject {
public class AbstractSDocument implements PlatformPersistentObject {

@Id
private long tenantId;
@Id
private long id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.MappedSuperclass;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.bonitasoft.engine.persistence.PersistentObject;
import org.bonitasoft.engine.persistence.PersistentObjectId;
import org.bonitasoft.engine.persistence.PlatformPersistentObject;

/**
* Mapping for a document
Expand All @@ -38,13 +36,10 @@
@AllArgsConstructor
@SuperBuilder
@MappedSuperclass
@IdClass(PersistentObjectId.class)
public class AbstractSDocumentMapping implements PersistentObject {
public class AbstractSDocumentMapping implements PlatformPersistentObject {

@Id
private long id;
@Id
private long tenantId;

@Column(name = "processinstanceid")
private long processInstanceId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;

Expand All @@ -37,9 +36,7 @@
public abstract class AbstractSMappedDocument extends AbstractSDocumentMapping {

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "tenantid", referencedColumnName = "tenantid", insertable = false, updatable = false),
@JoinColumn(name = "documentid", referencedColumnName = "id", insertable = false, updatable = false) })
@JoinColumn(name = "documentid", referencedColumnName = "id", insertable = false, updatable = false)
protected SLightDocument document;

public AbstractSMappedDocument(AbstractSDocumentMapping documentMapping, SDocument document) {
Expand All @@ -52,7 +49,6 @@ public AbstractSMappedDocument(AbstractSDocumentMapping documentMapping, SDocume
this.setIndex(documentMapping.getIndex());
this.document = SLightDocument.builder()
.id(document.getId())
.tenantId(document.getTenantId())
.fileName(document.getFileName())
.hasContent(document.hasContent())
.mimeType(document.getMimeType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public SMappedDocument(AbstractSDocumentMapping documentMapping, SDocument docum
this.setIndex(documentMapping.getIndex());
this.document = SLightDocument.builder()
.id(document.getId())
.tenantId(document.getTenantId())
.fileName(document.getFileName())
.hasContent(document.hasContent())
.mimeType(document.getMimeType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import lombok.experimental.SuperBuilder;
import org.bonitasoft.engine.core.document.model.AbstractSDocumentMapping;
import org.bonitasoft.engine.core.document.model.SDocumentMapping;
import org.bonitasoft.engine.persistence.ArchivedPersistentObject;
import org.bonitasoft.engine.persistence.ArchivedPlatformPersistentObject;
import org.bonitasoft.engine.persistence.PersistentObject;

@Data
Expand All @@ -35,7 +35,7 @@
@Entity
@Table(name = "arch_document_mapping")
@Cacheable(false)
public class SADocumentMapping extends AbstractSDocumentMapping implements ArchivedPersistentObject {
public class SADocumentMapping extends AbstractSDocumentMapping implements ArchivedPlatformPersistentObject {

public static final String ID = "id";
public static final String PROCESS_INSTANCE_ID = "processInstanceId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import lombok.experimental.SuperBuilder;
import org.bonitasoft.engine.core.document.model.AbstractSMappedDocument;
import org.bonitasoft.engine.core.document.model.SMappedDocument;
import org.bonitasoft.engine.persistence.ArchivedPersistentObject;
import org.bonitasoft.engine.persistence.ArchivedPlatformPersistentObject;
import org.bonitasoft.engine.persistence.PersistentObject;

/**
Expand All @@ -38,7 +38,7 @@
@Entity
@Table(name = "arch_document_mapping")
@Cacheable(false)
public class SAMappedDocument extends AbstractSMappedDocument implements ArchivedPersistentObject {
public class SAMappedDocument extends AbstractSMappedDocument implements ArchivedPlatformPersistentObject {

private long archiveDate;
private long sourceObjectId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bonitasoft.engine.persistence.ArchivedPersistentObject;
import org.bonitasoft.engine.persistence.ArchivedPlatformPersistentObject;
import org.bonitasoft.engine.persistence.PersistentObject;
import org.hibernate.annotations.Type;

Expand All @@ -31,7 +31,7 @@
@Builder
@Entity
@Table(name = "arch_bpm_failure")
public class SABPMFailure implements ArchivedPersistentObject {
public class SABPMFailure implements ArchivedPlatformPersistentObject {

@Id
private long id;
Expand Down Expand Up @@ -66,8 +66,4 @@ public Class<? extends PersistentObject> getPersistentObjectInterface() {
return SBPMFailure.class;
}

@Override
public void setTenantId(long id) {
// Ignore tenant id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,7 @@ CREATE TABLE process_definition (
);
ALTER TABLE process_definition ADD CONSTRAINT fk_process_definition_content_id FOREIGN KEY (content_id) REFERENCES process_content(id);

CREATE TABLE arch_document_mapping (
tenantid BIGINT NOT NULL,
id BIGINT NOT NULL,
sourceObjectId BIGINT,
processinstanceid BIGINT NOT NULL,
documentid BIGINT NOT NULL,
name VARCHAR(50) NOT NULL,
description TEXT,
version VARCHAR(50) NOT NULL,
index_ INT NOT NULL,
archiveDate BIGINT NOT NULL,
PRIMARY KEY (tenantid, id)
);
CREATE INDEX idx_a_doc_mp_pr_id ON arch_document_mapping (processinstanceid);
CREATE TABLE document (
tenantid BIGINT NOT NULL,
id BIGINT NOT NULL,
author BIGINT,
creationdate BIGINT NOT NULL,
Expand All @@ -147,19 +132,36 @@ CREATE TABLE document (
mimetype VARCHAR(255),
url VARCHAR(1024),
content LONGBLOB NULL,
PRIMARY KEY (tenantid, id)
CONSTRAINT pk_document PRIMARY KEY (id)
);

CREATE TABLE document_mapping (
tenantid BIGINT NOT NULL,
id BIGINT NOT NULL,
processinstanceid BIGINT NOT NULL,
documentid BIGINT NOT NULL,
name VARCHAR(50) NOT NULL,
description TEXT,
version VARCHAR(50) NOT NULL,
index_ INT NOT NULL,
PRIMARY KEY (tenantid, id)
CONSTRAINT pk_document_mapping PRIMARY KEY (id)
);
ALTER TABLE document_mapping ADD CONSTRAINT fk_document_mapping_documentid FOREIGN KEY (documentid) REFERENCES document(id) ON DELETE CASCADE;

CREATE TABLE arch_document_mapping (
id BIGINT NOT NULL,
sourceObjectId BIGINT,
processinstanceid BIGINT NOT NULL,
documentid BIGINT NOT NULL,
name VARCHAR(50) NOT NULL,
description TEXT,
version VARCHAR(50) NOT NULL,
index_ INT NOT NULL,
archiveDate BIGINT NOT NULL,
CONSTRAINT pk_arch_document_mapping PRIMARY KEY (id)
);
CREATE INDEX idx_a_doc_mp_pr_id ON arch_document_mapping (processinstanceid);
ALTER TABLE arch_document_mapping ADD CONSTRAINT fk_arch_document_mapping_documentid FOREIGN KEY (documentid) REFERENCES document(id) ON DELETE CASCADE;

CREATE TABLE arch_process_instance (
tenantid BIGINT NOT NULL,
id BIGINT NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
-- ------------------------------------------------ Foreign Keys -----------------------------------------------
ALTER TABLE connector_instance ADD CONSTRAINT fk_connector_instance_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE data_instance ADD CONSTRAINT fk_data_instance_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE document ADD CONSTRAINT fk_document_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE document_mapping ADD CONSTRAINT fk_document_mapping_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE document_mapping ADD CONSTRAINT fk_docmap_docid FOREIGN KEY (tenantid, documentid) REFERENCES document(tenantid, id) ON DELETE CASCADE;
ALTER TABLE pending_mapping ADD CONSTRAINT fk_pending_mapping_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE pending_mapping ADD CONSTRAINT fk_pending_mapping_flownode_instanceId FOREIGN KEY (activityId) REFERENCES flownode_instance(id);
ALTER TABLE multi_biz_data ADD CONSTRAINT fk_multi_biz_data_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE ref_biz_data_inst ADD CONSTRAINT fk_ref_biz_data_inst_tenantId FOREIGN KEY (tenantId) REFERENCES tenant(id);

-- ------------------------ Foreign Keys to disable if archiving is on another BD ------------------
ALTER TABLE arch_document_mapping ADD CONSTRAINT fk_arch_document_mapping_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE arch_document_mapping ADD CONSTRAINT fk_archdocmap_docid FOREIGN KEY (tenantid, documentid) REFERENCES document(tenantid, id) ON DELETE CASCADE;
ALTER TABLE arch_flownode_instance ADD CONSTRAINT fk_arch_flownode_instance_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE arch_process_instance ADD CONSTRAINT fk_arch_process_instance_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
ALTER TABLE arch_data_instance ADD CONSTRAINT fk_arch_data_instance_tenantId FOREIGN KEY (tenantid) REFERENCES tenant(id);
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ ALTER TABLE profilemember DROP CONSTRAINT fk_profilemember_profileid;

ALTER TABLE connector_instance DROP CONSTRAINT fk_connector_instance_tenantId;
ALTER TABLE data_instance DROP CONSTRAINT fk_data_instance_tenantId;
ALTER TABLE document DROP CONSTRAINT fk_document_tenantId;
ALTER TABLE document_mapping DROP CONSTRAINT fk_document_mapping_tenantId;
ALTER TABLE document_mapping DROP CONSTRAINT fk_docmap_docid;
ALTER TABLE document_mapping DROP CONSTRAINT fk_document_mapping_documentid;
ALTER TABLE pending_mapping DROP CONSTRAINT fk_pending_mapping_tenantId;
ALTER TABLE pending_mapping DROP CONSTRAINT fk_pending_mapping_flownode_instanceId;
ALTER TABLE process_definition DROP CONSTRAINT fk_process_definition_content_id;
Expand All @@ -29,8 +27,7 @@ ALTER TABLE business_app DROP CONSTRAINT fk_business_app_themeid;


-- ------------------------ Foreign Keys to disable if archiving is on another BD ------------------
ALTER TABLE arch_document_mapping DROP CONSTRAINT fk_arch_document_mapping_tenantId;
ALTER TABLE arch_document_mapping DROP CONSTRAINT fk_archdocmap_docid;
ALTER TABLE arch_document_mapping DROP CONSTRAINT fk_arch_document_mapping_documentid;
ALTER TABLE arch_flownode_instance DROP CONSTRAINT fk_arch_flownode_instance_tenantId;
ALTER TABLE arch_process_instance DROP CONSTRAINT fk_arch_process_instance_tenantId;
ALTER TABLE arch_data_instance DROP CONSTRAINT fk_arch_data_instance_tenantId;
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,7 @@ CREATE TABLE process_definition (
);
ALTER TABLE process_definition ADD CONSTRAINT fk_process_definition_content_id FOREIGN KEY (content_id) REFERENCES process_content(id);

CREATE TABLE arch_document_mapping (
tenantid INT8 NOT NULL,
id INT8 NOT NULL,
sourceObjectId INT8,
processinstanceid INT8 NOT NULL,
documentid INT8 NOT NULL,
name VARCHAR(50) NOT NULL,
description TEXT,
version VARCHAR(50) NOT NULL,
index_ INT NOT NULL,
archiveDate INT8 NOT NULL,
PRIMARY KEY (tenantid, ID)
);
CREATE INDEX idx_a_doc_mp_pr_id ON arch_document_mapping (processinstanceid);
CREATE TABLE document (
tenantid INT8 NOT NULL,
id INT8 NOT NULL,
author INT8,
creationdate INT8 NOT NULL,
Expand All @@ -147,19 +132,36 @@ CREATE TABLE document (
mimetype VARCHAR(255),
url VARCHAR(1024),
content BYTEA,
PRIMARY KEY (tenantid, id)
CONSTRAINT pk_document PRIMARY KEY (id)
);

CREATE TABLE document_mapping (
tenantid INT8 NOT NULL,
id INT8 NOT NULL,
processinstanceid INT8 NOT NULL,
documentid INT8 NOT NULL,
name VARCHAR(50) NOT NULL,
description TEXT,
version VARCHAR(50) NOT NULL,
index_ INT NOT NULL,
PRIMARY KEY (tenantid, ID)
CONSTRAINT pk_document_mapping PRIMARY KEY (id)
);
ALTER TABLE document_mapping ADD CONSTRAINT fk_document_mapping_documentid FOREIGN KEY (documentid) REFERENCES document(id) ON DELETE CASCADE;

CREATE TABLE arch_document_mapping (
id INT8 NOT NULL,
sourceObjectId INT8,
processinstanceid INT8 NOT NULL,
documentid INT8 NOT NULL,
name VARCHAR(50) NOT NULL,
description TEXT,
version VARCHAR(50) NOT NULL,
index_ INT NOT NULL,
archiveDate INT8 NOT NULL,
CONSTRAINT pk_arch_document_mapping PRIMARY KEY (id)
);
CREATE INDEX idx_a_doc_mp_pr_id ON arch_document_mapping (processinstanceid);
ALTER TABLE arch_document_mapping ADD CONSTRAINT fk_arch_document_mapping_documentid FOREIGN KEY (documentid) REFERENCES document(id) ON DELETE CASCADE;

CREATE TABLE arch_process_instance (
tenantid INT8 NOT NULL,
id INT8 NOT NULL,
Expand Down
Loading

0 comments on commit 30ac8f3

Please sign in to comment.