MySQL database configuration and Flyway migration management for Portfolio Hub API
Portfolio Hub API uses MySQL 8.0+ as its primary database with Flyway for automated schema migrations. This guide covers database setup, schema structure, and migration management.
For better security, create a dedicated user for the application:
-- Create userCREATE USER 'portfolio_user'@'localhost' IDENTIFIED BY 'secure_password';-- Grant privilegesGRANT ALL PRIVILEGES ON `studiostkoh.portafolio`.* TO 'portfolio_user'@'localhost';-- Apply changesFLUSH PRIVILEGES;
User Permissions Required
The database user needs the following permissions:
SELECT, INSERT, UPDATE, DELETE - Data operations
CREATE, ALTER, DROP - Schema migrations (Flyway)
INDEX - Index management
REFERENCES - Foreign key constraints
For Flyway to work properly, the user must be able to create and modify tables.
# Enable Flywayspring.flyway.enabled=true# Migration scripts locationspring.flyway.locations=classpath:db/migration# Allow Flyway to work with existing databasesspring.flyway.baseline-on-migrate=true# Hibernate validation (no auto-modification)spring.jpa.hibernate.ddl-auto=validate
CREATE TABLE experience ( id BIGINT AUTO_INCREMENT PRIMARY KEY, created_at datetime(6) NOT NULL, created_by VARCHAR(150), updated_at datetime(6), updated_by VARCHAR(150), version BIGINT, profile_id BIGINT NOT NULL, company VARCHAR(150) NOT NULL, role VARCHAR(150) NOT NULL, location VARCHAR(100), start_date DATE NOT NULL, end_date DATE, current BOOLEAN NOT NULL DEFAULT FALSE, description TEXT, CONSTRAINT fk_experience_profile FOREIGN KEY (profile_id) REFERENCES profile(id) ON DELETE CASCADE);
CREATE TABLE contact_message ( id BIGINT AUTO_INCREMENT PRIMARY KEY, created_at datetime(6) NOT NULL, created_by VARCHAR(150), updated_at datetime(6), updated_by VARCHAR(150), version BIGINT, profile_id BIGINT NOT NULL, name VARCHAR(120) NOT NULL, email VARCHAR(160) NOT NULL, message TEXT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'NEW', CONSTRAINT fk_contact_profile FOREIGN KEY (profile_id) REFERENCES profile(id) ON DELETE CASCADE);
Migrations run automatically on application startup:
./mvnw spring-boot:run
You’ll see Flyway output in the logs:
Flyway: Migrating schema `studiostkoh.portafolio` to version 1 - initFlyway: Migrating schema `studiostkoh.portafolio` to version 2 - create core tablesFlyway: Migrating schema `studiostkoh.portafolio` to version 3 - add collaborator flagFlyway: Migrating schema `studiostkoh.portafolio` to version 4 - create certificate tableFlyway: Migrating schema `studiostkoh.portafolio` to version 5 - migration and addFlyway: Successfully applied 5 migrations
Symptoms: Application fails to start with Flyway errorSolutions:
Check database user has CREATE/ALTER permissions
Verify schema exists: SHOW DATABASES LIKE 'studiostkoh.portafolio';
Check migration file syntax
Review flyway_schema_history table for failed migrations
Repair failed migration:
DELETE FROM `studiostkoh.portafolio`.flyway_schema_history WHERE success = 0;
Schema validation failed
Symptoms:Hibernate validation error on startupCause: Schema doesn’t match JPA entitiesSolution:
Ensure all Flyway migrations have run successfully
Check for manual schema modifications
Compare schema with entity definitions
Connection refused
Symptoms: Cannot connect to MySQLCheck:
# Is MySQL running?sudo systemctl status mysql# Can you connect manually?mysql -h localhost -u portfolio_user -p# Check environment variablesecho $MYSQL_HOSTecho $MYSQL_USER