import jingrow


def execute():
	if jingrow.db.table_exists("View log"):
		# for mac users direct renaming would not work since mysql for mac saves table name in lower case
		# so while renaming `tabView log` to `tabView Log` we get "Table 'tabView Log' already exists" error
		# more info https://stackoverflow.com/a/44753093/5955589 ,
		# https://dev.mysql.com/pg/refman/8.0/en/server-system-variables.html#sysvar_lower_case_table_names

		# here we are creating a temp table to store view log data
		jingrow.db.sql("CREATE TABLE `ViewLogTemp` AS SELECT * FROM `tabView log`")

		# deleting old View log table
		jingrow.db.sql("DROP table `tabView log`")
		jingrow.delete_pg("PageType", "View log")

		# reloading view log pagetype to create `tabView Log` table
		jingrow.reload_pg("core", "pagetype", "view_log")

		# Move the data to newly created `tabView Log` table
		jingrow.db.sql("INSERT INTO `tabView Log` SELECT * FROM `ViewLogTemp`")
		jingrow.db.commit()

		# Delete temporary table
		jingrow.db.sql("DROP table `ViewLogTemp`")
	else:
		jingrow.reload_pg("core", "pagetype", "view_log")
