timescaledb/test/sql/drop_schema.sql
Erik Nordström b9a6f890a1 Handle DROP SCHEMA for hypertable and chunk schemas
Dropping a schema that a hypertable depends on should clean up
dependent metadata. There are two schemas that matter for hypertables:
the hypertable's schema and the associated schema where chunks are
stored.

This change deals with the above as follows:

- If the hypertable schema is dropped, the hypertable and all chunks
should be deleted as well, including metadata.
- If an associated schema is dropped, the hypertables that use that
associated schema will have their associated schemas reset to the
internal schema.
- Even if no hypertable currently uses the dropped schema as their
associated schema, there might be chunks that reside in the dropped
schema (e.g., if the associated schema was changed for their
hypertables), so those chunks should have the metadata deleted.
2018-02-05 23:07:55 +01:00

54 lines
2.2 KiB
SQL

\c single :ROLE_SUPERUSER
CREATE SCHEMA chunk_schema1;
CREATE SCHEMA chunk_schema2;
CREATE SCHEMA hypertable_schema;
CREATE SCHEMA extra_schema;
GRANT ALL ON SCHEMA hypertable_schema TO :ROLE_DEFAULT_PERM_USER;
GRANT ALL ON SCHEMA chunk_schema1 TO :ROLE_DEFAULT_PERM_USER;
GRANT ALL ON SCHEMA chunk_schema2 TO :ROLE_DEFAULT_PERM_USER;
SET ROLE :ROLE_DEFAULT_PERM_USER;
CREATE TABLE hypertable_schema.test1 (time timestamptz, temp float, location int);
CREATE TABLE hypertable_schema.test2 (time timestamptz, temp float, location int);
--create two identical tables with their own chunk schemas
SELECT create_hypertable('hypertable_schema.test1', 'time', 'location', 2, associated_schema_name => 'chunk_schema1');
SELECT create_hypertable('hypertable_schema.test2', 'time', 'location', 2, associated_schema_name => 'chunk_schema2');
INSERT INTO hypertable_schema.test1 VALUES ('2001-01-01 01:01:01', 23.3, 1);
INSERT INTO hypertable_schema.test2 VALUES ('2001-01-01 01:01:01', 23.3, 1);
SELECT * FROM _timescaledb_catalog.hypertable ORDER BY id;
SELECT * FROM _timescaledb_catalog.chunk;
RESET ROLE;
--drop the associated schema. We drop the extra schema to show we can
--handle multi-schema drops
DROP SCHEMA chunk_schema1, extra_schema CASCADE;
SET ROLE :ROLE_DEFAULT_PERM_USER;
--show that the metadata for the table using the dropped schema is
--changed. The other table is not affected.
SELECT * FROM _timescaledb_catalog.hypertable ORDER BY id;
SELECT * FROM _timescaledb_catalog.chunk;
--new chunk should be created in the internal associated schema
INSERT INTO hypertable_schema.test1 VALUES ('2001-01-01 01:01:01', 23.3, 1);
SELECT * FROM _timescaledb_catalog.chunk;
RESET ROLE;
--dropping the internal schema should not work
\set ON_ERROR_STOP 0
DROP SCHEMA _timescaledb_internal CASCADE;
\set ON_ERROR_STOP 1
--dropping the hypertable schema should delete everything
DROP SCHEMA hypertable_schema CASCADE;
SET ROLE :ROLE_DEFAULT_PERM_USER;
--everything should be cleaned up
SELECT * FROM _timescaledb_catalog.hypertable GROUP BY id;
SELECT * FROM _timescaledb_catalog.chunk;
SELECT * FROM _timescaledb_catalog.dimension;
SELECT * FROM _timescaledb_catalog.dimension_slice;
SELECT * FROM _timescaledb_catalog.chunk_index;
SELECT * FROM _timescaledb_catalog.chunk_constraint;