From 66df278aec30d216a02472f8ec7877fae17811a6 Mon Sep 17 00:00:00 2001 From: jagadeeswaran-zipstack Date: Thu, 9 Jan 2025 09:57:35 +0530 Subject: [PATCH 1/3] migrate v1 adapters to v2 --- .../migrations/0002_added_version_in_llmw.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py diff --git a/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py b/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py new file mode 100644 index 000000000..d0bd13162 --- /dev/null +++ b/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py @@ -0,0 +1,54 @@ +# Generated by Django 4.2.1 on 2025-01-08 21:08 + +import json + +from cryptography.fernet import Fernet +from django.conf import settings +from django.db import migrations + + +def migrate_adapter_data(apps, schema_editor): + AdapterInstance = apps.get_model("adapter_processor_v2", "AdapterInstance") + + old_adapter_id = "llmwhisperer|0a1647f0-f65f-410d-843b-3d979c78350e" + new_adapter_id = "llmwhisperer|a5e6b8af-3e1f-4a80-b006-d017e8e67f93" + + for adapter in AdapterInstance.objects.all(): + + # Deserialize adapter_metadata_b if it exists + encryption_secret: str = settings.ENCRYPTION_KEY + f: Fernet = Fernet(encryption_secret.encode("utf-8")) + if adapter.adapter_metadata_b: + + metadata = json.loads( + f.decrypt(bytes(adapter.adapter_metadata_b).decode("utf-8")) + ) + else: + metadata = {} + + # Add version in adapter_metadata_b + if "version" not in metadata: + if adapter.adapter_id == old_adapter_id: + metadata["version"] = "v1" + else: + metadata["version"] = "v2" + + # Serialize and save updated metadata + + adapter.adapter_metadata_b = f.encrypt(json.dumps(metadata).encode("utf-8")) + + if adapter.adapter_id == old_adapter_id: + adapter.adapter_id = new_adapter_id + + adapter.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("adapter_processor_v2", "0001_initial"), + ] + + operations = [ + migrations.RunPython(migrate_adapter_data), + ] From 9ca83667cccdf973ea66a22ab55901d29504f400 Mon Sep 17 00:00:00 2001 From: jagadeeswaran-zipstack Date: Mon, 13 Jan 2025 18:28:17 +0530 Subject: [PATCH 2/3] added rollback function for migration --- .../migrations/0002_added_version_in_llmw.py | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py b/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py index d0bd13162..9dac086b2 100644 --- a/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py +++ b/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py @@ -43,6 +43,35 @@ def migrate_adapter_data(apps, schema_editor): adapter.save() +def rollback_adapter_data(apps, schema_editor): + AdapterInstance = apps.get_model("adapter_processor_v2", "AdapterInstance") + + old_adapter_id = "llmwhisperer|0a1647f0-f65f-410d-843b-3d979c78350e" + + encryption_secret: str = settings.ENCRYPTION_KEY + f: Fernet = Fernet(encryption_secret.encode("utf-8")) + + for adapter in AdapterInstance.objects.all(): + if adapter.adapter_metadata_b: + # Deserialize adapter_metadata_b + metadata = json.loads( + f.decrypt(bytes(adapter.adapter_metadata_b).decode("utf-8")) + ) + + # Check if 'version' is 'v1' and revert adapter_id to old value + if metadata.get("version") == "v1": + adapter.adapter_id = old_adapter_id + + # Remove 'version' key to restore original state + if "version" in metadata: + del metadata["version"] + + # Serialize and save updated metadata + adapter.adapter_metadata_b = f.encrypt(json.dumps(metadata).encode("utf-8")) + + adapter.save() + + class Migration(migrations.Migration): dependencies = [ @@ -50,5 +79,5 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunPython(migrate_adapter_data), + migrations.RunPython(migrate_adapter_data, reverse_code=rollback_adapter_data), ] From 0ed5d94c401732561cb5f5530d2341caf9cee43f Mon Sep 17 00:00:00 2001 From: jagadeeswaran-zipstack Date: Mon, 13 Jan 2025 18:33:16 +0530 Subject: [PATCH 3/3] fixed sonar issues --- .../migrations/0002_added_version_in_llmw.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py b/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py index 9dac086b2..63c0129ed 100644 --- a/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py +++ b/backend/adapter_processor_v2/migrations/0002_added_version_in_llmw.py @@ -8,12 +8,12 @@ def migrate_adapter_data(apps, schema_editor): - AdapterInstance = apps.get_model("adapter_processor_v2", "AdapterInstance") + adapter_instance = apps.get_model("adapter_processor_v2", "AdapterInstance") old_adapter_id = "llmwhisperer|0a1647f0-f65f-410d-843b-3d979c78350e" new_adapter_id = "llmwhisperer|a5e6b8af-3e1f-4a80-b006-d017e8e67f93" - for adapter in AdapterInstance.objects.all(): + for adapter in adapter_instance.objects.all(): # Deserialize adapter_metadata_b if it exists encryption_secret: str = settings.ENCRYPTION_KEY @@ -44,14 +44,14 @@ def migrate_adapter_data(apps, schema_editor): def rollback_adapter_data(apps, schema_editor): - AdapterInstance = apps.get_model("adapter_processor_v2", "AdapterInstance") + adapter_instance = apps.get_model("adapter_processor_v2", "AdapterInstance") old_adapter_id = "llmwhisperer|0a1647f0-f65f-410d-843b-3d979c78350e" encryption_secret: str = settings.ENCRYPTION_KEY f: Fernet = Fernet(encryption_secret.encode("utf-8")) - for adapter in AdapterInstance.objects.all(): + for adapter in adapter_instance.objects.all(): if adapter.adapter_metadata_b: # Deserialize adapter_metadata_b metadata = json.loads(