<s> from optparse import make_option <EOL> from django . core . management . base import BaseCommand <EOL> from corehq . apps . hqcase . dbaccessors import get_case_ids_in_domain <EOL> from corehq . apps . tzmigration import set_migration_started , set_migration_complete , set_migration_not_started , get_migration_status , MigrationStatus <EOL> from corehq . apps . tzmigration . timezonemigration import prepare_planning_db , get_planning_db , get_planning_db_filepath , delete_planning_db , prepare_case_json , FormJsonDiff , commit_plan <EOL> from corehq . util . dates import iso_string_to_datetime <EOL> from couchforms . dbaccessors import get_form_ids_by_type <EOL> def _is_datetime ( string ) : <EOL> if not isinstance ( string , basestring ) : <EOL> return False <EOL> try : <EOL> iso_string_to_datetime ( string ) <EOL> except ( ValueError , OverflowError , TypeError ) : <EOL> return False <EOL> else : <EOL> return True <EOL> class Command ( BaseCommand ) : <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> ) <EOL> @ staticmethod <EOL> def require_only_option ( sole_option , options ) : <EOL> base_options = { option . dest for option in BaseCommand . option_list } <EOL> assert all ( not value for key , value in options . items ( ) <EOL> if key not in base_options and key != sole_option ) <EOL> def handle ( self , domain , ** options ) : <EOL> filepath = get_planning_db_filepath ( domain ) <EOL> self . stdout . write ( '<STR_LIT>' . format ( filepath ) ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . require_only_option ( '<STR_LIT>' , options ) <EOL> set_migration_started ( domain ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . require_only_option ( '<STR_LIT>' , options ) <EOL> set_migration_not_started ( domain ) <EOL> if options [ '<STR_LIT>' ] : <EOL> delete_planning_db ( domain ) <EOL> self . stdout . write ( '<STR_LIT>' . format ( filepath ) ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . planning_db = prepare_planning_db ( domain ) <EOL> self . stdout . write ( '<STR_LIT>' . format ( filepath ) ) <EOL> else : <EOL> self . planning_db = get_planning_db ( domain ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . require_only_option ( '<STR_LIT>' , options ) <EOL> assert get_migration_status ( domain , strict = True ) == MigrationStatus . IN_PROGRESS <EOL> commit_plan ( domain , self . planning_db ) <EOL> set_migration_complete ( domain ) <EOL> if options [ '<STR_LIT>' ] : <EOL> prepare_case_json ( self . planning_db ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . valiate_forms_and_cases ( domain ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . show_diffs ( ) <EOL> if options [ '<STR_LIT>' ] : <EOL> from corehq . apps . tzmigration . planning import * <EOL> session = self . planning_db . Session ( ) <EOL> try : <EOL> import ipdb as pdb <EOL> except ImportError : <EOL> import pdb <EOL> pdb . set_trace ( ) <EOL> def valiate_forms_and_cases ( self , domain ) : <EOL> form_ids_in_couch = set ( get_form_ids_by_type ( domain , '<STR_LIT>' ) ) <EOL> form_ids_in_sqlite = set ( self . planning_db . get_all_form_ids ( ) ) <EOL> print '<STR_LIT>' . format ( len ( form_ids_in_couch ) ) <EOL> print '<STR_LIT>' . format ( len ( form_ids_in_sqlite ) ) <EOL> if form_ids_in_couch ^ form_ids_in_sqlite : <EOL> print '<STR_LIT>' . format ( <EOL> list ( form_ids_in_couch - form_ids_in_sqlite ) ) <EOL> case_ids_in_couch = set ( get_case_ids_in_domain ( domain ) ) <EOL> case_ids_in_sqlite = set ( self . planning_db . get_all_case_ids ( ) ) <EOL> print '<STR_LIT>' . format ( len ( case_ids_in_couch ) ) <EOL> print '<STR_LIT>' . format ( len ( case_ids_in_sqlite ) ) <EOL> if case_ids_in_couch ^ case_ids_in_sqlite : <EOL> print '<STR_LIT>' . format ( <EOL> list ( case_ids_in_couch - case_ids_in_sqlite ) ) <EOL> print '<STR_LIT>' . format ( <EOL> list ( case_ids_in_sqlite - case_ids_in_couch ) ) <EOL> def show_diffs ( self ) : <EOL> for form_id , json_diff in self . planning_db . get_diffs ( ) : <EOL> if json_diff . diff_type == '<STR_LIT>' : <EOL> if _is_datetime ( json_diff . old_value ) and _is_datetime ( json_diff . new_value ) : <EOL> continue <EOL> if json_diff in ( <EOL> FormJsonDiff ( diff_type = u'<STR_LIT:type>' , path = [ u'<STR_LIT>' ] , old_value = u'<STR_LIT>' , new_value = None ) , <EOL> FormJsonDiff ( diff_type = u'<STR_LIT:type>' , path = [ u'<STR_LIT>' ] , old_value = u'<STR_LIT>' , new_value = None ) ) : <EOL> continue <EOL> print '<STR_LIT>' . format ( form_id , json_diff ) </s>
