timescaledb/tsl/test/sql/include/deparse_recreate.sql
niksa 4368fcff3c Add function to reconstruct the creating command for a table
Deparse a table into a set of SQL commands that can be used to
reconstruct it. Together with column definiton it deparses
constraints, indexes, triggers and rules as well. There are some table
types that are not supported: temporary, partitioned, foreign,
inherited and a table that uses options.  Row security is also not
supported.
2020-05-27 17:31:09 +02:00

25 lines
844 B
SQL

-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
-- deparse each table%, drop it and recreate it using deparse result
DO
$$DECLARE
tables CURSOR FOR
SELECT tablename, schemaname
FROM pg_tables
WHERE tablename LIKE 'table%' AND (schemaname = 'public' OR schemaname = 'myschema')
ORDER BY tablename;
deparse_stmt text;
tablename text;
BEGIN
FOR table_record IN tables
LOOP
tablename := format('%I.%I', table_record.schemaname, table_record.tablename);
EXECUTE format('SELECT _timescaledb_internal.get_tabledef(%L)', tablename) INTO deparse_stmt;
EXECUTE 'DROP TABLE ' || tablename;
EXECUTE deparse_stmt;
END LOOP;
END$$;