-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

\set TEST_DBNAME_2 :TEST_DBNAME _2

\c :TEST_DBNAME :ROLE_SUPERUSER

CREATE DATABASE :"TEST_DBNAME_2";

DROP EXTENSION timescaledb;
--no extension
SELECT * FROM test.extension;
SELECT 1;

\c :TEST_DBNAME :ROLE_SUPERUSER
CREATE EXTENSION timescaledb VERSION 'mock-1';
SELECT 1;
SELECT * FROM test.extension;

CREATE EXTENSION IF NOT EXISTS timescaledb VERSION 'mock-1';
CREATE EXTENSION IF NOT EXISTS timescaledb VERSION 'mock-2';

DROP EXTENSION timescaledb;
\set ON_ERROR_STOP 0
--test that we cannot accidentally load another library version
CREATE EXTENSION IF NOT EXISTS timescaledb VERSION 'mock-2';
\set ON_ERROR_STOP 1

\c :TEST_DBNAME :ROLE_SUPERUSER
--no extension
SELECT * FROM test.extension;

SELECT 1;

CREATE EXTENSION timescaledb VERSION 'mock-1';
--same backend as create extension;
SELECT 1;
SELECT * FROM test.extension;

--start new backend;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER

SELECT 1;
SELECT 1;
--test fn call after load
SELECT mock_function();
SELECT * FROM test.extension;

\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
--test fn call as first command
SELECT mock_function();

--use guc to prevent loading
\c :TEST_DBNAME :ROLE_SUPERUSER
SET timescaledb.disable_load = 'on';
SELECT 1;
SELECT 1;
SET timescaledb.disable_load = 'off';
SELECT 1;
\set ON_ERROR_STOP 0
SET timescaledb.disable_load = 'not bool';
\set ON_ERROR_STOP 1

\c :TEST_DBNAME :ROLE_SUPERUSER
RESET ALL;
SELECT 1;

\c :TEST_DBNAME :ROLE_SUPERUSER
SET timescaledb.disable_load TO DEFAULT;
SELECT 1;

\c :TEST_DBNAME :ROLE_SUPERUSER
RESET timescaledb.disable_load;
SELECT 1;

\c :TEST_DBNAME :ROLE_SUPERUSER
SET timescaledb.other = 'on';
SELECT 1;

\set ON_ERROR_STOP 0
--cannot update extension after .so of previous version already loaded
ALTER EXTENSION timescaledb UPDATE TO 'mock-2';
\set ON_ERROR_STOP 1

\c :TEST_DBNAME_2 :ROLE_SUPERUSER
SELECT * FROM test.extension;
CREATE EXTENSION timescaledb VERSION 'mock-1';
SELECT * FROM test.extension;
--start a new backend to update
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
ALTER EXTENSION timescaledb UPDATE TO 'mock-2';
SELECT 1;
SELECT * FROM test.extension;

--ALTER EXTENSION UPDATE works inside a transaction block; BEGIN must not
--load the old version before UPDATE runs as the first command.
DROP EXTENSION timescaledb;
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
CREATE EXTENSION timescaledb VERSION 'mock-1';
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
BEGIN;
ALTER EXTENSION timescaledb UPDATE TO 'mock-2';
COMMIT;
SELECT 1;
SELECT * FROM test.extension;

--drop extension
DROP EXTENSION timescaledb;
SELECT 1;
SELECT * FROM test.extension;

\c :TEST_DBNAME_2 :ROLE_SUPERUSER
CREATE EXTENSION timescaledb VERSION 'mock-2';
SELECT 1;
SELECT * FROM test.extension;

-- test db 1 still has old version
\c :TEST_DBNAME :ROLE_SUPERUSER
SELECT 1;
SELECT * FROM test.extension;

--try a broken upgrade
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
SELECT * FROM test.extension;
\set ON_ERROR_STOP 0
ALTER EXTENSION timescaledb UPDATE TO 'mock-3';
\set ON_ERROR_STOP 1
--should still be on mock-2
SELECT 1;
SELECT * FROM test.extension;

--drop extension
DROP EXTENSION timescaledb;
SELECT 1;
SELECT * FROM test.extension;

--create extension anew, only upgrade was broken
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
CREATE EXTENSION timescaledb VERSION 'mock-3';
SELECT 1;
SELECT * FROM test.extension;
DROP EXTENSION timescaledb;
SELECT 1;

--mismatched version errors
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
--mock-4 has mismatched versions, so the .so load should be fatal
SELECT format($$\! utils/test_fatal_command.sh %1$s "CREATE EXTENSION timescaledb VERSION 'mock-4'"$$, :'TEST_DBNAME_2') as command_to_run \gset
:command_to_run
--mock-4 not installed.
SELECT * FROM test.extension;

\c :TEST_DBNAME_2 :ROLE_SUPERUSER
CREATE EXTENSION timescaledb VERSION 'mock-broken';

SELECT * FROM test.extension;
SELECT 1;
SELECT 1;
DROP EXTENSION timescaledb;
SELECT * FROM test.extension;

--cannot update extension; already loaded version
\c :TEST_DBNAME_2 :ROLE_SUPERUSER
CREATE EXTENSION timescaledb VERSION 'mock-broken';
SELECT 1;
\set ON_ERROR_STOP 0
ALTER EXTENSION timescaledb UPDATE TO 'mock-5';
\set ON_ERROR_STOP 1

\c :TEST_DBNAME_2 :ROLE_SUPERUSER
--can update extension now.
ALTER EXTENSION timescaledb UPDATE TO 'mock-5';
SELECT 1;
SELECT mock_function();

\c :TEST_DBNAME_2 :ROLE_SUPERUSER
ALTER EXTENSION timescaledb UPDATE TO 'mock-6';
--The mock-5->mock_6 upgrade is intentionally broken.
--The mock_function was never changed to point to mock-6 in the update script.
--Thus mock_function is defined incorrectly to point to the mock-5.so
--This will now be a FATAL error.
SELECT format($$\! utils/test_fatal_command.sh %1$s "SELECT mock_function()"$$, :'TEST_DBNAME_2') as command_to_run \gset
:command_to_run
SELECT * FROM test.extension;

--TEST: create extension when old .so already loaded
\c :TEST_DBNAME :ROLE_SUPERUSER
SELECT * FROM test.extension;
DROP EXTENSION timescaledb;
SELECT * FROM test.extension;

\set ON_ERROR_STOP 0
CREATE EXTENSION timescaledb VERSION 'mock-2';
\set ON_ERROR_STOP 1
SELECT * FROM test.extension;
--can create in a new session.
\c :TEST_DBNAME :ROLE_SUPERUSER
CREATE EXTENSION timescaledb VERSION 'mock-2';
SELECT * FROM test.extension;

--make sure parallel workers started after a 'DISCARD ALL' work
CREATE TABLE test (i int, j double precision);
INSERT INTO test SELECT x, x+0.1 FROM generate_series(1,100) AS x;

DISCARD ALL;
SELECT set_config(CASE WHEN current_setting('server_version_num')::int < 160000 THEN 'force_parallel_mode' ELSE 'debug_parallel_query' END,'on', false);
SET max_parallel_workers_per_gather = 1;
SELECT count(*) FROM test;

CREATE EXTENSION timescaledb_osm VERSION 'mock-1';

-- Test that OSM process utility hook works:  it should see this DROP TABLE.
DROP TABLE test;

-- clean up additional database
\c :TEST_DBNAME :ROLE_SUPERUSER
DROP DATABASE :"TEST_DBNAME_2" WITH (FORCE);
