# -*- coding: utf-8 -*-
# Copyright (c) 2021, JINGROW
# For license information, please see license.txt


import jingrow


def execute():
	jingrow.reload_pg("jcloud", "pagetype", "deploy_candidate_difference")
	jingrow.reload_pg("jcloud", "pagetype", "deploy_candidate_difference_app")
	jingrow.reload_pg("jcloud", "pagetype", "app_release_difference")

	jingrow.db.delete("Deploy Candidate Difference App", {"changed": False})
	differences = jingrow.get_all("Deploy Candidate Difference App", "*")
	for difference in differences:
		release_difference = jingrow.db.exists(
			"App Release Difference",
			{
				"app": difference.app,
				"source_release": difference.source_release,
				"destination_release": difference.destination_release,
			},
		)
		if not release_difference:
			release_difference_pg = jingrow.get_pg(
				{
					"pagetype": "App Release Difference",
					"app": difference.app,
					"deploy_type": difference.deploy_type,
					"source": jingrow.db.get_value("App Release", difference.source_release, "source"),
					"source_release": difference.source_release,
					"source_hash": difference.source_hash,
					"destination_release": difference.destination_release,
					"destination_hash": difference.destination_hash,
					"files": difference.files,
					"github_diff_url": difference.github_diff_url,
				}
			)
			release_difference_pg.db_insert()
			release_difference = release_difference_pg.name
		jingrow.db.set_value(
			"Deploy Candidate Difference App", difference.name, "difference", release_difference
		)
