Objective
Implement version control for documents with change history tracking. This enables document revision management, audit trails, and collaborative document editing.
Step 1: Create Documents Table
Create a table for versioned documents.
CREATE TABLE versioned_documents (
id INTEGER PRIMARY KEY,
document_code VARCHAR(50) NOT NULL UNIQUE,
title VARCHAR(255) NOT NULL,
category VARCHAR(50),
current_version INTEGER DEFAULT 1,
total_versions INTEGER DEFAULT 1,
status VARCHAR(20) DEFAULT 'draft',
owner VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Create Versions Table
Create a table for version history.
CREATE TABLE document_versions (
id INTEGER PRIMARY KEY,
document_id INTEGER NOT NULL,
version_number INTEGER NOT NULL,
document_file PDF,
file_size BIGINT,
page_count INTEGER,
change_summary TEXT,
change_type VARCHAR(20) DEFAULT 'update',
created_by VARCHAR(100) NOT NULL,
approved_by VARCHAR(100),
approval_date TIMESTAMP,
is_current BOOLEAN DEFAULT FALSE,
is_published BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (document_id) REFERENCES versioned_documents(id)
);
Step 3: Create Change Log Table
Track detailed changes.
CREATE TABLE version_changes (
id INTEGER PRIMARY KEY,
version_id INTEGER NOT NULL,
section VARCHAR(100),
change_description TEXT NOT NULL,
change_author VARCHAR(100),
changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (version_id) REFERENCES document_versions(id)
);
Step 4: Insert Sample Documents
Add documents for versioning.
INSERT INTO versioned_documents (id, document_code, title, category, current_version, total_versions, status, owner) VALUES
(1, 'DOC-001', 'Product Requirements Document', 'product', 4, 4, 'approved', 'product_manager'),
(2, 'DOC-002', 'System Architecture Specification', 'technical', 3, 3, 'approved', 'tech_lead'),
(3, 'DOC-003', 'Employee Handbook', 'hr', 2, 2, 'published', 'hr_director'),
(4, 'DOC-004', 'Security Policy', 'security', 5, 5, 'approved', 'security_officer'),
(5, 'DOC-005', 'API Documentation', 'technical', 2, 2, 'draft', 'api_team');
Step 5: Insert Version History
Add version records.
INSERT INTO document_versions (id, document_id, version_number, file_size, page_count, change_summary, change_type, created_by, approved_by, approval_date, is_current, is_published) VALUES
-- Product Requirements versions
(1, 1, 1, 512000, 12, 'Initial draft of product requirements', 'create', 'pm_john', NULL, NULL, FALSE, FALSE),
(2, 1, 2, 645000, 15, 'Added user stories and acceptance criteria', 'major', 'pm_john', 'director_mary', '2024-01-10 14:00:00', FALSE, FALSE),
(3, 1, 3, 720000, 18, 'Incorporated stakeholder feedback', 'major', 'pm_john', 'director_mary', '2024-01-20 10:00:00', FALSE, FALSE),
(4, 1, 4, 780000, 20, 'Final review and formatting updates', 'minor', 'pm_john', 'director_mary', '2024-01-25 16:00:00', TRUE, TRUE),
-- Architecture Spec versions
(5, 2, 1, 1024000, 28, 'Initial architecture design', 'create', 'arch_sam', NULL, NULL, FALSE, FALSE),
(6, 2, 2, 1280000, 35, 'Added microservices details', 'major', 'arch_sam', 'cto', '2024-02-01 11:00:00', FALSE, FALSE),
(7, 2, 3, 1350000, 38, 'Security review updates', 'minor', 'arch_sam', 'cto', '2024-02-10 15:00:00', TRUE, TRUE),
-- Employee Handbook versions
(8, 3, 1, 2048000, 45, 'Initial handbook release', 'create', 'hr_jane', 'hr_director', '2023-06-01 09:00:00', FALSE, TRUE),
(9, 3, 2, 2200000, 48, 'Updated remote work policies', 'major', 'hr_jane', 'hr_director', '2024-01-05 10:00:00', TRUE, TRUE),
-- Security Policy versions
(10, 4, 1, 450000, 10, 'Initial security policy', 'create', 'sec_admin', 'ciso', '2023-01-15 14:00:00', FALSE, FALSE),
(11, 4, 2, 520000, 12, 'Added cloud security section', 'major', 'sec_admin', 'ciso', '2023-04-20 11:00:00', FALSE, FALSE),
(12, 4, 3, 580000, 14, 'Updated compliance requirements', 'major', 'sec_admin', 'ciso', '2023-08-10 16:00:00', FALSE, FALSE),
(13, 4, 4, 620000, 15, 'Added incident response procedures', 'major', 'sec_admin', 'ciso', '2023-11-15 10:00:00', FALSE, TRUE),
(14, 4, 5, 650000, 16, 'Annual review and updates', 'minor', 'sec_admin', 'ciso', '2024-01-30 14:00:00', TRUE, TRUE);