Skip to content

Commit 67777b1

Browse files
authored
Merge pull request frappe#6498 from ElasticRun/series-key
fix: refactor primary key in series patch
2 parents c2d8201 + 0520c97 commit 67777b1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

frappe/patches.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ frappe.patches.v10_0.modify_naming_series_table
229229
frappe.patches.v10_0.enhance_security
230230
frappe.patches.v11_0.multiple_references_in_events
231231
frappe.patches.v11_0.set_allow_self_approval_in_workflow
232-
execute:frappe.db.sql('ALTER table `tabSeries` ADD PRIMARY KEY IF NOT EXISTS (name)')
233232
frappe.patches.v11_0.remove_skip_for_doctype
234233
frappe.patches.v11_0.migrate_report_settings_for_new_listview
235234
frappe.patches.v11_0.delete_all_prepared_reports
236235
frappe.patches.v11_0.fix_order_by_in_reports_json
237236
execute:frappe.delete_doc('Page', 'applications', ignore_missing=True)
237+
frappe.patches.v12_0.set_primary_key_in_series
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import frappe
2+
3+
def execute():
4+
#if current = 0, simply delete the key as it'll be recreated on first entry
5+
frappe.db.sql('delete from `tabSeries` where current = 0')
6+
duplicate_keys = frappe.db.sql('''
7+
SELECT name, max(current) as current
8+
from
9+
`tabSeries`
10+
group by
11+
name
12+
having count(name) > 1
13+
''', as_dict=True)
14+
for row in duplicate_keys:
15+
frappe.db.sql('delete from `tabSeries` where name = %(key)s', {
16+
'key': row.name
17+
})
18+
if row.current:
19+
frappe.db.sql('insert into `tabSeries`(`name`, `current`) values (%(name)s, %(current)s)', row)
20+
frappe.db.commit()
21+
frappe.db.sql('ALTER table `tabSeries` ADD PRIMARY KEY IF NOT EXISTS (name)')

0 commit comments

Comments
 (0)