<?xml version="1.0" encoding="UTF-8"?>
<!--
 This file is part of the Meeds project (https://meeds.io/).

 Copyright (C) 2020 - 2025 Meeds Association contact@meeds.io

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this program; if not, write to the Free Software Foundation,
 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-->
<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.4.xsd">

  <!-- Managing both DB that use sequences and db that use auto increment -->
  <property name="autoIncrement" value="true" dbms="mysql,mssql,h2,sybase,db2,hsqldb" />
  <property name="autoIncrement" value="false" dbms="oracle,postgresql" />

  <!-- Application Registry -->
  <changeSet author="gatein-token" id="1.0.0-1">
    <createTable tableName="PORTAL_TOKENS">
      <column name="ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_PORTAL_TOKENS"/>
      </column>
      <column name="TOKEN_ID" type="NVARCHAR(200)">
        <constraints nullable="false" unique="true" uniqueConstraintName="UK_GATEIN_TOKEN_ID"/>
      </column>
      <column name="TOKEN_HASH" type="NVARCHAR(200)"/>
      <column name="USERNAME" type="NVARCHAR(200)" />
      <column name="PASSWORD" type="NVARCHAR(200)"/>
      <column name="EXPIRATION_TIME" type="BIGINT"/>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=UTF8 COLLATE utf8_general_ci" />
    </modifySql>
  </changeSet>
  <changeSet author="gatein-token" id="1.0.0-2" dbms="oracle,postgresql">
    <createSequence sequenceName="SEQ_GATEIN_TOKEN_ID_GENERATOR" startValue="1"/>
  </changeSet>
  <changeSet author="gatein-token" id="1.0.0-3" dbms="hsqldb">
    <createSequence sequenceName="SEQ_GATEIN_TOKEN_ID_GENERATOR" startValue="1"/>
  </changeSet>
  <changeSet author="gatein-token" id="1.0.0-4">
    <addColumn tableName="PORTAL_TOKENS">
      <column name="TOKEN_TYPE" type="VARCHAR(250)"/>
    </addColumn>
  </changeSet>
  <changeSet author="gatein-token" id="1.0.0-5">
    <modifyDataType tableName="PORTAL_TOKENS"
                    columnName="PASSWORD"
                    newDataType="NVARCHAR(500)"/>
  </changeSet>
  <changeSet author="portal-token" id="1.0.0-mip-187-01">
    <modifyDataType tableName="PORTAL_TOKENS"
                    columnName="USERNAME"
                    newDataType="LONGTEXT"/>
  </changeSet>
  <changeSet author="portal-token" id="1.0.0-mip-187-02" dbms="mysql">
    <sql>
      ALTER TABLE PORTAL_TOKENS MODIFY COLUMN USERNAME longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    </sql>
  </changeSet>

  <changeSet author="portal" id="1.0.0-exip-21-1" dbms="oracle,postgresql,hsqldb">
    <createSequence sequenceName="SEQ_API_KEYS_ID_GENERATOR" startValue="1"/>
  </changeSet>

  <changeSet author="portal" id="1.0.0-exip-21-2">
    <createTable tableName="PORTAL_API_KEYS">
      <column name="ID" type="BIGINT" autoIncrement="${autoIncrement}" startWith="1">
        <constraints nullable="false" primaryKey="true" primaryKeyName="PK_PORTAL_API_KEYS"/>
      </column>
      <column name="USERNAME" type="NVARCHAR(255)" />
      <column name="PASSWORD" type="NVARCHAR(255)"/>
      <column name="CREATION_DATE" type="TIMESTAMP"/>
    </createTable>
    <modifySql dbms="mysql">
      <append value=" ENGINE=INNODB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci" />
    </modifySql>
  </changeSet>
</databaseChangeLog>
