-- 
--  This file is part of the SavaPage project <https://www.savapage.org>.
--  Copyright (c) 2011-2020 Datraverse B.V.
--  Author: Rijk Ravestein.
-- 
--  SPDX-FileCopyrightText: 2011-2020 Datraverse B.V. <info@datraverse.com>
--  SPDX-License-Identifier: AGPL-3.0-or-later
-- 
--  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 Affero 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 <https://www.gnu.org/licenses/>.
-- 
--  For more information, please contact Datraverse B.V. at this
--  address: info@datraverse.com
-- 

-- |--------------------------------------------------------------------------
-- | Schema Version 1.10 Upgrade Script - PostgreSQL
-- | 
-- | Upgrade from Application Version 1.1.0 to 1.2.0
-- | 
-- |--------------------------------------------------------------------------

-- +==========================================================================
-- | Set MINOR version
-- |
-- | Force "division by zero" error if current version is not 1.9
-- +==========================================================================

UPDATE TBL_CONFIG SET PROPERTY_VALUE = '10' 
WHERE PROPERTY_NAME = 'system.schema-version-minor'
  AND 1 = (SELECT 1 / COUNT(*) FROM TBL_CONFIG WHERE 
    PROPERTY_NAME = 'system.schema-version'
      AND PROPERTY_VALUE = '1')
  AND 1 = (SELECT 1 / COUNT(*) FROM TBL_CONFIG WHERE 
    PROPERTY_NAME = 'system.schema-version-minor'
      AND PROPERTY_VALUE = '9');

-- +==========================================================================
-- | DATA CHANGES
-- +==========================================================================

-- +--------------------------------------------------------
-- | tbl_ipp_queue
-- +--------------------------------------------------------
UPDATE tbl_ipp_queue SET url_path = '/' WHERE url_path = '';

-- +--------------------------------------------------------
-- | tbl_printer
-- +--------------------------------------------------------
UPDATE tbl_printer SET display_name = printer_name WHERE display_name = '';

-- +--------------------------------------------------------
-- | tbl_print_out
-- +--------------------------------------------------------
UPDATE tbl_print_out SET cups_job_sheets = 'none' WHERE cups_job_sheets = '';
UPDATE tbl_print_out SET cups_page_set = 'all' WHERE cups_page_set = '';

-- +--------------------------------------------------------
-- | tbl_user
-- +--------------------------------------------------------
UPDATE tbl_user SET user_name = '=erased=' WHERE deleted = true AND user_name = '';
UPDATE tbl_user SET external_user_name = '=erased=' WHERE deleted = true AND external_user_name = '';
UPDATE tbl_user SET external_user_name = user_name WHERE external_user_name = '';

-- +--------------------------------------------------------
-- | tbl_account
-- +--------------------------------------------------------
UPDATE tbl_account SET account_name = '=erased=' WHERE deleted = true AND account_name = '';
UPDATE tbl_account SET account_name_lower = '=erased=' WHERE deleted = true AND account_name_lower = '';
UPDATE tbl_account SET account_name_lower = account_name WHERE account_name_lower = '';

-- +--------------------------------------------------------
-- | tbl_pos_purchase
-- +--------------------------------------------------------
alter table tbl_pos_purchase add column payment_comment varchar(255);
update tbl_pos_purchase set payment_comment = comment;
alter table tbl_pos_purchase drop column comment;

-- end-of-script
