from django.core.management.base import LabelCommand, CommandError

from corehq.apps.couch_sql_migration.couchsqlmigration import do_couch_to_sql_migration
from corehq.form_processor.utils import should_use_sql_backend


class Command(LabelCommand):

    def handle_label(self, domain, **options):
        if should_use_sql_backend(domain):
            raise CommandError(u'It looks like {} has already been migrated.'.format(domain))

        do_couch_to_sql_migration(domain)

@@ -1,12 +1,81 @@
+from itertools import izip_longest%0Afrom optparse import make_option%0A%0A
 from django.
@@ -106,34 +106,34 @@
 ort 
-Label
 Command
-, 
+Error, Label
 Command
-Error
 %0A%0Afr
@@ -234,314 +234,1907 @@
 ehq.
-form_processor.utils import should_use_sql_backend%0A%0A%0Aclass Command(LabelCommand):%0A%0A    def handle_label(self, domain, **options):%0A        if should_use_sql_backend(domain):%0A            raise CommandError(u'It looks like %7B%7D has already been migrated.'.format(domain))%0A%0A        do_couch_to_sql_migration(domain
+apps.hqcase.dbaccessors import get_case_ids_in_domain%0Afrom corehq.form_processor.backends.sql.dbaccessors import FormAccessorSQL, CaseAccessorSQL%0Afrom corehq.form_processor.utils import should_use_sql_backend%0Afrom couchforms.dbaccessors import get_form_ids_by_type%0Afrom couchforms.models import doc_types%0A%0A%0Aclass Command(LabelCommand):%0A    args = %22%3Cdomain%3E%22%0A    option_list = LabelCommand.option_list + (%0A        make_option('--stats', action='store_true', default=False),%0A    )%0A%0A    def handle_label(self, domain, **options):%0A        if should_use_sql_backend(domain):%0A            raise CommandError(u'It looks like %7B%7D has already been migrated.'.format(domain))%0A%0A        if options%5B'stats'%5D:%0A            self.print_stats(domain)%0A        else:%0A            do_couch_to_sql_migration(domain)%0A%0A    def print_stats(self, domain):%0A        for doc_type in doc_types():%0A            form_ids_in_couch = set(get_form_ids_by_type(domain, doc_type))%0A            form_ids_in_sql = set(FormAccessorSQL.get_form_ids_in_domain_by_type(domain, doc_type))%0A            self._print_status(doc_type, form_ids_in_couch, form_ids_in_sql)%0A%0A        case_ids_in_couch = set(get_case_ids_in_domain(domain))%0A        case_ids_in_sql = set(CaseAccessorSQL.get_case_ids_in_domain(domain))%0A        self._print_status('Cases', case_ids_in_couch, case_ids_in_sql)%0A%0A    def _print_status(self, name, ids_in_couch, ids_in_sql):%0A        header = ((82 - len(name)) / 2) * '_'%0A        print '%5Cn%7B%7D %7B%7D %7B%7D'.format(header, name, header)%0A        row = %22%7B:%5E40%7D %7C %7B:%5E40%7D%22%0A        print row.format('Couch', 'SQL')%0A        print row.format(len(ids_in_couch), len(ids_in_sql))%0A        if ids_in_couch %5E ids_in_sql:%0A            couch_only = list(ids_in_couch - ids_in_sql)%0A            sql_only = list(ids_in_sql - ids_in_couch)%0A            for couch, sql in izip_longest(couch_only, sql_only):%0A                print row.format(couch or '', sql or ''
 )%0A

add print stats option