-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Furr
committed
Sep 26, 2018
1 parent
02bb94e
commit ffdb9ad
Showing
2 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters