From 55e70521894a1c5e6ad6098bc5de73782bf50762 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sun, 28 Apr 2024 08:15:13 -0400 Subject: [PATCH] [+] Migrate: Remove signed IDs --- .../java/icu/samnyan/aqua/net/Migrations.kt | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/icu/samnyan/aqua/net/Migrations.kt diff --git a/src/main/java/icu/samnyan/aqua/net/Migrations.kt b/src/main/java/icu/samnyan/aqua/net/Migrations.kt new file mode 100644 index 00000000..d1bc7208 --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/net/Migrations.kt @@ -0,0 +1,58 @@ +package icu.samnyan.aqua.net + +import ext.JACKSON +import ext.invoke +import ext.logger +import ext.parse +import icu.samnyan.aqua.net.db.AquaNetUserRepo +import icu.samnyan.aqua.sega.general.dao.CardRepository +import icu.samnyan.aqua.sega.general.dao.PropertyEntryRepository +import icu.samnyan.aqua.sega.general.model.PropertyEntry +import jakarta.annotation.PostConstruct +import org.springframework.stereotype.Service + + +@Service +class Migrations( + val userRepo: AquaNetUserRepo, + val cardRepo: CardRepository, + val props: PropertyEntryRepository +) { + companion object { + val log = logger() + } + + @PostConstruct + fun migrate() { + val db = props.findByPropertyKey("migrations")() ?: PropertyEntry("migrations", "[]") + val p = JACKSON.parse>(db.propertyValue) + val old = p.size + + if ("0. Signed ID" !in p) { + migrateSignedID() + p.add("0. Signed ID") + } + + if (p.size > old) props.save(db.apply { propertyValue = JACKSON.writeValueAsString(p) }) + } + + /** + * Migrate the ID to remove the signed bit + */ + fun migrateSignedID() { + // Loop through everyone's ID, remove the signed bit + val cards = cardRepo.findAll() + val max = Int.MAX_VALUE.toLong() + cards.forEach { c -> + if (c.extId > max) { + var new = c.extId and max + log.info("Removing signed bit: {${c.extId} -> $new} for ${c.luid}") + while (cardRepo.findByExtId(new).isPresent) { + log.error("> Conflicting card found for ${c.luid}: $new") + new++ + } + cardRepo.save(c.apply { extId = new }) + } + } + } +} \ No newline at end of file