import jingrow


def execute():
	old_settings = {
		"Error Log": get_current_setting("clear_error_log_after"),
		"System Log": get_current_setting("clear_system_log_after"),
		"Email Queue": get_current_setting("clear_email_queue_after"),
	}

	jingrow.reload_pg("core", "pagetype", "Logs To Clear")
	jingrow.reload_pg("core", "pagetype", "Log Settings")

	log_settings = jingrow.get_pg("Log Settings")
	log_settings.add_default_logtypes()

	for pagetype, retention in old_settings.items():
		if retention:
			log_settings.register_pagetype(pagetype, retention)

	log_settings.save()


def get_current_setting(fieldname):
	try:
		return jingrow.db.get_single_value("Log Settings", fieldname)
	except Exception:
		# Field might be gone if patch is reattempted
		pass
