CREATE SCHEMA single_node_truncate;
SET search_path TO single_node_truncate;
SET citus.shard_replication_factor TO 1;
SET citus.next_shard_id TO 91630500;
-- helper view that prints out local table names and sizes in the schema
CREATE VIEW table_sizes AS
SELECT
  c.relname as name,
  pg_catalog.pg_table_size(c.oid) > 0 as has_data
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
      AND n.nspname = 'single_node_truncate'
ORDER BY 1;
-- test truncating reference tables
CREATE TABLE ref(id int UNIQUE, data int);
INSERT INTO ref SELECT x,x FROM generate_series(1,10000) x;
SELECT create_reference_table('ref');
NOTICE:  Copying data from local table...
NOTICE:  copying the data has completed
DETAIL:  The local data in the table is no longer visible, but is still on disk.
HINT:  To remove the local data, run: SELECT truncate_local_data_after_distributing_table($$single_node_truncate.ref$$)
 create_reference_table
---------------------------------------------------------------------

(1 row)

CREATE TABLE citus_local(id int, ref_id int REFERENCES ref(id));
INSERT INTO citus_local SELECT x,x FROM generate_series(1,10000) x;
-- verify that shell tables for citus local tables are empty
SELECT * FROM table_sizes;
         name         | has_data
---------------------------------------------------------------------
 citus_local          | f
 citus_local_91630501 | t
 ref                  | t
 ref_91630500         | t
(4 rows)

-- verify that this UDF is noop on Citus local tables
SELECT truncate_local_data_after_distributing_table('citus_local');
 truncate_local_data_after_distributing_table
---------------------------------------------------------------------

(1 row)

SELECT * FROM table_sizes;
         name         | has_data
---------------------------------------------------------------------
 citus_local          | f
 citus_local_91630501 | t
 ref                  | t
 ref_91630500         | t
(4 rows)

-- test that we allow cascading truncates to citus local tables
BEGIN;
SELECT truncate_local_data_after_distributing_table('ref');
NOTICE:  truncate cascades to table "citus_local"
 truncate_local_data_after_distributing_table
---------------------------------------------------------------------

(1 row)

SELECT * FROM table_sizes;
         name         | has_data
---------------------------------------------------------------------
 citus_local          | f
 citus_local_91630501 | t
 ref                  | f
 ref_91630500         | t
(4 rows)

ROLLBACK;
-- test that we allow distributing tables that have foreign keys to reference tables
CREATE TABLE dist(id int, ref_id int REFERENCES ref(id));
INSERT INTO dist SELECT x,x FROM generate_series(1,10000) x;
SELECT create_distributed_table('dist','id');
NOTICE:  Copying data from local table...
NOTICE:  copying the data has completed
DETAIL:  The local data in the table is no longer visible, but is still on disk.
HINT:  To remove the local data, run: SELECT truncate_local_data_after_distributing_table($$single_node_truncate.dist$$)
 create_distributed_table
---------------------------------------------------------------------

(1 row)

-- the following should truncate ref, dist and citus_local
BEGIN;
SELECT truncate_local_data_after_distributing_table('ref');
NOTICE:  truncate cascades to table "citus_local"
NOTICE:  truncate cascades to table "dist"
 truncate_local_data_after_distributing_table
---------------------------------------------------------------------

(1 row)

SELECT * FROM table_sizes;
         name         | has_data
---------------------------------------------------------------------
 citus_local          | f
 citus_local_91630501 | t
 dist                 | f
 dist_91630503        | t
 dist_91630504        | t
 dist_91630505        | t
 dist_91630506        | t
 ref                  | f
 ref_91630500         | t
(9 rows)

ROLLBACK;
-- the following should truncate dist table only
BEGIN;
SELECT truncate_local_data_after_distributing_table('dist');
 truncate_local_data_after_distributing_table
---------------------------------------------------------------------

(1 row)

SELECT * FROM table_sizes;
         name         | has_data
---------------------------------------------------------------------
 citus_local          | f
 citus_local_91630501 | t
 dist                 | f
 dist_91630503        | t
 dist_91630504        | t
 dist_91630505        | t
 dist_91630506        | t
 ref                  | t
 ref_91630500         | t
(9 rows)

ROLLBACK;
DROP TABLE ref, dist, citus_local;
DROP VIEW table_sizes;
DROP SCHEMA single_node_truncate CASCADE;
-- Remove the coordinator
SELECT 1 FROM master_remove_node('localhost', :master_port);
 ?column?
---------------------------------------------------------------------
        1
(1 row)

-- restart nodeid sequence so that multi_cluster_management still has the same
-- nodeids
ALTER SEQUENCE pg_dist_node_nodeid_seq RESTART 1;
