-- 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.
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 
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+------------------------+-------------------+-------------------+--------------------------+--------

SELECT * from _timescaledb_catalog.dimension;
 id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func 
----+---------------+-------------+-------------+---------+------------+--------------------------+-------------------+-----------------+--------------------------+-------------------------+------------------

CREATE TABLE should_drop (time timestamp, temp float8);
SELECT create_hypertable('should_drop', 'time');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
    create_hypertable     
--------------------------
 (1,public,should_drop,t)

CREATE TABLE hyper_with_dependencies (time timestamp, temp float8);
SELECT create_hypertable('hyper_with_dependencies', 'time');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
          create_hypertable           
--------------------------------------
 (2,public,hyper_with_dependencies,t)

CREATE VIEW dependent_view AS SELECT * FROM hyper_with_dependencies;
INSERT INTO hyper_with_dependencies VALUES (now(), 1.0);
\set ON_ERROR_STOP 0
DROP TABLE hyper_with_dependencies;
ERROR:  cannot drop table hyper_with_dependencies because other objects depend on it
\set ON_ERROR_STOP 1
DROP TABLE hyper_with_dependencies CASCADE;
NOTICE:  drop cascades to view dependent_view
-- check that the view is dropped
SELECT oid FROM pg_class WHERE relname = 'dependent_view';
 oid 
-----

CREATE TABLE chunk_with_dependencies (time timestamp, temp float8);
SELECT create_hypertable('chunk_with_dependencies', 'time');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
          create_hypertable           
--------------------------------------
 (3,public,chunk_with_dependencies,t)

INSERT INTO chunk_with_dependencies VALUES (now(), 1.0);
CREATE VIEW dependent_view_chunk AS SELECT * FROM _timescaledb_internal._hyper_3_2_chunk;
\set ON_ERROR_STOP 0
DROP TABLE chunk_with_dependencies;
ERROR:  cannot drop table _timescaledb_internal._hyper_3_2_chunk because other objects depend on it
\set ON_ERROR_STOP 1
DROP TABLE chunk_with_dependencies CASCADE;
NOTICE:  drop cascades to view dependent_view_chunk
-- check that the view is dropped
SELECT oid FROM pg_class WHERE relname = 'dependent_view_chunk';
 oid 
-----

-- Calling create hypertable again will increment hypertable ID
-- although no new hypertable is created. Make sure we can handle this.
SELECT create_hypertable('should_drop', 'time', if_not_exists => true);
NOTICE:  table "should_drop" is already a hypertable, skipping
    create_hypertable     
--------------------------
 (1,public,should_drop,f)

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      | should_drop | _timescaledb_internal  | _hyper_1                |              1 | _timescaledb_functions   | calculate_chunk_interval |                 0 |                 0 |                          |      0

SELECT * from _timescaledb_catalog.dimension;
 id | hypertable_id | column_name |         column_type         | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func 
----+---------------+-------------+-----------------------------+---------+------------+--------------------------+-------------------+-----------------+--------------------------+-------------------------+------------------
  1 |             1 | time        | timestamp without time zone | t       |            |                          |                   |    604800000000 |                          |                         | 

DROP TABLE should_drop;
CREATE TABLE should_drop (time timestamp, temp float8);
SELECT create_hypertable('should_drop', 'time');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
    create_hypertable     
--------------------------
 (4,public,should_drop,t)

INSERT INTO should_drop VALUES (now(), 1.0);
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 
----+-------------+-------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
  4 | public      | should_drop | _timescaledb_internal  | _hyper_4                |              1 | _timescaledb_functions   | calculate_chunk_interval |                 0 |                 0 |                          |      0

SELECT * from _timescaledb_catalog.dimension;
 id | hypertable_id | column_name |         column_type         | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func 
----+---------------+-------------+-----------------------------+---------+------------+--------------------------+-------------------+-----------------+--------------------------+-------------------------+------------------
  4 |             4 | time        | timestamp without time zone | t       |            |                          |                   |    604800000000 |                          |                         | 

-- test dropping multiple objects at once
CREATE TABLE t1 (time timestamptz) WITH (tsdb.hypertable);
NOTICE:  using column "time" as partitioning column
INSERT INTO t1 VALUES ('2025-01-01');
CREATE TABLE t2 (time timestamptz) WITH (tsdb.hypertable);
NOTICE:  using column "time" as partitioning column
INSERT INTO t2 VALUES ('2025-01-01');
CREATE TABLE t3 (time timestamptz);
DROP TABLE t1, t2, t3;
