File: src/main/resources/application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/techtorque_timelogs
spring.datasource.username=techtorque
spring.datasource.password=techtorque123
spring.jpa.hibernate.ddl-auto=update # Auto-creates tables!File: src/main/java/.../config/DatabasePreflightInitializer.java
- Tests database connection BEFORE app starts
- Exits with error if database is unavailable
- Same pattern as Auth service
When you start the service, Hibernate automatically creates:
CREATE TABLE time_logs (
id VARCHAR(255) PRIMARY KEY,
employee_id VARCHAR(255) NOT NULL,
service_id VARCHAR(255),
project_id VARCHAR(255),
hours DOUBLE PRECISION NOT NULL,
date DATE NOT NULL,
description TEXT,
work_type VARCHAR(255),
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL
);File: src/main/java/.../config/DataSeeder.java
β
Runs automatically when app starts
β
Only in dev profile (not production)
β
Creates sample time logs for testing
β
Skips if data already exists
- 3 Employees: EMP001, EMP002, EMP003
- 7 Days of time logs (excluding weekends)
- ~30-40 entries with realistic data
- Various work types: Development, Testing, Meetings, etc.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. Application Starts β
β .\mvnw.cmd spring-boot:run β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 2. DatabasePreflightInitializer β
β β Reads application.properties β
β β Tries to connect to PostgreSQL β
β β SUCCESS β Continue | FAIL β Exit β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 3. Hibernate Schema Creation β
β β Reads @Entity TimeLog β
β β Compares with database β
β β Creates/Updates tables automatically β
β β CREATE TABLE IF NOT EXISTS time_logs... β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 4. DataSeeder.run() [NEW!] β
β β Check if dev profile active β
β β Check if data exists β
β β Insert sample time logs (if empty) β
β β Log statistics β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 5. Service Ready! π β
β http://localhost:8085 β
β Database: techtorque_timelogs β
β API endpoints accepting requests β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Feature | Auth Service | Time Logging Service |
|---|---|---|
| Database Config | β application.properties | β application.properties |
| Preflight Check | β Yes | β Yes |
| Auto Schema | β Hibernate DDL | β Hibernate DDL |
| Data Seeder | β Seeds Users & Roles | β Seeds Time Logs |
| Dev Profile Only | β Yes | β Yes |
| Idempotent | β Checks before insert | β Checks before insert |
Your service follows the EXACT SAME pattern! β
cd D:\TechTorque\Time_Logging_Service\time-logging-service
.\mvnw.cmd spring-boot:runWhat Happens:
- β Connects to PostgreSQL
- β
Creates
time_logstable (if not exists) - β Seeds sample data (if empty)
- β Service ready on port 8085
# Via API
curl http://localhost:8085/api/time-logs/employee/EMP001
# Via Database
psql -U techtorque -d techtorque_timelogs
SELECT COUNT(*) FROM time_logs;- Users (superadmin, admin, employee, customer)
- Roles (SUPER_ADMIN, ADMIN, EMPLOYEE, CUSTOMER)
- User-Role Relationships
- Time Log Entries (work records)
- Sample Projects (PRJ001, PRJ002, etc.)
- Sample Services (SRV001, SRV002, etc.)
- Various Work Types (Development, Testing, etc.)
Both follow the same pattern: Check profile β Check existing data β Insert if needed
@Component
public class DataSeeder implements CommandLineRunner {
@Override
public void run(String... args) {
// Step 1: Check if dev profile
if (!isDevProfile()) {
return; // Skip in production
}
// Step 2: Check if data exists
if (timeLogRepository.count() > 0) {
return; // Already seeded
}
// Step 3: Insert sample data
seedSampleTimeLogs();
}
private void seedSampleTimeLogs() {
// Create logs for 3 employees
// Over 7 days (excluding weekends)
// Morning + afternoon sessions
// Total: ~30-40 entries
}
}After starting the service with the DataSeeder:
β
Service Running on Port: 8085
β
Database: techtorque_timelogs (connected)
β
Tables: time_logs (auto-created)
β
Sample Data: ~30-40 time log entries
β
3 Test Employees: EMP001, EMP002, EMP003
β
Date Range: Last 7 days (weekdays only)
β
Work Types: Development, Testing, Meetings, etc.
{
"id": "uuid-here",
"employeeId": "EMP001",
"projectId": "PRJ001",
"serviceId": "SRV001",
"hours": 4.5,
"date": "2025-10-31",
"description": "Implemented new feature for customer dashboard",
"workType": "Development",
"createdAt": "2025-10-31T10:00:00",
"updatedAt": "2025-10-31T10:00:00"
}If you want to create the database manually first:
-- Connect to PostgreSQL
psql -U postgres
-- Create database
CREATE DATABASE techtorque_timelogs;
-- Create user
CREATE USER techtorque WITH PASSWORD 'techtorque123';
-- Grant permissions
GRANT ALL PRIVILEGES ON DATABASE techtorque_timelogs TO techtorque;But this is OPTIONAL! The service can create everything automatically.
# Override defaults in production
export DB_HOST=production-server.com
export DB_PORT=5432
export DB_NAME=timelogs_prod
export DB_USER=secure_user
export DB_PASS=secure_password
export DB_MODE=validate # Don't auto-modify schema
export SPRING_PROFILE=prod # No data seeding# Uses defaults from application.properties
DB_HOST=localhost
DB_PORT=5432
DB_NAME=techtorque_timelogs
DB_USER=techtorque
DB_PASS=techtorque123
DB_MODE=update # Auto-update schema
SPRING_PROFILE=dev # Enable data seeding| File | Purpose |
|---|---|
application.properties |
Database connection config |
DatabasePreflightInitializer.java |
Connection check on startup |
TimeLog.java |
Entity β defines table structure |
TimeLogRepository.java |
Database operations |
DataSeeder.java |
Sample data population [NEW!] |
- Database connection configuration
- Preflight check (connection verification)
- Automatic table creation
- Working CRUD operations
- DataSeeder.java - Populates sample data for testing
- DATABASE_SETUP_GUIDE.md - Comprehensive documentation
- THIS SUMMARY - Quick reference
Your Time Logging Service now has the EXACT SAME database setup pattern as the Auth Service!
β
Connection config
β
Preflight check
β
Auto schema creation
β
Data seeding (dev only)
β
Production-ready
Everything follows Spring Boot best practices and mirrors the Auth service architecture!
-
Start the service:
.\mvnw.cmd spring-boot:run -
Verify seeding:
curl http://localhost:8085/api/time-logs/employee/EMP001
-
Check database:
psql -U techtorque -d techtorque_timelogs SELECT * FROM time_logs LIMIT 5;
That's it! Your database setup is complete and matches the Auth service pattern. π