blob: 3ef8ec367aebdf4febf24285bb0692599e4fec9d [file] [log] [blame]
Nabin Haitfe117362011-07-10 12:01:19 +05301"""
2 This patch removes wrong indexs and add proper indexes in tables
3"""
4
nabinhait1458af02011-07-20 10:31:19 +05305import webnotes
6sql = webnotes.conn.sql
7from webnotes.utils import cint, cstr
Nabin Haitfe117362011-07-10 12:01:19 +05308
9def create_proper_index():
10 from webnotes.modules.export_module import export_to_files
11
12 dt_index_fields={
13 'Purchase Receipt Detail': ['prevdoc_docname', 'item_code', 'warehouse', 'prevdoc_detail_docname'],
14 'Period Closing Voucher': ['closing_account_head', 'fiscal_year'],
15 'Lead': ['lead_name', 'status', 'transaction_date'],
16 'Time Sheet Detail': ['app_name'],
17 'Item Specification Detail': [],
18 'Budget Detail': ['fiscal_year', 'account'],
19 'Grade': [],
20 'RV Tax Detail': ['parenttype', 'account_head'],
21 'TDS Category Account': ['account_head'],
22 'Role': [],
23 'Leave Allocation': ['leave_type', 'employee', 'fiscal_year'],
24 'Branch': [],
25 'Department': [],
26 'Contact Detail': [],
27 'Territory': ['lft', 'rgt', 'parent_territory'],
28 'Item Tax': ['tax_type'],
29 'Bin': ['warehouse', 'item_code'],
30 'PPW Detail': ['warehouse'],
31 'Sales Partner': ['partner_name'],
32 'Default Home Page': ['home_page', 'role'],
33 'Custom Field': ['dt'],
34 'DocFormat': ['format'],
35 'DocType Mapper': ['from_doctype', 'to_doctype'],
36 'Brand': [],
37 'Order Lost Reason': [],
38 'Journal Voucher': ['posting_date', 'voucher_type'],
39 'TDS Return Acknowledgement': ['date_of_receipt', 'acknowledgement'],
40 'BOM Report Detail': ['item_code'],
41 'Quotation Detail': ['item_code'],
42 'Update Delivery Date Detail': ['sales_order_no'],
43 'Advance Adjustment Detail': ['journal_voucher'],
44 'Authorization Rule': ['approving_user', 'system_user', 'system_role', 'approving_role'],
45 'DocPerm': ['permlevel', 'role'],
46 'Stock Entry Detail': ['item_code', 't_warehouse', 's_warehouse'],
47 'Stock Entry': ['posting_date', 'delivery_note_no', 'purchase_receipt_no', 'production_order'],
48 'Price List': [],
49 'KRA Sheet': [],
50 'Production Order': ['status', 'project_name', 'production_item'],
51 'Account': ['lft', 'rgt', 'parent_account'],
52 'Earn Deduction Detail': [],
53 'Indent': ['status', 'transaction_date'],
54 'Tag Detail': [],
55 'SS Deduction Detail': ['d_type'],
56 'Batch': ['item'],
57 'Deduction Type': [],
58 'Project': ['project_name', 'customer'],
59 'UserRole': ['role'],
60 'DocField': ['label', 'fieldtype', 'fieldname'],
61 'Property Setter': ['doc_type', 'doc_name', 'property'],
62 'Appraisal': ['status', 'employee'],
63 'Letter Head': [],
64 'Follow up': ['follow_up_by'],
65 'Project Cost Breakup': [],
66 'Table Mapper Detail': [],
67 'Campaign': [],
68 'Static Parameter Detail': [],
69 'Leave Type': [],
70 'Account Balance': ['period', 'start_date', 'end_date', 'account'],
71 'Absent Days Detail': [],
72 'Tag': [],
73 'Raw Materials Supplied': ['raw_material'],
74 'Project Activity Update': [],
75 'PR Raw Material Detail': [],
76 'Bank Reconciliation Detail': ['voucher_id'],
77 'Sales Order': ['quotation_no', 'project_name', 'customer', 'posting_date'],
78 'Chapter VI A Detail': [],
79 'Experience In Company Detail': [],
80 'Order Reconciliation Detail': ['sales_order_no'],
81 'Attendance': ['employee', 'att_date'],
82 'Previous Experience Detail': [],
83 'Earning Detail': ['e_type'],
84 'Sales Order Detail': ['item_code', 'prevdoc_docname', 'reserved_warehouse'],
85 'KRA Template': [],
86 'Budget Distribution': ['fiscal_year'],
87 'Workstation': ['warehouse'],
88 'Period': [],
89 'Training Session Details': [],
90 'Other Charges': [],
91 'State': [],
92 'Bulk Rename Tool': [],
93 'Landed Cost Master Detail': [],
94 'Employee': ['employee_name', 'designation', 'department'],
95 'Terms And Conditions': [],
96 'TC Detail': [],
97 'UOM': [],
98 'Supplier Type': [],
99 'Project Milestone': [],
100 'Landed Cost Master': [],
101 'Budget Distribution Detail': [],
102 'Form 16A Ack Detail': [],
103 'Campaign Expense': [],
104 'Time Sheet': ['employee_name', 'time_sheet_date'],
105 'File Group': ['parent_group'],
106 'Maintenance Visit Detail': ['item_code', 'service_person'],
107 'Support Ticket Response': [],
nabinhait1458af02011-07-20 10:31:19 +0530108 'PV Detail': ['item_code', 'purchase_order', 'po_detail', 'purchase_receipt', 'pr_detail', 'expense_head', 'cost_center'],
Nabin Haitfe117362011-07-10 12:01:19 +0530109 'Timesheet Detail': ['project_name', 'task_id', 'customer_name'],
110 'Holiday List Detail': [],
111 'Workflow Rule Detail': [],
112 'Module Def': ['module_seq', 'module_page'],
113 'Term': [],
114 'PF Detail': ['item_code'],
115 'POS Setting': ['user', 'territory'],
116 'QA Specification Detail': [],
117 'Support Ticket': ['customer', 'allocated_to', 'status'],
118 'Project Activity': ['project'],
119 'Customer Group': ['lft', 'rgt', 'parent_customer_group'],
120 'Return Detail': ['item_code'],
121 'Series Detail': [],
122 'Event Role': ['role'],
123 'Contact': ['employee_id'],
124 'BOM Material': ['item_code', 'bom_no'],
125 'Invest 80 Declaration Detail': [],
126 'PO Raw Material Detail': [],
127 'Industry Type': [],
128 'Declaration Detail': [],
129 'Holiday List': ['fiscal_year'],
130 'Sales Person': ['lft', 'rgt', 'parent_sales_person'],
nabinhait1458af02011-07-20 10:31:19 +0530131 'RV Detail': ['item_code', 'sales_order', 'so_detail', 'delivery_note', 'dn_detail', 'cost_center', 'income_account'],
Nabin Haitfe117362011-07-10 12:01:19 +0530132 'Module Def Item': [],
133 'TDS Category': [],
134 'DocTrigger': [],
135 'Print Format': ['standard'],
136 'Installed Item Details': ['prevdoc_docname', 'item_code'],
137 'Form 16A Tax Detail': [],
138 'Event': ['event_date', 'event_type'],
139 'Currency': [],
140 'Service Quotation Detail': ['item_code'],
141 'Warehouse Type': ['warehouse_type'],
142 'Sales BOM': ['item_group'],
143 'IT Checklist': ['employee'],
144 'Purchase Other Charges': [],
145 'Company': [],
146 'Call Log': [],
147 'Professional Training Details': [],
148 'Warehouse': ['warehouse_type'],
149 'Competitor': [],
150 'Mode of Payment': [],
151 'Training Session': ['customer'],
152 'Cost Center': ['lft', 'rgt', 'parent_cost_center'],
153 'Timesheet': ['status', 'timesheet_date'],
154 'Form 16A': ['party_no'],
155 'Sales BOM Detail': ['item_code'],
156 'Answer': ['question'],
157 'Supplier': [],
158 'Installation Note': ['delivery_note_no', 'customer', 'inst_date'],
159 'Expense Voucher': ['approval_status', 'employee'],
160 'Target Detail': ['from_date', 'to_date', 'fiscal_year'],
161 'Page Role': ['role'],
162 'Partner Target Detail': ['fiscal_year', 'item_group'],
163 'Shipping Address': ['customer'],
164 'Indent Detail': ['item_code', 'warehouse'],
165 'TDS Payment Detail': [],
166 'Market Segment': [],
167 'Comment Widget Record': [],
168 'Service Order Detail': ['item_code', 'prevdoc_docname'],
169 'TDS Payment': ['from_date', 'to_date', 'tds_category'],
170 'Lead Email CC Detail': [],
171 'User Setting-Role User': [],
172 'Salary Slip': ['month', 'year', 'employee'],
173 'Maintenance Schedule Detail': ['item_code', 'scheduled_date'],
174 'Employment Type': [],
175 'Advance Allocation Detail': ['journal_voucher'],
176 'Quotation': ['customer', 'transaction_date'],
177 'Deduction Detail': ['d_type'],
178 'Bill Of Materials': ['item', 'project_name'],
179 'Earning Type': [],
180 'Designation': [],
181 'BOM Replace Utility Detail': ['parent_bom'],
182 'Question': [],
183 'Stock Ledger Entry': ['item_code', 'warehouse', 'posting_date', 'posting_time'],
184 'Educational Qualifications Detail': [],
185 'BOM Operation': [],
186 'Item Group': ['lft', 'rgt', 'parent_item_group'],
187 'Workflow Action Detail': [],
188 'User Setting-Profile': [],
189 'Customer Issue': ['item_code', 'customer', 'complaint_date'],
190 'Feed': [],
191 'Purchase Tax Detail': ['account_head'],
192 'GL Mapper Detail': [],
193 'TDS Detail': [],
194 'PRO Detail': ['item_code', 'source_warehouse'],
195 'DocType Label': [],
196 'Receivable Voucher': ['posting_date', 'debit_to', 'project_name'],
197 'GL Entry': ['posting_date', 'account', 'voucher_no'],
198 'Serial No': ['status', 'warehouse'],
199 'Delivery Note': ['posting_date', 'project_name', 'customer'],
200 'UOM Conversion Detail': ['uom'],
201 'Search Criteria': ['criteria_name'],
202 'Salary Structure': [],
203 'Educational Qualifications': ['qualification'],
204 'TDS Rate Chart': ['applicable_from', 'applicable_to'],
205 'GL Mapper': [],
206 'Announcement': [],
207 'Call Log Details': [],
208 'Enquiry': ['lead', 'customer', 'transaction_date'],
209 'Flat BOM Detail': ['item_code'],
210 'Landed Cost Detail': ['account_head'],
211 'Field Mapper Detail': ['from_field', 'to_field'],
212 'File Data': [],
213 'Question Tag': [],
214 'QA Inspection Report': ['item_code', 'purchase_receipt_no', 'report_date'],
215 'Appraisal Detail': [],
216 'POS Settings': ['territory'],
217 'Delivery Note Detail': ['item_code', 'prevdoc_docname', 'warehouse', 'prevdoc_detail_docname'],
218 'Profile': [],
219 'Other Income Detail': [],
220 'Product': ['item_code', 'stock_warehouse'],
221 'PO Detail': ['prevdoc_docname', 'item_code', 'prevdoc_detail_docname', 'warehouse'],
222 'Module Def Role': ['role'],
223 'Sales Team': ['sales_person'],
224 'Enquiry Detail': ['item_code'],
225 'DocType': [],
226 'Compaint Note': ['nature_of_complaint', 'compliance_date'],
227 'Maintenance Schedule': ['customer', 'sales_order_no'],
228 'Event User': ['person'],
229 'Stock Reconciliation': ['reconciliation_date'],
230 'Purchase Receipt': ['posting_date', 'supplier', 'project_name'],
231 'Complaint Detail': ['item_name'],
232 'Address': ['customer', 'supplier'],
233 'Ticket': ['request_date', 'allocated_to', 'category', 'customer', 'project'],
234 'Territory Target Detail': ['month', 'fiscal_year'],
235 'LC PR Detail': ['purchase_receipt_no'],
236 'Customer': ['customer_name', 'customer_group'],
237 'PP SO Detail': [],
238 'PP Detail': ['document_date', 'item_code', 'parent_item'],
239 'User Setting-Role Permission': [],
240 'Custom Script': ['dt'],
241 'Country': [],
242 'DefaultValue': [],
243 'Ledger Detail': [],
244 'SS Earning Detail': ['e_type'],
245 'SMS Log': [],
246 'Expense Type': [],
247 'Item': ['item_group'],
248 'Fiscal Year': [],
249 'ToDo Item': ['role'],
250 'Payable Voucher': ['posting_date', 'credit_to', 'project_name', 'supplier'],
251 'Journal Voucher Detail': ['account', 'against_voucher', 'against_invoice', 'against_jv'],
252 'Online Contact': [],
253 'Page': ['module'],
254 'Leave Application': ['employee', 'leave_type', 'from_date', 'to_date'],
255 'Expense Voucher Detail': ['expense_type'],
256 'Maintenance Visit': ['customer', 'sales_order_no', 'customer_issue_no'],
257 'Ref Rate Detail': ['price_list_name', 'ref_currency'],
258 'Receiver Detail': [],
259 'Naming Series Options': ['doc_type'],
260 'Activity Type': [],
261 'PRO PP Detail': [],
262 'Delivery Note Packing Detail': ['item_code', 'parent_item', 'warehouse'],
263 'Workflow Rule': ['select_form'],
264 'File': ['file_group'],
265 'Item Maintenance Detail': ['item_code', 'start_date', 'end_date', 'prevdoc_docname'],
266 'Purchase Order': ['supplier', 'project_name', 'posting_date'],
267 'Print Heading': [],
268 'TDS Rate Detail': ['category']
269 }
nabinhaitde9826c2011-07-20 18:02:59 +0530270 #sql("commit") # only required if run from login
nabinhait1458af02011-07-20 10:31:19 +0530271 exist_dt = [cstr(d[0]) for d in sql("select name from `tabDocType`")]
Nabin Haitfe117362011-07-10 12:01:19 +0530272
nabinhait1458af02011-07-20 10:31:19 +0530273 for dt in [d for d in dt_index_fields.keys() if d in exist_dt]:
274 try:
275 current_index = sql("show indexes from `tab%s`" % dt)
Nabin Haitfe117362011-07-10 12:01:19 +0530276
nabinhait1458af02011-07-20 10:31:19 +0530277 proper_index = dt_index_fields[dt]
Nabin Haitfe117362011-07-10 12:01:19 +0530278
nabinhait1458af02011-07-20 10:31:19 +0530279 for d in current_index:
280 if d[4] not in ['name', 'parent', 'parenttype']:
281 if d[4] not in proper_index:
282 sql("ALTER TABLE `tab%s` DROP INDEX %s" % (dt, d[4]))
283 sql("start transaction")
284 sql("UPDATE `tabDocField` SET search_index = 0 WHERE fieldname = '%s' AND parent = '%s'" % (d[4], dt))
285 sql("commit")
286 else:
287 proper_index.remove(d[4])
288
289 for d in proper_index:
290 sql("ALTER TABLE `tab%s` ADD INDEX ( `%s` ) " % (dt, d))
291 sql("start transaction")
292 sql("UPDATE `tabDocField` SET search_index = 1 WHERE fieldname = '%s' AND parent = '%s'" % (d, dt))
293 sql("commit")
nabinhait1458af02011-07-20 10:31:19 +0530294 except:
295 continue