@@ -0,0 +1,2266 @@
+'''%0ACreated on Mar 26, 2010%0A%0A@author: bderenzi%0A'''%0Aimport MySQLdb%0A%0AHOST = %22localhost%22%0AUSER = %22root%22%0APASSWORD = %22%22%0ADB_NAME = %22commcarehq_data%22%0A%0Adef fix_table(table_name):%0A    q = %22SELECT id, parent_id FROM %22 + table_name%0A    cursor.execute(q)%0A    %0A    result = cursor.fetchall()%0A    for r in result:%0A        # check that we don't update if parent_id = 1 because cory says those are correct%0A        # CZUE: unfortunately this isn't 100%25 true.  They are correct if the ID was supposed%0A        # to be 1, however 2 will actually go in as 1 as well.  I have no idea how to %0A        # reconcile this cleanly%0A        if int(r%5B1%5D) == 1:%0A            continue%0A        %0A        q = %22UPDATE %22 + table_name + %22 SET parent_id='%22 + str(r%5B1%5D+1) + %22' WHERE id = %22 + str(r%5B0%5D)%0A        cursor.execute(q)%0A        print q%0A%0A%0Adef get_tables():%0A    q = '''%0A    SELECT xformmanager_elementdefmodel.table_name%0A    FROM xformmanager_elementdefmodel, xformmanager_formdefmodel%0A    WHERE xformmanager_elementdefmodel.form_id = xformmanager_formdefmodel.id%0A    %09AND xformmanager_elementdefmodel.table_name %3C%3E xformmanager_formdefmodel.form_name%0A    '''%0A%0A    cursor.execute(q)%0A    return cursor.fetchall()%0A%0A    %0A'''%0Astart here%0A'''%0Adb = MySQLdb.connect(HOST,USER,PASSWORD,DB_NAME)%0Acursor = db.cursor()%0A%0Afor res in get_tables():%0A    table = res%5B0%5D%0A    print %22Fixing: %22, table%0A    fix_table(table)%0A# %0A# # normal brac domain%0A# # CZUE: need to fix this to work with all the other child tables on HQ as well%0A# fix_table(%22schema_brac_chp_homevisit_followup_brac_new_babies_8%22)%0A# fix_table(%22schema_brac_chp_homevisit_followup_brac_new_babies_10%22)%0A# fix_table(%22schema_brac_chp_homevisit_followup_brac_problem_death_10%22)%0A# fix_table(%22schema_brac_chp_homevisit_followup_brac_problem_death_6%22)%0A# fix_table(%22schema_brac_chp_homevisit_followup_brac_problem_death_8%22)%0A# fix_table(%22schema_brac_chp_homevisit_followup_brac_sick_person_10%22)%0A# fix_table(%22schema_brac_chp_homevisit_followup_brac_sick_person_8%22)%0A# %0A# # dodoma domain%0A# fix_table(%22schema_dodoma_brac_chp_homevisit_followup_brac_new_babies_10%22)%0A# fix_table(%22schema_dodoma_brac_chp_homevisit_followup_brac_problem_death_10%22)%0A# fix_table(%22schema_dodoma_brac_chp_homevisit_followup_brac_sick_person_10%22)%0A%0A# clean up%0Acursor.close ()%0Adb.commit ()%0Adb.close()%0A

fix parent id