db-changelog-3.2.0.xml 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
  #%L
  Quadrige3 Core :: Quadrige3 Server Core
  $Id:$
  $HeadURL:$
  %%
  Copyright (C) 2017 Ifremer
  %%
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  #L%
  -->

<!-- Changes history:
  11/06/2019 Modify PHOTO table (Mantis #47677)
  -->
<databaseChangeLog
	xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd"
	logicalFilePath="https://gitlab.ifremer.fr/quadrige/quadrige3-core/blob/master/quadrige3-core-server/src/main/resources/fr/ifremer/quadrige3/core/db/changelog/pgsql/db-changelog-3.2.0.xml">

	<preConditions onFail="HALT">
		<dbms type="postgresql"/>
	</preConditions>

	<!-- Add UPDATE_CAMPAIGN_PROG procedure -->
PECQUOT's avatar
PECQUOT committed
39
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-1">
40
		<createProcedure dbms="postgresql" procedureName="UPDATE_CAMPAIGN_PROG">
41
			create or replace function UPDATE_CAMPAIGN_PROG (P_PROG_CD VARCHAR) returns void AS $$
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
			BEGIN
			-- insert a line in CAMPAIGN_PROG is not already exists
			-- for surveys in specified program, with existing campaign
			-- and updated since the last hour
			INSERT INTO CAMPAIGN_PROG(CAMPAIGN_ID, PROG_CD)
			SELECT DISTINCT s.CAMPAIGN_ID, sp.PROG_CD
			FROM SURVEY s
			INNER JOIN SURVEY_PROG sp ON s.SURVEY_ID = sp.SURVEY_ID
			WHERE sp.PROG_CD = P_PROG_CD
			AND s.CAMPAIGN_ID IS NOT NULL
			AND s.UPDATE_DT > current_timestamp - interval '1 hour';
			EXCEPTION
			WHEN unique_violation THEN
			NULL; -- ignore duplicate values
			WHEN OTHERS THEN
			RAISE;
			END;
59 60 61 62 63
			$$ language plpgsql
		</createProcedure>
	</changeSet>

	<!--  TODO difficult -->
PECQUOT's avatar
PECQUOT committed
64
	<!--  <changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-2">-->
65 66 67 68 69 70
	<!--    <createView viewName="RULE_PRECONDITIONS" replaceIfExists="true">-->
	<!--      <![CDATA[-->
	<!--      ]]>-->
	<!--    </createView>-->
	<!--  </changeSet>-->

71 72 73 74 75 76
	<!-- Add reset_sequence procedure -->
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-3">
		<createProcedure dbms="postgresql" procedureName="reset_sequence">
			CREATE OR REPLACE FUNCTION reset_sequence (table_name text, column_name text, sequence_name text) RETURNS bigint
			VOLATILE STRICT LANGUAGE plpgsql AS
			$$
77 78 79 80 81 82 83 84 85 86 87 88
			DECLARE
			new_sequence_value bigint;
			query varchar;
			BEGIN
			query = format('select coalesce(max(%I),1) from %I', column_name, table_name);
			raise notice '%', query;
			execute query into new_sequence_value;
			query = format('select setval(''%s'', %s)', sequence_name, new_sequence_value);
			raise notice '%', query;
			EXECUTE query;
			return new_sequence_value;
			END;
89 90 91 92 93 94 95 96
			$$;
		</createProcedure>
	</changeSet>

	<!-- Add reset_all_sequences procedure -->
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-4">
		<createProcedure dbms="postgresql" procedureName="reset_all_sequences">
			create or replace function reset_all_sequences() returns setof record language sql as $$
97 98 99 100 101 102 103 104 105 106 107 108 109 110
			select kcu.table_name, kcu.column_name, s.sequence_name,
			reset_sequence(kcu.table_name, kcu.column_name, s.sequence_name) as new_sequence_value
			from information_schema.table_constraints tco
			inner join information_schema.key_column_usage kcu
			on kcu.constraint_name = tco.constraint_name
			and kcu.constraint_schema = tco.constraint_schema
			and kcu.constraint_name = tco.constraint_name
			inner join information_schema.sequences s
			on s.sequence_schema = kcu.table_schema
			where tco.constraint_type = 'PRIMARY KEY'
			and s.sequence_name = kcu.table_name || '_seq'
			and tco.table_schema = user
			and kcu.table_name not in ('wfd_water_body')
			order by kcu.table_name;
111 112 113 114 115 116 117 118 119
			$$;
		</createProcedure>
	</changeSet>

	<!-- call reset_all_sequences -->
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-5">
		<sql>select reset_all_sequences();</sql>
	</changeSet>

120 121 122 123 124
	<!--	Create missing columns (used for backward compatibility) -->
	<include relativeToChangelogFile="true" file="create-missing-columns.xml"/>

	<!--	Remove some unused columns -->
	<changeSet author="peck7 (generated)" id="1569947116576-4263">
PECQUOT's avatar
PECQUOT committed
125 126 127 128
		<preConditions onFail="MARK_RAN">
			<primaryKeyExists tableName="extract_table" primaryKeyName="extract_table_pkey"/>
		</preConditions>
		<dropPrimaryKey tableName="extract_table" constraintName="extract_table_pkey"/>
129
	</changeSet>
130
	<changeSet author="peck7 (generated)" id="1569947116576-4268">
PECQUOT's avatar
PECQUOT committed
131 132 133 134
		<preConditions onFail="MARK_RAN">
			<columnExists tableName="extract_table" columnName="extract_table_id"/>
		</preConditions>
		<dropColumn tableName="extract_table" columnName="extract_table_id"/>
135
	</changeSet>
136 137 138 139 140 141
	<!--	<changeSet author="peck7 (generated)" id="1569947116576-4266">-->
	<!--		<dropColumn columnName="prod_sector_area_id" tableName="PROD_SECTOR_AREA"/>-->
	<!--	</changeSet>-->
	<!--	<changeSet author="peck7 (generated)" id="1569947116576-4267">-->
	<!--		<dropColumn columnName="selection_item_id" tableName="SELECTION_ITEM"/>-->
	<!--	</changeSet>-->
142

143 144
	<!--	Change columns type  -->
	<include relativeToChangelogFile="true" file="modify-data-type.xml"/>
145 146 147 148 149 150 151 152

	<!--	Create missing remarks -->
	<include relativeToChangelogFile="true" file="create-remarks.xml"/>

	<!--	Create missing SANDRE tables -->
	<include relativeToChangelogFile="true" file="create-sandre-tables.xml"/>

	<!--	Create constraints -->
153
	<changeSet author="peck7 (generated)" id="1569947116576-4130">
PECQUOT's avatar
PECQUOT committed
154 155 156 157 158
		<preConditions onFail="MARK_RAN">
			<not>
				<foreignKeyConstraintExists foreignKeyTableName="MEASUREMENT" foreignKeyName="FK_MEAS_PMFM_QUAL"/>
			</not>
		</preConditions>
159 160 161 162
		<addForeignKeyConstraint baseColumnNames="QUAL_VALUE_ID,PMFM_ID" baseTableName="MEASUREMENT" constraintName="FK_MEAS_PMFM_QUAL"
														 referencedColumnNames="QUAL_VALUE_ID,PMFM_ID" referencedTableName="PMFM_QUAL_VALUE"/>
	</changeSet>
	<changeSet author="peck7 (generated)" id="1569947116576-4243">
PECQUOT's avatar
PECQUOT committed
163 164 165 166 167
		<preConditions onFail="MARK_RAN">
			<not>
				<foreignKeyConstraintExists foreignKeyTableName="TAXON_MEASUREMENT" foreignKeyName="FK_TAXON_MEAS_PMFM_QUAL"/>
			</not>
		</preConditions>
168 169 170 171
		<addForeignKeyConstraint baseColumnNames="QUAL_VALUE_ID,PMFM_ID" baseTableName="TAXON_MEASUREMENT" constraintName="FK_TAXON_MEAS_PMFM_QUAL"
														 referencedColumnNames="QUAL_VALUE_ID,PMFM_ID" referencedTableName="PMFM_QUAL_VALUE"/>
	</changeSet>
	<changeSet author="peck7 (generated)" id="1569947116576-4244">
PECQUOT's avatar
PECQUOT committed
172 173 174 175 176
		<preConditions onFail="MARK_RAN">
			<not>
				<foreignKeyConstraintExists foreignKeyTableName="TAXON_MEASUREMENT" foreignKeyName="FK_TAXON_MEAS_TAXON_NAME"/>
			</not>
		</preConditions>
177 178 179
		<addForeignKeyConstraint baseColumnNames="TAXON_NAME_ID" baseTableName="TAXON_MEASUREMENT" constraintName="FK_TAXON_MEAS_TAXON_NAME"
														 referencedColumnNames="TAXON_NAME_ID" referencedTableName="TAXON_NAME"/>
	</changeSet>
180 181

	<!-- Add PK on ALRT_MON_LOC_PROG -->
PECQUOT's avatar
PECQUOT committed
182
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-20">
183 184 185 186
		<preConditions onFail="MARK_RAN">
			<tableExists tableName="ALRT_MON_LOC_PROG"/>
		</preConditions>
		<dropPrimaryKey tableName="ALRT_MON_LOC_PROG"/>
187
		<addPrimaryKey tableName="ALRT_MON_LOC_PROG" columnNames="MON_LOC_ID,PROG_CD"/>
188 189 190
	</changeSet>

	<!-- add PK on ALRT_MON_LOC_ORDER_ITEM -->
PECQUOT's avatar
PECQUOT committed
191
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-21">
192 193 194 195 196 197 198
		<preConditions onFail="MARK_RAN">
			<tableExists tableName="ALRT_MON_LOC_ORDER_ITEM"/>
		</preConditions>
		<dropPrimaryKey tableName="ALRT_MON_LOC_ORDER_ITEM"/>
		<addPrimaryKey tableName="ALRT_MON_LOC_ORDER_ITEM" columnNames="ORDER_ITEM_TYPE_CD,ORDER_ITEM_CD,MON_LOC_ID"/>
	</changeSet>

PECQUOT's avatar
PECQUOT committed
199
	<!-- Create table temp_query_parameter	-->
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	<!--	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-22">-->
	<!--		<preConditions onFail="MARK_RAN">-->
	<!--			<not>-->
	<!--				<tableExists tableName="temp_query_parameter"/>-->
	<!--			</not>-->
	<!--		</preConditions>-->
	<!--		<createTable tableName="temp_query_parameter">-->
	<!--			<column name="id" type="bigint">-->
	<!--				<constraints primaryKey="true" primaryKeyName="temp_query_parameter_pkey"/>-->
	<!--			</column>-->
	<!--			<column name="parameter_name" type="varchar(50)">-->
	<!--				<constraints nullable="false"/>-->
	<!--			</column>-->
	<!--			<column name="grouping_key" type="bigint"/>-->
	<!--			<column name="numerical_value" type="numeric(38,7)"/>-->
	<!--			<column name="alphanumerical_value" type="varchar(50)"/>-->
	<!--			<column name="quser_id" type="bigint">-->
	<!--				<constraints nullable="false"/>-->
	<!--			</column>-->
	<!--		</createTable>-->
	<!--	</changeSet>-->
	<!--	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-22b">-->
	<!--		<preConditions onFail="MARK_RAN">-->
	<!--			<not>-->
	<!--				<sequenceExists sequenceName="temp_query_parameter_seq"/>-->
	<!--			</not>-->
	<!--		</preConditions>-->
	<!--		<createSequence sequenceName="temp_query_parameter_seq"/>-->
	<!--	</changeSet>-->
PECQUOT's avatar
PECQUOT committed
229

PECQUOT's avatar
PECQUOT committed
230 231 232 233 234 235 236 237
	<!--	 Remove table temp_query_parameter	-->
		<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-22">
			<preConditions onFail="MARK_RAN">
					<tableExists tableName="temp_query_parameter"/>
			</preConditions>
			<dropTable tableName="temp_query_parameter"/>
		</changeSet>

PECQUOT's avatar
PECQUOT committed
238 239 240 241 242 243 244 245 246 247
	<!-- add missing pk on unit -->
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-23">
		<preConditions onFail="MARK_RAN">
			<not>
				<primaryKeyExists tableName="unit"/>
			</not>
		</preConditions>
		<addPrimaryKey tableName="unit" columnNames="unit_id" constraintName="pk_unit"/>
	</changeSet>

PECQUOT's avatar
PECQUOT committed
248 249 250
	<!-- observed_habitat: change pk -->
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-24a">
		<preConditions onFail="MARK_RAN">
251
			<primaryKeyExists tableName="observed_habitat" primaryKeyName="pk_observed_habitat"/>
PECQUOT's avatar
PECQUOT committed
252
		</preConditions>
253
		<dropPrimaryKey tableName="observed_habitat" constraintName="pk_observed_habitat"/>
PECQUOT's avatar
PECQUOT committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	</changeSet>
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-24b">
		<preConditions onFail="MARK_RAN">
			<columnExists tableName="observed_habitat" columnName="observed_habitat_id"/>
		</preConditions>
		<dropColumn tableName="observed_habitat" columnName="observed_habitat_id"/>
	</changeSet>
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-24c">
		<preConditions onFail="MARK_RAN">
			<not>
				<primaryKeyExists tableName="observed_habitat" primaryKeyName="pk_observed_habitat"/>
			</not>
		</preConditions>
		<addPrimaryKey tableName="observed_habitat" constraintName="pk_observed_habitat" columnNames="survey_id,eunis_typ_id"/>
	</changeSet>


271 272
	<!--	Create indexes -->
	<include relativeToChangelogFile="true" file="create-indexes.xml"/>
273 274

	<!-- update SYSTEM_VERSION -->
PECQUOT's avatar
PECQUOT committed
275
	<changeSet author="ludovic.pecquot@e-is.pro" id="1569947116576-5000" runOnChange="true">
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
		<delete tableName="SYSTEM_VERSION">
			<where>SYSTEM_VERSION_LB='3.2.0'</where>
		</delete>
		<insert tableName="SYSTEM_VERSION">
			<column name="SYSTEM_VERSION_ID" valueComputed="nextval('SYSTEM_VERSION_SEQ')"/>
			<column name="SYSTEM_VERSION_LB">3.2.0</column>
			<column name="SYSTEM_VERSION_DC">
				Initial pgsql schema version
			</column>
			<column name="SYSTEM_VERSION_CREATION_DT" valueComputed="now()"/>
			<column name="UPDATE_DT" valueComputed="now()"/>
			<column name="SYSTEM_VERSION_CM">
			</column>
		</insert>
	</changeSet>

</databaseChangeLog>