Skip to content

Commit

Permalink
[BASE] added open chat connector migration command
Browse files Browse the repository at this point in the history
  • Loading branch information
dudanogueira committed Apr 20, 2024
1 parent 8632e66 commit c9db9de
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
17 changes: 15 additions & 2 deletions docs/deploy.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
DEPLOY
DEPLOY / MIGRATION
======================================================================

COOKIE CUTTER
----------------------------------------------------------------------

Check this out: https://github.com/dudanogueira/rocketconnect_cookiecutter
Check this out: https://github.com/dudanogueira/rocketconnect_cookiecutter


Open Chat Connector Migration Command
----------------------------------------------------------------------

There is a new migration command that will help you migrating all open chats from connector X to connector Y

this is helpful if you want to change connectors. This will basically change in bulk the connector from the open chats of a given connector.


❯ docker compose -f local.yml run --rm django python manage.py migrate_connector_chat 6 2 --apply

this will apply the migration from open chats at connector id 6 to connector id 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django_celery_beat.models import PeriodicTask
from instance.models import LiveChatRoom, Connector

class Command(BaseCommand):
help = "Migrate chats from one connector to the other. This is useful for migrating a number between different connectors."

def add_arguments(self, parser):
parser.add_argument("from", nargs=1, type=int)
parser.add_argument("to", nargs=1, type=int)

# Named (optional) arguments
parser.add_argument(
"--apply",
action="store_true",
help="Apply the migration",
)

def handle(self, *args, **options):
print(options)
# get from
from_connector = int(options.get("from")[0])
to_connector = int(options.get("to")[0])
open_chats = LiveChatRoom.objects.filter(
connector__id=from_connector,
open=True
)
if open_chats.count():
from_connector_object = open_chats.first().connector
print("Migrating from connector: ", open_chats.first().connector)
print(open_chats)
print(open_chats.count(), "Chats found.")
# get the to connector
try:
to_connector_object = Connector.objects.get(id=to_connector)
print("Migrating to this connector: ", to_connector_object)
except Connector.DoesNotExist:
print("Error! Connector to migrate not found")
to_connector_object = None
# select and print all the findings
# apply
if options.get("apply"):
print(f"Applying... migrating {open_chats.count()} open chats from {from_connector_object} to {to_connector_object}")
update = open_chats.update(connector_id=to_connector_object.id)
print("RESULT: ", update)

else:
print("No chats found on connector with id", from_connector)

0 comments on commit c9db9de

Please sign in to comment.