<?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 INDEX temp_authors_s_id ON temp_authors (submission_id)</query>
		<query>CREATE INDEX temp_authors_a_s_id ON temp_authors (author_id, submission_id)</query>
		<query>CREATE TABLE temp_submissions AS SELECT * FROM submissions</query>
		<query>CREATE INDEX temp_submissions_s_id ON temp_submissions (submission_id)</query>
		<query>CREATE TABLE temp_submission_galleys AS SELECT * FROM submission_galleys</query>
		<query>CREATE INDEX temp_submission_galleys_s_id ON temp_submission_galleys (submission_id)</query>

		<!--
			temp_submission_galleys does not have an auto increment primary key when it is copied. When
			moving supplementary files over to galleys, new entries are added to the table, so the table
			needs to fully mimic the original table re: functionality.
		 -->
		<query driver="mysql,mysqli">ALTER TABLE temp_submission_galleys MODIFY COLUMN galley_id bigint NOT NULL AUTO_INCREMENT PRIMARY KEY;</query>
		<query driver="postgres,postgres64,postgres7,postgres8,postgres9">CREATE SEQUENCE galleySeq INCREMENT 1 OWNED BY temp_submission_galleys.galley_id</query>
		<query driver="postgres,postgres64,postgres7,postgres8,postgres9">SELECT setval('galleySeq', (SELECT max(galley_id) FROM temp_submission_galleys))</query>
		<query driver="postgres,postgres64,postgres7,postgres8,postgres9">ALTER TABLE temp_submission_galleys ALTER column galley_id SET DEFAULT nextval('galleySeq')</query>
		<query driver="postgres,postgres64,postgres7,postgres8,postgres9">ALTER TABLE temp_submission_galleys ALTER column galley_id SET NOT NULL</query>
		<query driver="postgres,postgres64,postgres7,postgres8,postgres9">ALTER TABLE temp_submission_galleys ADD PRIMARY KEY (galley_id)</query>

		<query>CREATE TABLE temp_published_submissions AS SELECT * FROM published_submissions</query>
		<query>CREATE INDEX temp_published_submissions_s_id ON temp_published_submissions (submission_id)</query>
		<query>CREATE TABLE temp_citations AS SELECT * FROM citations</query>
		<query>CREATE INDEX temp_citations_s_id ON temp_citations (submission_id)</query>
		<query>CREATE INDEX temp_citations_c_s_id ON temp_citations (citation_id, submission_id)</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 driver="postgres,postgres64,postgres7,postgres8,postgres9">UPDATE submissions SET status=1 FROM submissions AS s LEFT JOIN published_submissions ps ON (ps.submission_id = s.submission_id) WHERE s.submission_id = submissions.submission_id AND s.status=3 AND ps.submission_id IS NULL</query>
		<query driver="mysql,mysqli">UPDATE submissions s LEFT JOIN published_submissions ps ON (ps.submission_id = s.submission_id) SET s.status=1 WHERE s.status=3 AND ps.submission_id 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>
