import jingrow


def execute():
	jingrow.reload_pagetype("Comment")

	if jingrow.db.count("Communication", filters=dict(communication_type="Comment")) > 20000:
		jingrow.db.auto_commit_on_many_writes = True

	for comment in jingrow.get_all("Communication", fields=["*"], filters=dict(communication_type="Comment")):
		new_comment = jingrow.new_pg("Comment")
		new_comment.comment_type = comment.comment_type
		new_comment.comment_email = comment.sender
		new_comment.comment_by = comment.sender_full_name
		new_comment.subject = comment.subject
		new_comment.content = comment.content or comment.subject
		new_comment.reference_pagetype = comment.reference_pagetype
		new_comment.reference_name = comment.reference_name
		new_comment.link_pagetype = comment.link_pagetype
		new_comment.link_name = comment.link_name
		new_comment.creation = comment.creation
		new_comment.modified = comment.modified
		new_comment.owner = comment.owner
		new_comment.modified_by = comment.modified_by
		new_comment.db_insert()

	if jingrow.db.auto_commit_on_many_writes:
		jingrow.db.auto_commit_on_many_writes = False

	# clean up
	jingrow.db.delete("Communication", {"communication_type": "Comment"})
