From 3a4b3403138f38f6e90e6ba6bc1259979745acaf Mon Sep 17 00:00:00 2001 From: Anthony Stirling Date: Thu, 11 Jun 2026 21:03:05 +0100 Subject: [PATCH] Remove legacy CreditBackfillRunner (PAYG replaces it; rows created lazily) --- .../saas/service/CreditBackfillRunner.java | 78 ------------------- 1 file changed, 78 deletions(-) delete mode 100644 app/saas/src/main/java/stirling/software/saas/service/CreditBackfillRunner.java diff --git a/app/saas/src/main/java/stirling/software/saas/service/CreditBackfillRunner.java b/app/saas/src/main/java/stirling/software/saas/service/CreditBackfillRunner.java deleted file mode 100644 index 008a7e937..000000000 --- a/app/saas/src/main/java/stirling/software/saas/service/CreditBackfillRunner.java +++ /dev/null @@ -1,78 +0,0 @@ -package stirling.software.saas.service; - -import java.util.List; - -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; - -import stirling.software.proprietary.security.database.repository.UserRepository; -import stirling.software.proprietary.security.model.User; - -/** - * ApplicationRunner that backfills user_credits table for existing users who don't have credit rows - * yet. This prevents existing users from being hard-blocked when the credit system is enabled. - * - *

This runs once at application startup after the database schema is ready. - */ -@Component -@Profile("saas") -@ConditionalOnProperty(name = "credits.enabled", havingValue = "true", matchIfMissing = true) -@RequiredArgsConstructor -@Slf4j -public class CreditBackfillRunner implements ApplicationRunner { - - private final UserRepository userRepository; - private final CreditService creditService; - - @Override - @Transactional - public void run(ApplicationArguments args) { - try { - backfillUserCredits(); - } catch (Exception e) { - log.error("Failed to backfill user credits", e); - // Don't throw; this shouldn't prevent app startup - } - } - - private void backfillUserCredits() { - log.info("Starting user credits backfill for existing users..."); - - List usersNeedingCredits = userRepository.findUsersWithApiKeyButNoCredits(); - - if (usersNeedingCredits.isEmpty()) { - log.info( - "No users need credit backfill; all users with API keys already have credit rows"); - return; - } - - log.info("Found {} users with API keys that need credit rows", usersNeedingCredits.size()); - - int backfilled = 0; - for (User user : usersNeedingCredits) { - try { - // Use the existing getOrCreateUserCredits method which handles proper allocation - creditService.getOrCreateUserCredits(user); - backfilled++; - - if (backfilled % 100 == 0) { - log.info("Backfilled credits for {} users so far...", backfilled); - } - } catch (Exception e) { - log.warn( - "Failed to create credits for user {}: {}", - user.getUsername(), - e.getMessage()); - } - } - - log.info("Successfully backfilled user_credits for {} existing users", backfilled); - } -}