Skip to content

Commit

Permalink
Working on ml_copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Furr committed Sep 26, 2018
1 parent 02bb94e commit ffdb9ad
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
67 changes: 67 additions & 0 deletions mongolog/management/commands/ml_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
"""
Management command to handle deletion of previous mongolog records.
Usage Examples:
# Used to migrate monoglog data from one collection to another
./manage.py ml_migrate {from_collection} {to_collectioj}
"""
from __future__ import print_function
import sys
import logging
import subprocess
from pymongo import MongoClient

from mongolog.handlers import get_mongolog_handler

from django.core.management.base import BaseCommand

console = logging.getLogger('mongolog-int')


class Command(BaseCommand):

def add_arguments(self, parser):
parser.add_argument(
'-f', '--from', default='', type=str, action='store', dest='from',
help='From Collection',
)
parser.add_argument(
'-t', '--to', default='', type=str, action='store', dest='to',
help='To Collection',
)

def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs)

def confirm(self, **options):
if not options['force']:
while 1:
console.warn("Would you like to proceed? Y/N")
ans = input().strip().lower()

if ans not in ['y', 'yes', 'n', 'no']:
continue
elif ans[0] == 'n':
console.info("You chose not to continue. Bye!")
sys.exit(1)
elif ans[0] == 'y':
break

return True

def backup(self):
""" Backup the collection before deleting """
console.info("Backing up your documents...")
cmd = 'mongodump --db mongolog'
subprocess.check_call([cmd], shell=True)

def handle(self, *args, **options):
""" Main processing handle """
handler = get_mongolog_handler()
client = MongoClient(handler.connection)
db = client.mongolog

from_collection = getattr(db, options['from_collection'])
from_collection.copyTo(options['to_collection'])
4 changes: 2 additions & 2 deletions mongolog/management/commands/ml_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
Usage Examples:
# Delete all records alder than 54 day's AFTER backing up the current collection 'bulkupload'
./manage.py mongolog_purge -d 54 -b -c bulkupload
./manage.py ml_purge -d 54 -b -c bulkupload
# Delete all entries
./manage.py mongolog_purge -p -c bulkupload
./manage.py ml_purge -p -c bulkupload
"""
from __future__ import print_function
import sys
Expand Down

0 comments on commit ffdb9ad

Please sign in to comment.