|
| 1 | +# OpenESPI Database Initialization System |
| 2 | + |
| 3 | +This document describes the comprehensive database initialization system for OpenESPI that works with Flyway migrations and Spring Boot 3.5 profiles. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The OpenESPI database initialization system provides: |
| 8 | + |
| 9 | +- **Multi-database support**: MySQL and PostgreSQL |
| 10 | +- **Environment-specific configurations**: Development, Test, Production |
| 11 | +- **Schema separation**: `openespi_usage`, `openespi_customer`, `openespi_tokenstore` |
| 12 | +- **ESPI-compliant seed data**: Realistic Green Button test data |
| 13 | +- **Flyway integration**: Compatible with existing migration scripts |
| 14 | +- **Security**: Role-based database users with appropriate permissions |
| 15 | + |
| 16 | +## Directory Structure |
| 17 | + |
| 18 | +``` |
| 19 | +etc/ |
| 20 | +├── init/ # Database and user creation scripts |
| 21 | +│ ├── mysql/ |
| 22 | +│ │ ├── 01_create_databases.sql |
| 23 | +│ │ └── 02_create_users.sql |
| 24 | +│ └── postgresql/ |
| 25 | +│ ├── 01_create_databases.sql |
| 26 | +│ └── 02_create_users.sql |
| 27 | +├── seed/ # ESPI-compliant test data |
| 28 | +│ ├── mysql/ |
| 29 | +│ │ ├── 01_seed_application_information.sql |
| 30 | +│ │ ├── 02_seed_retail_customers.sql |
| 31 | +│ │ └── 03_seed_usage_points_and_readings.sql |
| 32 | +│ └── postgresql/ |
| 33 | +│ └── 01_seed_application_information.sql |
| 34 | +├── env/ # Environment-specific scripts |
| 35 | +│ └── dev/ |
| 36 | +│ ├── init-mysql-dev.sh |
| 37 | +│ └── init-postgresql-dev.sh |
| 38 | +├── cleanup/ # Cleanup and rollback scripts |
| 39 | +│ ├── mysql/ |
| 40 | +│ │ ├── cleanup_databases.sql |
| 41 | +│ │ └── cleanup_data_only.sql |
| 42 | +│ └── postgresql/ |
| 43 | +│ └── cleanup_databases.sql |
| 44 | +└── DATABASE_INITIALIZATION.md # This documentation |
| 45 | +``` |
| 46 | + |
| 47 | +## Database Schema Design |
| 48 | + |
| 49 | +### Schema Separation |
| 50 | + |
| 51 | +The system uses three separate schemas: |
| 52 | + |
| 53 | +1. **openespi_usage**: Energy usage data (Usage Points, Meter Readings, Interval Readings) |
| 54 | +2. **openespi_customer**: Customer information and device data |
| 55 | +3. **openespi_tokenstore**: OAuth 2.0 token storage |
| 56 | + |
| 57 | +### Database Users |
| 58 | + |
| 59 | +| User | Purpose | Permissions | |
| 60 | +|------|---------|-------------| |
| 61 | +| `openespi_dev` | Development | Full access to all schemas | |
| 62 | +| `openespi_prod` | Production | SELECT, INSERT, UPDATE, DELETE | |
| 63 | +| `openespi_readonly` | Analytics/Reporting | SELECT only | |
| 64 | +| `openespi_migrate` | Flyway migrations | Full schema management | |
| 65 | + |
| 66 | +## Quick Start |
| 67 | + |
| 68 | +### Development Environment |
| 69 | + |
| 70 | +#### MySQL |
| 71 | +```bash |
| 72 | +# Initialize MySQL development environment |
| 73 | +./etc/env/dev/init-mysql-dev.sh |
| 74 | + |
| 75 | +# With custom parameters |
| 76 | +./etc/env/dev/init-mysql-dev.sh --host localhost --port 3306 --user root --password mypassword |
| 77 | +``` |
| 78 | + |
| 79 | +#### PostgreSQL |
| 80 | +```bash |
| 81 | +# Initialize PostgreSQL development environment |
| 82 | +./etc/env/dev/init-postgresql-dev.sh |
| 83 | + |
| 84 | +# With custom parameters |
| 85 | +./etc/env/dev/init-postgresql-dev.sh --host localhost --port 5432 --user postgres --password mypassword |
| 86 | +``` |
| 87 | + |
| 88 | +## Manual Setup Process |
| 89 | + |
| 90 | +### 1. Database Creation |
| 91 | + |
| 92 | +#### MySQL |
| 93 | +```bash |
| 94 | +mysql -u root -p < etc/init/mysql/01_create_databases.sql |
| 95 | +mysql -u root -p < etc/init/mysql/02_create_users.sql |
| 96 | +``` |
| 97 | + |
| 98 | +#### PostgreSQL |
| 99 | +```bash |
| 100 | +psql -U postgres -f etc/init/postgresql/01_create_databases.sql |
| 101 | +psql -U postgres -f etc/init/postgresql/02_create_users.sql |
| 102 | +``` |
| 103 | + |
| 104 | +### 2. Schema Creation (Flyway) |
| 105 | + |
| 106 | +```bash |
| 107 | +# MySQL |
| 108 | +mvn flyway:migrate -Dspring.profiles.active=dev-mysql -Dflyway.locations=classpath:db/migration/mysql |
| 109 | + |
| 110 | +# PostgreSQL |
| 111 | +mvn flyway:migrate -Dspring.profiles.active=dev-postgresql -Dflyway.locations=classpath:db/migration/postgresql |
| 112 | +``` |
| 113 | + |
| 114 | +### 3. Seed Data Population |
| 115 | + |
| 116 | +#### MySQL |
| 117 | +```bash |
| 118 | +mysql -u root -p < etc/seed/mysql/01_seed_application_information.sql |
| 119 | +mysql -u root -p < etc/seed/mysql/02_seed_retail_customers.sql |
| 120 | +mysql -u root -p < etc/seed/mysql/03_seed_usage_points_and_readings.sql |
| 121 | +``` |
| 122 | + |
| 123 | +#### PostgreSQL |
| 124 | +```bash |
| 125 | +psql -U postgres -f etc/seed/postgresql/01_seed_application_information.sql |
| 126 | +``` |
| 127 | + |
| 128 | +## Spring Boot 3.5 Integration |
| 129 | + |
| 130 | +### Database Profiles |
| 131 | + |
| 132 | +The initialization system supports the following Spring Boot profiles: |
| 133 | + |
| 134 | +- `dev-mysql`: Development with MySQL |
| 135 | +- `dev-postgresql`: Development with PostgreSQL |
| 136 | +- `test-mysql`: Testing with MySQL |
| 137 | +- `test-postgresql`: Testing with PostgreSQL |
| 138 | +- `prod-mysql`: Production with MySQL |
| 139 | +- `prod-postgresql`: Production with PostgreSQL |
| 140 | + |
| 141 | +### Application Properties |
| 142 | + |
| 143 | +#### MySQL Development (application-dev-mysql.properties) |
| 144 | +```properties |
| 145 | +spring.datasource.url=jdbc:mysql://localhost:3306/openespi_usage |
| 146 | +spring.datasource.username=openespi_dev |
| 147 | +spring.datasource.password=openespi_dev_password |
| 148 | +spring.jpa.hibernate.ddl-auto=none |
| 149 | +spring.flyway.enabled=true |
| 150 | +spring.flyway.locations=classpath:db/migration/mysql |
| 151 | +``` |
| 152 | + |
| 153 | +#### PostgreSQL Development (application-dev-postgresql.properties) |
| 154 | +```properties |
| 155 | +spring.datasource.url=jdbc:postgresql://localhost:5432/openespi_usage |
| 156 | +spring.datasource.username=openespi_dev |
| 157 | +spring.datasource.password=openespi_dev_password |
| 158 | +spring.jpa.hibernate.ddl-auto=none |
| 159 | +spring.flyway.enabled=true |
| 160 | +spring.flyway.locations=classpath:db/migration/postgresql |
| 161 | +``` |
| 162 | + |
| 163 | +## ESPI-Compliant Test Data |
| 164 | + |
| 165 | +The seed data includes: |
| 166 | + |
| 167 | +### Application Information |
| 168 | +- 5 sample third-party applications with OAuth 2.0 configurations |
| 169 | +- Different application types: web, native, mobile |
| 170 | +- Realistic redirect URIs and client credentials |
| 171 | + |
| 172 | +### Retail Customers |
| 173 | +- 10 diverse customer profiles: |
| 174 | + - Administrative user (Data Custodian) |
| 175 | + - High usage family home |
| 176 | + - Medium usage apartment |
| 177 | + - Low usage efficient home |
| 178 | + - Small business |
| 179 | + - Solar customer with net metering |
| 180 | + - Electric vehicle owner |
| 181 | + - Time-of-use customer |
| 182 | + - Multi-unit building manager |
| 183 | + - Test customer for development |
| 184 | + |
| 185 | +### Usage Points and Readings |
| 186 | +- Time configurations for different time zones |
| 187 | +- Service delivery points with tariff information |
| 188 | +- Reading types for energy and power measurements |
| 189 | +- Usage points linked to customers |
| 190 | +- Meter readings with realistic consumption patterns |
| 191 | + |
| 192 | +## Environment-Specific Configurations |
| 193 | + |
| 194 | +### Development |
| 195 | +- Full access permissions |
| 196 | +- Comprehensive seed data |
| 197 | +- Debug logging enabled |
| 198 | +- Local database connections |
| 199 | + |
| 200 | +### Test |
| 201 | +- Isolated test databases |
| 202 | +- Minimal seed data |
| 203 | +- Test-specific configurations |
| 204 | +- In-memory or containerized databases |
| 205 | + |
| 206 | +### Production |
| 207 | +- Restricted user permissions |
| 208 | +- No seed data (production data only) |
| 209 | +- Security-hardened configurations |
| 210 | +- External database connections |
| 211 | + |
| 212 | +## Security Considerations |
| 213 | + |
| 214 | +### Password Management |
| 215 | +- Default passwords are provided for development only |
| 216 | +- **MUST** be changed for production environments |
| 217 | +- Use environment variables or secrets management |
| 218 | + |
| 219 | +### User Permissions |
| 220 | +- Principle of least privilege |
| 221 | +- Separate users for different purposes |
| 222 | +- Read-only access for analytics |
| 223 | + |
| 224 | +### Network Security |
| 225 | +- Database users restricted by host when possible |
| 226 | +- SSL/TLS connections recommended for production |
| 227 | +- Firewall rules for database access |
| 228 | + |
| 229 | +## Cleanup and Rollback |
| 230 | + |
| 231 | +### Complete Cleanup |
| 232 | +```bash |
| 233 | +# MySQL |
| 234 | +mysql -u root -p < etc/cleanup/mysql/cleanup_databases.sql |
| 235 | + |
| 236 | +# PostgreSQL |
| 237 | +psql -U postgres -f etc/cleanup/postgresql/cleanup_databases.sql |
| 238 | +``` |
| 239 | + |
| 240 | +### Data-Only Cleanup (Preserve Structure) |
| 241 | +```bash |
| 242 | +# MySQL |
| 243 | +mysql -u root -p < etc/cleanup/mysql/cleanup_data_only.sql |
| 244 | +``` |
| 245 | + |
| 246 | +## Troubleshooting |
| 247 | + |
| 248 | +### Common Issues |
| 249 | + |
| 250 | +#### Connection Failures |
| 251 | +- Verify database server is running |
| 252 | +- Check connection parameters (host, port, username, password) |
| 253 | +- Ensure database user has connection privileges |
| 254 | + |
| 255 | +#### Permission Errors |
| 256 | +- Verify user has appropriate database permissions |
| 257 | +- Check if user exists and has correct host restrictions |
| 258 | +- Ensure schema/database exists before granting permissions |
| 259 | + |
| 260 | +#### Flyway Migration Failures |
| 261 | +- Check if baseline migration is needed |
| 262 | +- Verify migration scripts are valid SQL |
| 263 | +- Ensure database schema is clean before first migration |
| 264 | + |
| 265 | +#### Seed Data Errors |
| 266 | +- Run schema creation (Flyway) before seed data |
| 267 | +- Check for foreign key constraint violations |
| 268 | +- Verify character encoding (UTF-8) support |
| 269 | + |
| 270 | +### Debugging |
| 271 | + |
| 272 | +#### Enable SQL Logging |
| 273 | +```properties |
| 274 | +logging.level.org.hibernate.SQL=DEBUG |
| 275 | +logging.level.org.flywaydb=DEBUG |
| 276 | +spring.jpa.show-sql=true |
| 277 | +spring.jpa.properties.hibernate.format_sql=true |
| 278 | +``` |
| 279 | + |
| 280 | +#### Verify Database State |
| 281 | +```sql |
| 282 | +-- MySQL |
| 283 | +SHOW DATABASES LIKE 'openespi_%'; |
| 284 | +SELECT User, Host FROM mysql.user WHERE User LIKE 'openespi_%'; |
| 285 | +SELECT COUNT(*) FROM openespi_usage.retail_customers; |
| 286 | + |
| 287 | +-- PostgreSQL |
| 288 | +\l openespi_* |
| 289 | +\du openespi_* |
| 290 | +SELECT COUNT(*) FROM usage.retail_customers; |
| 291 | +``` |
| 292 | + |
| 293 | +## Integration with Legacy Scripts |
| 294 | + |
| 295 | +The new initialization system is designed to replace the legacy scripts in `etc/` while maintaining compatibility: |
| 296 | + |
| 297 | +### Legacy Script Mapping |
| 298 | +- `initializedatabases.sh` → `./etc/env/dev/init-mysql-dev.sh` |
| 299 | +- `reset.sh` → `./etc/cleanup/mysql/cleanup_data_only.sql` + seed scripts |
| 300 | +- `cleandatabases_*.sql` → `./etc/cleanup/mysql/cleanup_databases.sql` |
| 301 | + |
| 302 | +### Migration Path |
| 303 | +1. Test new scripts in development environment |
| 304 | +2. Verify data compatibility with existing applications |
| 305 | +3. Update deployment scripts to use new initialization system |
| 306 | +4. Remove legacy scripts after successful migration |
| 307 | + |
| 308 | +## Future Enhancements |
| 309 | + |
| 310 | +- **Docker Compose**: Complete containerized development environment |
| 311 | +- **Kubernetes**: Helm charts for cloud deployment |
| 312 | +- **Monitoring**: Database health checks and metrics |
| 313 | +- **Backup**: Automated backup and restore procedures |
| 314 | +- **Performance**: Indexing optimization and query analysis |
| 315 | + |
| 316 | +## Support |
| 317 | + |
| 318 | +For issues and questions: |
| 319 | +- Review this documentation |
| 320 | +- Check the troubleshooting section |
| 321 | +- Examine log files for error details |
| 322 | +- Consult the Green Button Alliance community resources |
0 commit comments