mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
[Config] Migrate UUID for JSON backend (#2604)
* Migrate UUID for JSON backend * Interesting... * black * Simplify UUID creation
This commit is contained in:
parent
f20a174038
commit
05e2851c67
@ -7,7 +7,7 @@ import weakref
|
||||
import discord
|
||||
|
||||
from .data_manager import cog_data_path, core_data_path
|
||||
from .drivers import get_driver, IdentifierData
|
||||
from .drivers import get_driver, IdentifierData, BackendType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .drivers.red_base import BaseDriver
|
||||
@ -562,6 +562,10 @@ class Config:
|
||||
def defaults(self):
|
||||
return deepcopy(self._defaults)
|
||||
|
||||
@staticmethod
|
||||
def _create_uuid(identifier: int):
|
||||
return str(identifier)
|
||||
|
||||
@classmethod
|
||||
def get_conf(cls, cog_instance, identifier: int, force_registration=False, cog_name=None):
|
||||
"""Get a Config instance for your cog.
|
||||
@ -602,7 +606,8 @@ class Config:
|
||||
cog_path_override = cog_data_path(cog_instance=cog_instance)
|
||||
|
||||
cog_name = cog_path_override.stem
|
||||
uuid = str(hash(identifier))
|
||||
# uuid = str(hash(identifier))
|
||||
uuid = cls._create_uuid(identifier)
|
||||
|
||||
# We have to import this here otherwise we have a circular dependency
|
||||
from .data_manager import basic_config
|
||||
@ -613,6 +618,9 @@ class Config:
|
||||
driver = get_driver(
|
||||
driver_name, cog_name, uuid, data_path_override=cog_path_override, **driver_details
|
||||
)
|
||||
if driver_name == BackendType.JSON.value:
|
||||
driver.migrate_identifier(identifier)
|
||||
|
||||
conf = cls(
|
||||
cog_name=cog_name,
|
||||
unique_identifier=uuid,
|
||||
|
||||
@ -96,6 +96,18 @@ class JSON(BaseDriver):
|
||||
self.data = {}
|
||||
self.jsonIO._save_json(self.data)
|
||||
|
||||
def migrate_identifier(self, raw_identifier: int):
|
||||
if self.unique_cog_identifier in self.data:
|
||||
# Data has already been migrated
|
||||
return
|
||||
poss_identifiers = [str(raw_identifier), str(hash(raw_identifier))]
|
||||
for ident in poss_identifiers:
|
||||
if ident in self.data:
|
||||
self.data[self.unique_cog_identifier] = self.data[ident]
|
||||
del self.data[ident]
|
||||
self.jsonIO._save_json(self.data)
|
||||
break
|
||||
|
||||
async def get(self, identifier_data: IdentifierData):
|
||||
partial = self.data
|
||||
full_identifiers = identifier_data.to_tuple()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user