-- 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.
CREATE TABLE drop_test(time timestamp, temp float8, device text);
SELECT create_hypertable('drop_test', 'time', 'device', 2);
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
   create_hypertable    
------------------------
 (1,public,drop_test,t)

SELECT * FROM _timescaledb_catalog.hypertable;
 id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size | compression_state | compressed_hypertable_id | status 
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
  1 | public      | drop_test  | _timescaledb_internal  | _hyper_1                |              2 | _timescaledb_functions   | calculate_chunk_interval |                 0 |                 0 |                          |      0

INSERT INTO drop_test VALUES('Mon Mar 20 09:17:00.936242 2017', 23.4, 'dev1');
SELECT * FROM drop_test;
              time               | temp | device 
---------------------------------+------+--------
 Mon Mar 20 09:17:00.936242 2017 | 23.4 | dev1

\c :TEST_DBNAME :ROLE_SUPERUSER
DROP EXTENSION timescaledb CASCADE;
NOTICE:  drop cascades to table _timescaledb_internal._hyper_1_1_chunk
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- Querying the original table should not return any rows since all of
-- them actually existed in chunks that are now gone
SELECT * FROM drop_test;
 time | temp | device 
------+------+--------

\c :TEST_DBNAME :ROLE_SUPERUSER
-- Recreate the extension
SET client_min_messages=error;
CREATE EXTENSION timescaledb;
RESET client_min_messages;
-- Test that calling twice generates proper error
\set ON_ERROR_STOP 0
CREATE EXTENSION timescaledb;
ERROR:  extension "timescaledb" has already been loaded with another version
\set ON_ERROR_STOP 1
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- CREATE twice with IF NOT EXISTS should be OK
CREATE EXTENSION IF NOT EXISTS timescaledb;
NOTICE:  extension "timescaledb" already exists, skipping
-- Make the table a hypertable again
SELECT create_hypertable('drop_test', 'time', 'device', 2);
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
   create_hypertable    
------------------------
 (1,public,drop_test,t)

SELECT * FROM _timescaledb_catalog.hypertable;
 id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size | compression_state | compressed_hypertable_id | status 
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
  1 | public      | drop_test  | _timescaledb_internal  | _hyper_1                |              2 | _timescaledb_functions   | calculate_chunk_interval |                 0 |                 0 |                          |      0

INSERT INTO drop_test VALUES('Mon Mar 20 09:18:19.100462 2017', 22.1, 'dev1');
SELECT * FROM drop_test;
              time               | temp | device 
---------------------------------+------+--------
 Mon Mar 20 09:18:19.100462 2017 | 22.1 | dev1

--test drops thru cascades of other objects
\c :TEST_DBNAME :ROLE_SUPERUSER
-- Stop background workers to prevent them from interfering with the drop public schema
SELECT _timescaledb_functions.stop_background_workers();
 stop_background_workers 
-------------------------
 t

SET client_min_messages TO ERROR;
REVOKE CONNECT ON DATABASE :TEST_DBNAME FROM public;
SELECT count(pg_terminate_backend(pg_stat_activity.pid)) AS TERMINATED
FROM pg_stat_activity
WHERE pg_stat_activity.datname = :'TEST_DBNAME'
AND pg_stat_activity.pid <> pg_backend_pid() \gset
RESET client_min_messages;
-- drop the public schema and all its objects
DROP SCHEMA public CASCADE;
NOTICE:  drop cascades to 3 other objects
\dn
  List of schemas
 Name |   Owner    
------+------------
 test | super_user

