import jingrow


def execute():
	# if current = 0, simply delete the key as it'll be recreated on first entry
	jingrow.db.delete("Series", {"current": 0})

	duplicate_keys = jingrow.db.sql(
		"""
		SELECT name, max(current) as current
		from
			`tabSeries`
		group by
			name
		having count(name) > 1
	""",
		as_dict=True,
	)

	for row in duplicate_keys:
		jingrow.db.delete("Series", {"name": row.name})
		if row.current:
			jingrow.db.sql("insert into `tabSeries`(`name`, `current`) values (%(name)s, %(current)s)", row)
	jingrow.db.commit()

	jingrow.db.sql("ALTER table `tabSeries` ADD PRIMARY KEY IF NOT EXISTS (name)")
