/* _up/prologue.inc
 * Prologue of database schema update script for Facilitor, to be included in every schema update
 *
 * $Revision$
 * $Id$
 */
#include "modules.h"
#include "comsql.h"

/* Check the current version number. If it is different from the expected version
 * the user has the option to cancel the upgrace by pressing Ctrl-C.
 *  If you want to use this script in a non-interactive fashion, use e.g.
        DEFINE ACCEPT_DB33=1
        @@DB32TO33.SQL
    in a script
 * At the end the schema version and upgrade date are updated.
 */
SET HEADING OFF
SET TERMOUT ON
SET FEEDBACK OFF
SET ECHO OFF
SET PAGESIZE 0

#define _MKVER(a) STRINGIZE(a)

column fcltlogfile new_value fcltlogfile NOPRINT;
select _MKVER(_VER) || '_' || UPPER(USER || '_' || sys_context('USERENV', 'SERVICE_NAME') || '_' || sys_context('USERENV', 'SERVER_HOST')) || '_' || to_char(sysdate,'YYYYMMDD_HH24MISS') || '.lst' as fcltlogfile from dual;
spool &fcltlogfile

WHENEVER SQLERROR EXIT;
DECLARE
    tekst VARCHAR2 (1000);
BEGIN
    CASE ASCII('€')
        WHEN 191 THEN tekst :='WE8ISO8859P1';
        WHEN 128 THEN tekst :='WE8MSWIN1252'; -- De enige goede
        ELSE tekst :='UNKNOWN ';
    END CASE;
    IF tekst <> 'WE8MSWIN1252' THEN
        tekst := '=================================================================' || CHR(10)
              || 'Wrong clientside NLS_LANG ['|| TO_CHAR(ASCII('€')) ||'] for this Facilitor upgrade.' || CHR(10)
              || 'WE8MSWIN1252 is required, we suspect '|| tekst || CHR(10)
              || 'Please enter before you start SQL*Plus:' || CHR(10)
              || ' on Windows:' || CHR(10)
              || '    SET NLS_LANG=.WE8MSWIN1252' || CHR(10)
              || ' on Unix:' || CHR(10)
              || '    export NLS_LANG=.WE8MSWIN1252' || CHR(10)
              || 'This upgrade script is now cancelled!' || CHR(10)
              || '=================================================================' || CHR(10);
        RAISE_APPLICATION_ERROR(-20000, 'OR'||'A-' || CHR(10) || tekst);
    END IF;
    BEGIN
        SELECT table_name
          INTO tekst
          FROM user_tables
         WHERE table_name = 'FAC_VERSION';
    EXCEPTION
      WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(-20000, 'Could not find table FAC_VERSION. Not a Facilitor database?');
    END;
END;
/
WHENEVER SQLERROR CONTINUE;

PROMPT
PROMPT ************************************************************************
PROMPT UPGRADE of Facilitor schema to version EVALCONCAT(DB, _DBV) $Revision$
PROMPT Copyright (c) 1996-2019 Aareon Nederland B.V., the Netherlands. All rights reserved.
PROMPT
SELECT 'The current version of Facilitor@' || USER || ' is: ' || fac.getdbversion()
  FROM DUAL;
PROMPT After the upgrade the schema version will be: EVALCONCAT(DB, _DBV)
PROMPT
PROMPT                  PLEASE VERIFY THESE SCHEMA NUMBERS!
PROMPT
PROMPT  If these versions are not sequencing then execution of this script
PROMPT           should be stopped by pressing Ctrl-C right now!
PROMPT
PROMPT Only run this script when no users or processes are using Facilitor
PROMPT
PROMPT ************************************************************************
PROMPT
PROMPT Press Enter to continue
SET DEFINE ON
PROMPT EVALCONCAT(&&ACCEPT_DB, _DBV)
PROMPT
PROMPT The upgrade continues, be patient. You will be notified when we are done...
PROMPT

SET DEFINE OFF /* Pas na de accept! */
SET HEADING OFF
SET FEEDBACK ON
SET VERIFY OFF
SET LINESIZE 1000
SET TIME ON
SET SERVEROUTPUT ON SIZE UNLIMITED;
variable update_start number
exec :update_start := dbms_utility.get_time
/* om zeker te weten dat eventuele conversies goed gaan */
ALTER SESSION SET nls_territory='america';

SELECT 'Current os_user: ' || SYS_CONTEXT( 'userenv', 'os_user' ) FROM DUAL;

PROMPT This script was run in user:
SHOW USER
SELECT 'The version of Facilitor@' || USER || ' was ' || fac.getdbversion()
  FROM DUAL;

SET HEADING OFF
SET LINESIZE 132
/* first compile all views, triggers etc */
BEGIN DBMS_UTILITY.COMPILE_SCHEMA(USER, FALSE); END;
/
PROMPT List of all invalid objects *before* Facilitor upgrade.
SELECT 'OR'||'A-'||'DB'||_DBV_STRING||' Warning: ' || object_type || ' ' || object_name || ' was ' || status || ' before the upgrade.'
  FROM user_objects
 WHERE object_type IN ('VIEW', 'TRIGGER', 'PACKAGE', 'PROCEDURE', 'FUNCTION', 'PACKAGE BODY')
   AND status <> 'VALID'
 ORDER BY object_name;

SET ECHO ON
