<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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.0.xsd">

	<changeSet author="athou" id="recreate-fes-index">
		<preConditions onFail="MARK_RAN" onFailMessage="index already exists">
			<not>
				<indexExists tableName="FEEDENTRYSTATUSES" indexName="sub_entry_read_index" />
			</not>
		</preConditions>
		<createIndex indexName="sub_entry_read_index" tableName="FEEDENTRYSTATUSES"
			unique="false">
			<column name="subscription_id" />
			<column name="entry_id" />
			<column name="read_status" />
		</createIndex>
	</changeSet>
	
	<changeSet author="athou" id="drop-fes-index">
		<preConditions onFail="MARK_RAN" onFailMessage="index not found, skip drop index">
			<indexExists tableName="FEEDENTRYSTATUSES" indexName="subscription_id" />
		</preConditions>
		<dropIndex tableName="FEEDENTRYSTATUSES" indexName="subscription_id" />
	</changeSet>

	<changeSet author="athou" id="drop-sequences">
		<preConditions onFail="MARK_RAN" onFailMessage="table does not exist">
			<tableExists tableName="hibernate_sequences" />
		</preConditions>
		<dropTable tableName="hibernate_sequences" />
	</changeSet>

	<changeSet author="athou" id="recreate-sequences">
		<validCheckSum>7:094e881ed7e4fa14fb1f61147ce44acc</validCheckSum>
		<validCheckSum>7:f4bf2004a72bb41daa128df5771af902</validCheckSum>
		<preConditions onFail="MARK_RAN" onFailMessage="table already exists">
			<not>
				<tableExists tableName="hibernate_sequences" />
			</not>
		</preConditions>
		<createTable tableName="hibernate_sequences">
			<column name="sequence_name" type="VARCHAR(255)" />
			<column name="sequence_next_hi_value" type="INT" />
		</createTable>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'APPLICATIONSETTINGS', COALESCE(ceiling(max(id) / 1000 + 2), 1) from APPLICATIONSETTINGS</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'FEEDCATEGORIES', COALESCE(ceiling(max(id) / 1000 + 2), 1) from FEEDCATEGORIES</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'FEEDENTRIES', COALESCE(ceiling(max(id) / 1000 + 2), 1) from FEEDENTRIES</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'FEEDENTRYCONTENTS', COALESCE(ceiling(max(id) / 1000 + 2), 1) from FEEDENTRYCONTENTS</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'FEEDENTRYSTATUSES', COALESCE(ceiling(max(id) / 1000 + 2), 1) from FEEDENTRYSTATUSES</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'FEEDS', COALESCE(ceiling(max(id) / 1000 + 2), 1) from FEEDS</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'FEEDSUBSCRIPTIONS', COALESCE(ceiling(max(id) / 1000 + 2), 1) from FEEDSUBSCRIPTIONS</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'USERROLES', COALESCE(ceiling(max(id) / 1000 + 2), 1) from USERROLES</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'USERS', COALESCE(ceiling(max(id) / 1000 + 2), 1) from USERS</sql>
		<sql>insert into hibernate_sequences(sequence_name, sequence_next_hi_value) select 'USERSETTINGS', COALESCE(ceiling(max(id) / 1000 + 2), 1) from USERSETTINGS</sql>
	</changeSet>

	<changeSet author="athou" id="add-log-level">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="APPLICATIONSETTINGS"
					columnName="logLevel" />
			</not>
		</preConditions>
		<addColumn tableName="APPLICATIONSETTINGS">
			<column name="logLevel" type="VARCHAR(255)" />
		</addColumn>
	</changeSet>
	
	<changeSet author="athou" id="add-push-hub">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDS"
					columnName="pushHub" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDS">
			<column name="pushHub" type="VARCHAR(2048)" />
		</addColumn>
	</changeSet>
	
	<changeSet author="athou" id="add-push-topic">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDS"
					columnName="pushTopic" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDS">
			<column name="pushTopic" type="VARCHAR(2048)" />
		</addColumn>
	</changeSet>
	
	<changeSet author="athou" id="add-push-lastping">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDS"
					columnName="pushLastPing" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDS">
			<column name="pushLastPing" type="DATETIME" />
		</addColumn>
	</changeSet>
				
	<changeSet author="athou" id="add-lastpublished">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDS"
					columnName="lastPublishedDate" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDS">
			<column name="lastPublishedDate" type="DATETIME" />
		</addColumn>
	</changeSet>			
			
	<changeSet author="athou" id="add-lastcontenthash">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDS"
					columnName="lastContentHash" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDS">
			<column name="lastContentHash" type="VARCHAR(40)" />
		</addColumn>
	</changeSet>	
	
	<changeSet author="athou" id="add-lastinterval">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDS"
					columnName="averageEntryInterval" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDS">
			<column name="averageEntryInterval" type="BIGINT" />
		</addColumn>
	</changeSet>		
			
	<changeSet author="athou" id="add-lastentrydate">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDS"
					columnName="lastEntryDate" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDS">
			<column name="lastEntryDate" type="DATETIME" />
		</addColumn>
	</changeSet>
	
	<changeSet author="athou" id="add-cat-position">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDCATEGORIES"
					columnName="position" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDCATEGORIES">
			<column name="position" type="INT" />
		</addColumn>
	</changeSet>
	
	<changeSet author="athou" id="add-sub-position">
		<preConditions onFail="MARK_RAN" onFailMessage="column already exists">
			<not>
				<columnExists tableName="FEEDSUBSCRIPTIONS"
					columnName="position" />
			</not>
		</preConditions>
		<addColumn tableName="FEEDSUBSCRIPTIONS">
			<column name="position" type="INT" />
		</addColumn>
	</changeSet>
	
	<changeSet author="athou" id="drop-sequence">
		<preConditions onFail="MARK_RAN" onFailMessage="table does not exist">
			<tableExists tableName="hibernate_sequence" />
		</preConditions>
		<dropTable tableName="hibernate_sequence" />
	</changeSet>
	
	<changeSet author="athou" id="drop-old-pushinfos">
		<preConditions onFail="MARK_RAN" onFailMessage="table does not exist">
			<tableExists tableName="FEEDPUSHINFOS" />
		</preConditions>
		<dropTable tableName="FEEDPUSHINFOS" />
	</changeSet>
	
	<changeSet author="athou" id="add-topic-hash">
		<addColumn tableName="FEEDS">
			<column name="push_topic_hash" type="VARCHAR(40)" />
		</addColumn>
		
		<createIndex tableName="FEEDS" indexName="push_topic_hash_index">
			<column name="push_topic_hash"></column>
		</createIndex>
	</changeSet>

	<changeSet author="athou" id="add-author">
		<addColumn tableName="FEEDENTRIES">
			<column name="author" type="VARCHAR(128)" />
		</addColumn>
	</changeSet>	

	<changeSet author="athou" id="add-inserted-index">
		<createIndex tableName="FEEDENTRIES" indexName="inserted_index">
			<column name="inserted"></column>
		</createIndex>
	</changeSet>	
		
	<changeSet author="athou" id="rename-lang">
		<renameColumn tableName="USERSETTINGS" oldColumnName="language" newColumnName="user_lang" columnDataType="VARCHAR(4)" />
	</changeSet>
	
	<changeSet author="athou" id="norwegian-migration">
		<sql>update USERSETTINGS set user_lang='nb' where user_lang='no'</sql>
	</changeSet>
	
	<changeSet author="athou" id="add-user-created">
		<validCheckSum>3:b1bbf8d559ac25b785751704f2d24a91</validCheckSum>
		<validCheckSum>7:5bd8b28aadce012b56f003539ce99957</validCheckSum>
		<addColumn tableName="USERS">
		    <column name="created" type="DATETIME" />
		</addColumn>
	</changeSet>	
		
	<changeSet author="athou" id="add-proxy-setting">
		<validCheckSum>7:ffca06665d2dc182bd3cb718e62e98f0</validCheckSum>
		<addColumn tableName="APPLICATIONSETTINGS">
		    <column name="imageProxyEnabled" type="BIT" />
		</addColumn>
		<update tableName="APPLICATIONSETTINGS">
			<column name="imageProxyEnabled" valueBoolean="false"></column>
		</update>	
	</changeSet>
	
	<changeSet author="athou" id="add-query-timeout-setting">
		<addColumn tableName="APPLICATIONSETTINGS">
		    <column name="queryTimeout" type="INT" />
		</addColumn>
		<update tableName="APPLICATIONSETTINGS">
			<column name="queryTimeout" valueNumeric="0"></column>
		</update>	
	</changeSet>
	
	<changeSet author="athou" id="add-normalized-url">
		<addColumn tableName="FEEDS">
		    <column name="normalizedUrl" type="VARCHAR(2048)" />
		</addColumn>
		<addColumn tableName="FEEDS">
		    <column name="normalizedUrlHash" type="VARCHAR(40)" />
		</addColumn>
		<createIndex indexName="norm_url_hash_index" tableName="FEEDS"
			unique="false">
			<column name="normalizedUrlHash" />
		</createIndex>
	</changeSet>
	
	<changeSet author="athou" id="add-pause-crawling">
		<addColumn tableName="APPLICATIONSETTINGS">
		    <column name="crawlingPaused" type="BIT" />
		</addColumn>
		<update tableName="APPLICATIONSETTINGS">
			<column name="crawlingPaused" valueBoolean="false"></column>
		</update>	
	</changeSet>

</databaseChangeLog>
