<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE data SYSTEM "../../../lib/pkp/dtd/xmlData.dtd">

<!--
	* dbscripts/xml/upgrade/3.2.0_preupdate_versioning.xml
	*
	* Copyright (c) 2013-2020 Simon Fraser University
	* Copyright (c) 2003-2020 John Willinsky
	* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
	*
	* Copy submission data to temporary tables so it can be updated
	* after the publication table modifications are made.
	*
	-->

<data>
	<sql>
		<query>CREATE TABLE temp_authors AS SELECT * FROM authors</query>
		<query>CREATE TABLE temp_submissions AS SELECT * FROM submissions</query>
		<query>CREATE TABLE temp_submission_galleys AS SELECT * FROM submission_galleys</query>
		<query>CREATE TABLE temp_published_submissions AS SELECT * FROM published_submissions</query>
		<query>CREATE TABLE temp_citations AS SELECT * FROM citations</query>
	</sql>
	<!--
		Identify any submissions with STATUS_PUBLISHED that are missing a corresponding entry
		in the published_submissions table and reset them to STATUS_QUEUED. In the past, an
		entry in published_submissions was required before a submission would be public. Now
		it will be public if it has a status of STATUS_PUBLISHED. A submission should never
		have STATUS_PUBLISHED without a published_submissions entry, but this is a
		precautionary measure to prevent unexpectedly making a submission public if the
		status value is incorrect for any submissions.
	-->
	<sql>
		<query>
			UPDATE submissions s
			SET s.status=1
			WHERE s.status=3
			AND (
				SELECT COALESCE(ps.submission_id, NULL)
				FROM published_submissions ps
				WHERE ps.submission_id = s.submission_id
				AND s.status = 3
			) IS NULL
		</query>
	</sql>
	<!-- Modify citations table to refer to publications #5465-->
	<sql>
		<dropindex table="citations" index="citations_submission" />
		<dropindex table="citations" index="citations_submission_seq" />
		<rename table="citations" column="submission_id" to="publication_id" />
	</sql>
</data>
