Requirements
- MySQL Version: 8.0 or higher
- Storage Engine: InnoDB (default)
- Character Set: utf8mb4
- Collation: utf8mb4_unicode_ci
- Timezone: UTC (recommended)
MySQL 8 is required for proper JSON support and modern security features.
Database Creation
Schema Overview
The CCDigital database schema consists of core tables, views, stored procedures, and triggers.Core Tables
Identity and Access
persons
Central table storing citizen identity information.Key Fields:
id: Primary keyid_type: Document type (CC, TI, etc.)id_number: National ID numberfirst_name,last_name: Personal namesemail: Contact emailcreated_at,updated_at: Timestamps
users
Application users linked to persons. Supports multiple user types (government, issuer, end-user).Key Fields:
id: Primary keyperson_id: Foreign key topersonsusername: Login usernamepassword: Hashed passwordrole: User role (ROLE_GOBIERNO, ROLE_ISSUER, ROLE_USER)access_state: User access state (ENABLED, SUSPENDED, DISABLED)totp_enabled,totp_secret: TOTP configuration
entities
Organizations that can request document access (issuers).Key Fields:
id: Primary keyname: Entity nameentity_type: Type of organizationnit: Tax identification number
entity_users
Links users to entities for issuer role management.Key Fields:
entity_id: Foreign key toentitiesuser_id: Foreign key tousers
Document Management
documents
Document type definitions (birth certificates, diplomas, etc.).Key Fields:
id: Primary keyname: Document type namedescription: Document descriptioncategory_id: Foreign key tocategories
categories
Document categories for classification.Key Fields:
id: Primary keyname: Category namedescription: Category description
person_documents
Links specific document instances to persons.Key Fields:
id: Primary keyperson_id: Foreign key topersonsdocument_id: Foreign key todocumentsreview_status: Document approval status (PENDING, APPROVED, REJECTED)uploaded_at: Upload timestampreviewed_at: Review timestampreviewed_by: Admin user who reviewedblockchain_ref: Reference to Fabric ledger entry
files
Physical file metadata for stored documents.Key Fields:
id: Primary keyperson_document_id: Foreign key toperson_documentsstorage_path: Relative path from base directorysha256: File integrity hashsize_bytes: File sizeversion: Auto-incremented version (via trigger)uploaded_at: Upload timestamp
Access Control
access_requests
Document access requests from entities to persons.Key Fields:
id: Primary keyentity_id: Foreign key toentities(requester)person_id: Foreign key topersons(document owner)status: Request status (PENDING, APPROVED, REJECTED)purpose: Access justificationvalid_from,valid_until: Access validity windowcreated_at: Request creation timestamp
access_request_items
Individual documents included in an access request.Key Fields:
id: Primary keyaccess_request_id: Foreign key toaccess_requestsperson_document_id: Foreign key toperson_documents
consents
User consent records for access grants.Key Fields:
id: Primary keyaccess_request_id: Foreign key toaccess_requestsgranted: Boolean consent flaggranted_at: Consent timestamp
Audit and Traceability
audit_events
System audit trail for all sensitive operations.Key Fields:
id: Primary keyevent_type: Type of event (LOGIN, DOCUMENT_ACCESS, STATUS_CHANGE, etc.)user_id: Foreign key tousers(actor)person_id: Foreign key topersons(subject)details: JSON event detailsip_address: Source IPcreated_at: Event timestamp
Company Management
companies
Organization definitions for document templates.Key Fields:
id: Primary keyname: Company namenit: Tax identification
company_document_definitions
Document templates defined by companies.
entity_document_definitions
Document templates accessible by entities.
Database Views
CCDigital includes optimized views for common queries:v_documents
Consolidated view of all documents with category information.Purpose: Simplifies document listing and filtering queries.
v_person_full_documents
Complete document information joined with person data.Purpose: Provides full context for person-document relationships including review status, files, and blockchain references.
Stored Procedures
The database includes stored procedures for complex operations:sp_add_person_document
Adds a new document association for a person.Parameters:
p_person_id: Person IDp_document_id: Document type IDp_uploaded_by: User ID of uploader
sp_create_user_with_person
Creates a new user account linked to an existing person.Parameters:
p_person_id: Existing person IDp_username: Login usernamep_password_hash: Pre-hashed passwordp_role: User role
sp_upload_file_path
Records file metadata for a document using file path.Parameters:
p_person_document_id: Person document IDp_storage_path: Relative file pathp_sha256: File hashp_size_bytes: File size
FileStorageService to persist file metadata.sp_upload_pdf_blob
Stores PDF file as BLOB (legacy support).Note: Current implementation uses filesystem storage via
sp_upload_file_path.Triggers
trg_files_autoversion
Table:
Event: BEFORE INSERTPurpose: Automatically increments file version number for the same
filesEvent: BEFORE INSERTPurpose: Automatically increments file version number for the same
person_document_id.Behavior:- Queries maximum existing version for the document
- Sets new file version to
MAX(version) + 1 - Enables automatic file versioning without application logic
Schema Relationships
Key foreign key relationships:Importing Database Backup
If you have a database backup file, import it using MySQL command line:The backup should include schema definitions (tables, views, procedures, triggers) and optionally seed data.
JPA/Hibernate Configuration
CCDigital uses Spring Data JPA with Hibernate for database access.Connection Configuration
Database connection is configured via environment variables:Schema Management
Hibernate schema management modes:none(production): No automatic schema changesvalidate: Validates entity mappings match schemaupdate(development only): Auto-updates schemacreate(testing only): Drops and recreates schema
SQL Logging
For debugging, enable SQL logging:Disable SQL logging in production for performance and security.
Entity Mapping
All database tables are mapped to JPA entities in:Person.java→personsUser.java→usersPersonDocument.java→person_documentsFileRecord.java→filesAccessRequest.java→access_requestsEntity.java→entitiesAuditEvent.java→audit_events
Database Maintenance
Backups
Create regular backups usingmysqldump:
Monitoring
Monitor key database metrics:- Connection pool usage
- Slow query log
- Table sizes and growth
- Index usage
- Replication lag (if using replication)
Optimization
Regularly analyze and optimize tables:Connection Pooling
Spring Boot uses HikariCP as the default connection pool. Monitor pool health:- Active connections
- Idle connections
- Connection wait time
- Connection acquisition timeout
Troubleshooting
Connection Refused
Problem: Application cannot connect to MySQL. Solutions:- Verify MySQL is running:
systemctl status mysql - Check firewall rules allow port 3306
- Verify
DB_URLuses correct host and port - Test connection manually:
mysql -h <HOST> -u <USER> -p
Access Denied
Problem: Authentication fails for database user. Solutions:- Verify username and password in
DB_USERNAMEandDB_PASSWORD - Check user grants:
SHOW GRANTS FOR 'ccdigital_user'@'localhost'; - Ensure user has privileges:
GRANT ALL PRIVILEGES ON ccdigital.* TO 'ccdigital_user'@'localhost';
Slow Queries
Problem: Database queries taking too long. Solutions:- Enable slow query log in MySQL configuration
- Analyze slow queries with
EXPLAIN - Add missing indexes
- Review JPA fetch strategies (avoid N+1 queries)
Disk Space
Problem: Database disk space exhausted. Solutions:- Archive old
audit_eventsrecords - Purge binary logs:
PURGE BINARY LOGS BEFORE '2026-01-01'; - Optimize tables to reclaim space
- Monitor
filestable growth (file metadata)
