diff --git a/Backend/src/main/java/de/itsolutions/ticketsystem/config/DatabaseInitializer.java b/Backend/src/main/java/de/itsolutions/ticketsystem/config/DatabaseInitializer.java new file mode 100644 index 0000000..0b4cc91 --- /dev/null +++ b/Backend/src/main/java/de/itsolutions/ticketsystem/config/DatabaseInitializer.java @@ -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."); + }; + } +} diff --git a/Backend/src/main/java/de/itsolutions/ticketsystem/service/AuthService.java b/Backend/src/main/java/de/itsolutions/ticketsystem/service/AuthService.java index 6b86d2e..d1dbfb2 100644 --- a/Backend/src/main/java/de/itsolutions/ticketsystem/service/AuthService.java +++ b/Backend/src/main/java/de/itsolutions/ticketsystem/service/AuthService.java @@ -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 users = userRepository.findAll(); for (User user : users) {