This commit is contained in:
Hymmel 2026-01-22 11:40:06 +01:00
parent 48cca56b49
commit dfef90726c
2 changed files with 40 additions and 1 deletions

View file

@ -0,0 +1,36 @@
package de.itsolutions.ticketsystem.config;
import de.itsolutions.ticketsystem.service.AuthService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Configuration
public class DatabaseInitializer {
private static final Logger logger = LoggerFactory.getLogger(DatabaseInitializer.class);
@Bean
public CommandLineRunner initDatabase(JdbcTemplate jdbcTemplate, AuthService authService) {
return args -> {
logger.info("Checking database schema...");
// Add 'theme' column if it doesn't exist
try {
jdbcTemplate.execute("ALTER TABLE users ADD COLUMN IF NOT EXISTS theme VARCHAR(255) DEFAULT 'system' NOT NULL");
logger.info("Schema check: 'theme' column ensured.");
} catch (Exception e) {
logger.error("Error updating schema: " + e.getMessage());
// Continue, as it might fail if table locks exist, but we hope for the best or it's already there
}
// Run user data migration
logger.info("Running user migration...");
authService.migrateUsers();
logger.info("User migration completed.");
};
}
}

View file

@ -85,7 +85,10 @@ public class AuthService {
* Migrates existing user data, specifically populating firstname and lastname from the full name.
* This method runs once after the application context is initialized.
*/
@jakarta.annotation.PostConstruct
/**
* Migrates existing user data, specifically populating firstname and lastname from the full name.
* This method is called by DatabaseInitializer on startup.
*/
public void migrateUsers() {
List<User> users = userRepository.findAll();
for (User user : users) {