260330:1011 Addied correspondence_revieion_attcahments table table #01
CI / CD Pipeline / build (push) Failing after 21m19s
CI / CD Pipeline / deploy (push) Has been skipped

This commit is contained in:
admin
2026-03-30 10:11:40 +07:00
parent 1c6fec6c65
commit c83588ab43
287 changed files with 15117 additions and 69 deletions
@@ -78,8 +78,8 @@ erDiagram
shop_drawing_sub_categories ||--o{ shop_drawings : "sub_categorizes"
%% Attachments
attachments ||--o{ correspondence_attachments : "attached_to"
correspondences ||--o{ correspondence_attachments : "has"
attachments ||--o{ correspondence_revision_attachments : "attached_to"
correspondence_revisions ||--o{ correspondence_revision_attachments : "has"
```
---
@@ -357,7 +357,7 @@ erDiagram
**Relationships**:
- Parent: correspondence_types, **disciplines**, projects, organizations, users
- Children: correspondence_revisions, correspondence_recipients, correspondence_tags, correspondence_references, correspondence_attachments, circulations, transmittals
- Children: correspondence_revisions, correspondence_recipients, correspondence_tags, correspondence_references, circulations, transmittals
---
@@ -1386,7 +1386,7 @@ erDiagram
**Relationships**:
- Parent: users
- Referenced by: correspondence_attachments, circulation_attachments, shop_drawing_revision_attachments, contract_drawing_attachments
- Referenced by: correspondence_revision_attachments, circulation_attachments, shop_drawing_revision_attachments, contract_drawing_attachments
**Business Rules**:
@@ -1398,33 +1398,37 @@ erDiagram
---
### 8.2 correspondence_attachments
### 8.2 correspondence_revision_attachments (UPDATE v1.8.1)
**Purpose**: Junction table linking correspondences to file attachments (M:N)
**Purpose**: Junction table linking **correspondence_revisions** to file attachments (M:N)
| Column Name | Data Type | Constraints | Description |
| ----------------- | --------- | --------------- | ---------------------------- |
| correspondence_id | INT | PRIMARY KEY, FK | Reference to correspondences |
| attachment_id | INT | PRIMARY KEY, FK | Reference to attachments |
| is_main_document | BOOLEAN | DEFAULT FALSE | Main/primary document flag |
> **[FIX v1.8.1]** เปลี่ยนจาก `correspondence_attachments` (FK → `correspondences.id`) → `correspondence_revision_attachments` (FK → `correspondence_revisions.id`)
> เหตุผล: ไฟล์แนบผูกกับ revision ไม่ใช่ correspondence master เพราะแต่ละ revision อาจมีไฟล์แนบต่างกัน
| Column Name | Data Type | Constraints | Description |
| ---------------------------- | --------- | --------------- | --------------------------------------- |
| correspondence_revision_id | INT | PRIMARY KEY, FK | Reference to correspondence_revisions |
| attachment_id | INT | PRIMARY KEY, FK | Reference to attachments |
| is_main_document | BOOLEAN | DEFAULT FALSE | Main/primary document flag of revision |
**Indexes**:
- PRIMARY KEY (correspondence_id, attachment_id)
- FOREIGN KEY (correspondence_id) REFERENCES correspondences(id) ON DELETE CASCADE
- PRIMARY KEY (correspondence_revision_id, attachment_id)
- FOREIGN KEY (correspondence_revision_id) REFERENCES correspondence_revisions(id) ON DELETE CASCADE
- FOREIGN KEY (attachment_id) REFERENCES attachments(id) ON DELETE CASCADE
- INDEX (attachment_id)
- INDEX (is_main_document)
- INDEX idx_corr_rev_att_revision (correspondence_revision_id)
**Relationships**:
- Parent: correspondences, attachments
- Parent: correspondence_revisions, attachments
- correspondence_revisions has `attachmentLinks` OneToMany relation
**Business Rules**:
- One correspondence can have multiple attachments
- One attachment can be linked to multiple correspondences
- is_main_document identifies primary file (typically PDF)
- One revision can have multiple attachments
- One attachment can be linked to multiple revisions
- is_main_document identifies primary file of that revision (typically PDF)
- เมื่อ create/update correspondence → commit attachments → บันทึก revision_id ลงตารางนี้
---
@@ -1,4 +1,4 @@
-- ==========================================================
-- ==========================================================
-- DMS v1.8.0 Schema Part 1/3: DROP Statements
-- รันไฟล์นี้ก่อน เพื่อล้างตารางเดิมทั้งหมด
-- ==========================================================
@@ -98,7 +98,7 @@ DROP TABLE IF EXISTS shop_drawing_revision_attachments;
DROP TABLE IF EXISTS asbuilt_drawing_revision_attachments;
DROP TABLE IF EXISTS correspondence_attachments;
DROP TABLE IF EXISTS correspondence_revision_attachments;
DROP TABLE IF EXISTS attachments;
@@ -848,15 +848,18 @@ CREATE TABLE attachments (
UNIQUE INDEX idx_attachments_uuid (uuid)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'ตาราง "กลาง" เก็บไฟล์แนบทั้งหมดของระบบ';
-- ตารางเชื่อม correspondences กับ attachments (M:N)
CREATE TABLE correspondence_attachments (
correspondence_id INT COMMENT 'ID ของเอกสาร',
attachment_id INT COMMENT 'ID ของไฟล์แนบ',
is_main_document BOOLEAN DEFAULT FALSE COMMENT '(1 = ไฟล์หลัก)',
PRIMARY KEY (correspondence_id, attachment_id),
FOREIGN KEY (correspondence_id) REFERENCES correspondences (id) ON DELETE CASCADE,
FOREIGN KEY (attachment_id) REFERENCES attachments (id) ON DELETE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'ตารางเชื่อม correspondences กับ attachments (M :N)';
-- ตารางเชื่อม correspondence_revisions กับ attachments (M:N)
-- [FIX] FK เปลี่ยนจาก correspondences.id → correspondence_revisions.id
-- เหตุผล: ไฟล์แนบผูกกับ revision ไม่ใช่ correspondence master (แต่ละ revision มีไฟล์ต่างกัน)
CREATE TABLE correspondence_revision_attachments (
correspondence_revision_id INT NOT NULL COMMENT 'ID ของ Revision ที่ผูกไฟล์แนบนี้',
attachment_id INT NOT NULL COMMENT 'ID ของไฟล์แนบ',
is_main_document BOOLEAN DEFAULT FALSE COMMENT '(1 = ไฟล์หลักของ Revision นี้)',
PRIMARY KEY (correspondence_revision_id, attachment_id),
FOREIGN KEY (correspondence_revision_id) REFERENCES correspondence_revisions (id) ON DELETE CASCADE,
FOREIGN KEY (attachment_id) REFERENCES attachments (id) ON DELETE CASCADE,
INDEX idx_corr_rev_att_revision (correspondence_revision_id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'ตารางเชื่อม correspondence_revisions กับ attachments (M:N)';
-- ตารางเชื่อม circulations กับ attachments (M:N)
CREATE TABLE circulation_attachments (
@@ -1,4 +1,4 @@
-- ==========================================================
-- ==========================================================
-- DMS v1.8.0 Schema Part 3/3: Views, Indexes, Partitioning
-- รัน: หลังจาก 02-schema-tables.sql เสร็จ
-- ==========================================================
@@ -397,16 +397,20 @@ SELECT 'CORRESPONDENCE' AS document_type,
p.uuid AS project_uuid,
p.project_code,
p.project_name,
COUNT(ca.attachment_id) AS attachment_count,
COUNT(cra.attachment_id) AS attachment_count,
MAX(a.created_at) AS latest_attachment_date
FROM correspondences c
INNER JOIN projects p ON c.project_id = p.id
LEFT JOIN correspondence_attachments ca ON c.id = ca.correspondence_id
LEFT JOIN attachments a ON ca.attachment_id = a.id
INNER JOIN projects p ON c.project_id = p.id -- [FIX] JOIN ผ่าน correspondence_revisions เพราะไฟล์ผูกกับ revision ไม่ใช่ correspondence master
LEFT JOIN correspondence_revisions cr ON c.id = cr.correspondence_id
AND cr.is_current = TRUE
LEFT JOIN correspondence_revision_attachments cra ON cr.id = cra.correspondence_revision_id
LEFT JOIN attachments a ON cra.attachment_id = a.id
WHERE c.deleted_at IS NULL
GROUP BY c.id,
c.uuid,
c.correspondence_number,
c.project_id,
p.uuid,
p.project_code,
p.project_name
UNION ALL