import influxframework


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

	duplicate_keys = influxframework.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:
		influxframework.db.delete("Series", {"name": row.name})
		if row.current:
			influxframework.db.sql("insert into `tabSeries`(`name`, `current`) values (%(name)s, %(current)s)", row)
	influxframework.db.commit()

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