<?xml version="1.1" 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.10.xsd">

  <changeSet author="mulk" id="1.7-1">
    <createTable tableName="newsletters" schemaName="benki">
      <column name="id" type="INTEGER">
        <constraints primaryKeyName="newsletters_pkey" nullable="false" primaryKey="true"/>
      </column>

      <column name="date" type="TIMESTAMP WITH TIME ZONE">
        <constraints nullable="false"/>
      </column>
    </createTable>

    <addColumn tableName="bookmarks" schemaName="benki">
      <column name="newsletter" type="INTEGER">
        <constraints foreignKeyName="bookmarks_newsletter_fkey" references="benki.newsletters(id)"/>
      </column>
    </addColumn>

    <addColumn tableName="lazychat_messages" schemaName="benki">
      <column name="newsletter" type="INTEGER">
        <constraints foreignKeyName="lazychat_messages_newsletter_fkey" references="benki.newsletters(id)"/>
      </column>
    </addColumn>

    <addColumn tableName="posts" schemaName="benki">
      <column name="newsletter" type="INTEGER">
        <constraints foreignKeyName="posts_newsletter_fkey" references="benki.newsletters(id)"/>
      </column>
    </addColumn>
  </changeSet>

  <changeSet author="mulk" id="1.7-2">
    <createTable tableName="newsletter_subscriptions" schemaName="benki">
      <column name="id" type="SERIAL" autoIncrement="true">
        <constraints primaryKeyName="newsletter_subscriptions_pkey" nullable="false" primaryKey="true"/>
      </column>

      <column name="start_date" type="TIMESTAMP WITH TIME ZONE" defaultValue="now()">
        <constraints nullable="false"/>
      </column>

      <column name="email" type="VARCHAR">
        <constraints nullable="false" unique="true" uniqueConstraintName="newsletter_subscriptions_email_key"/>
      </column>
    </createTable>
  </changeSet>

  <changeSet author="mulk" id="1.7-3">
    <addColumn tableName="newsletter_subscriptions" schemaName="benki">
      <column name="registration_key" type="VARCHAR">
        <constraints unique="true" uniqueConstraintName="newsletter_subscriptions_registration_key_key"/>
      </column>
    </addColumn>

    <createIndex tableName="newsletter_subscriptions"
                 schemaName="benki"
                 indexName="newsletter_subscriptions_registration_key_idx">
      <column name="registration_key"/>
    </createIndex>
  </changeSet>

</databaseChangeLog>
