1+ \set testid ' 12'
2+
3+ \connect postgres
4+ \ir helper_psql_conn_setup .sql
5+
6+ DROP DATABASE IF EXISTS cloudsync_test_repeated;
7+ CREATE DATABASE cloudsync_test_repeated ;
8+
9+ \connect cloudsync_test_repeated
10+ \ir helper_psql_conn_setup .sql
11+
12+ -- Reset extension and install
13+ DROP EXTENSION IF EXISTS cloudsync CASCADE;
14+ CREATE EXTENSION IF NOT EXISTS cloudsync;
15+
16+ -- 'Test multi-schema table init (setup)'
17+ CREATE SCHEMA IF NOT EXISTS test_schema;
18+ DROP TABLE IF EXISTS public .repeated_table ;
19+ DROP TABLE IF EXISTS test_schema .repeated_table ;
20+ CREATE TABLE public .repeated_table (id TEXT PRIMARY KEY , data TEXT );
21+ CREATE TABLE test_schema .repeated_table (id TEXT PRIMARY KEY , data TEXT );
22+
23+ -- Reset the connection to test if we load the configuration correctly
24+ \connect cloudsync_test_repeated
25+ \ir helper_psql_conn_setup .sql
26+
27+ -- 'Test init on table that exists in multiple schemas (default: public)'
28+ SELECT cloudsync_cleanup(' repeated_table' ) AS _cleanup_repeated \gset
29+ SELECT cloudsync_init(' repeated_table' , ' CLS' , true) AS _init_repeated_public \gset
30+ SELECT (to_regclass(' public.repeated_table_cloudsync' ) IS NOT NULL ) AS init_repeated_public_ok \gset
31+ \if :init_repeated_public_ok
32+ \echo [PASS] (:testid) Test init on repeated_table in public schema
33+ \else
34+ \echo [FAIL] (:testid) Test init on repeated_table in public schema
35+ SELECT (:fail::int + 1 ) AS fail \gset
36+ \endif
37+
38+ -- 'Test insert on repeated_table in public schema'
39+ SELECT cloudsync_uuid() AS repeated_id1 \gset
40+ INSERT INTO public .repeated_table (id, data) VALUES (:' repeated_id1' , ' public_data' );
41+ SELECT (COUNT (* ) = 1 ) AS insert_repeated_public_ok
42+ FROM public .repeated_table_cloudsync
43+ WHERE pk = cloudsync_pk_encode(VARIADIC ARRAY[:' repeated_id1' ]::text [])
44+ AND col_name = ' data' \gset
45+ \if :insert_repeated_public_ok
46+ \echo [PASS] (:testid) Test insert metadata on repeated_table in public
47+ \else
48+ \echo [FAIL] (:testid) Test insert metadata on repeated_table in public
49+ SELECT (:fail::int + 1 ) AS fail \gset
50+ \endif
51+
52+ -- 'Test cloudsync_changes view read for public.repeated_table'
53+ SELECT COUNT (* ) AS changes_view_repeated_count
54+ FROM cloudsync_changes
55+ WHERE tbl = ' repeated_table' \gset
56+ SELECT COUNT (* ) AS changes_meta_repeated_count
57+ FROM public .repeated_table_cloudsync \gset
58+ SELECT (:changes_view_repeated_count::int = :changes_meta_repeated_count::int ) AS changes_read_repeated_ok \gset
59+ \if :changes_read_repeated_ok
60+ \echo [PASS] (:testid) Test cloudsync_changes view read for public .repeated_table
61+ \else
62+ \echo [FAIL] (:testid) Test cloudsync_changes view read for public .repeated_table
63+ SELECT (:fail::int + 1 ) AS fail \gset
64+ \endif
65+
66+ -- 'Test cloudsync_changes view write for public.repeated_table'
67+ SELECT cloudsync_uuid() AS repeated_id2 \gset
68+ INSERT INTO cloudsync_changes (tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq)
69+ VALUES (
70+ ' repeated_table' ,
71+ cloudsync_pk_encode(VARIADIC ARRAY[:' repeated_id2' ]::text []),
72+ ' data' ,
73+ -- "public_write" encoded as cloudsync text value (type 0x0b + len 0x0c)
74+ decode(' 0b0c7075626c69635f7772697465' , ' hex' ),
75+ 1 ,
76+ cloudsync_db_version_next(),
77+ cloudsync_siteid(),
78+ 1 ,
79+ 0
80+ );
81+ SELECT (COUNT (* ) = 1 ) AS changes_write_repeated_ok
82+ FROM public .repeated_table
83+ WHERE id = :' repeated_id2' AND data = ' public_write' \gset
84+ \if :changes_write_repeated_ok
85+ \echo [PASS] (:testid) Test cloudsync_changes view write for public .repeated_table
86+ \else
87+ \echo [FAIL] (:testid) Test cloudsync_changes view write for public .repeated_table
88+ SELECT (:fail::int + 1 ) AS fail \gset
89+ \endif
90+
91+ -- 'Test cleanup on table with ambiguous name'
92+ SELECT cloudsync_cleanup(' repeated_table' ) AS _cleanup_repeated2 \gset
93+ SELECT (to_regclass(' public.repeated_table_cloudsync' ) IS NULL ) AS cleanup_repeated_ok \gset
94+ \if :cleanup_repeated_ok
95+ \echo [PASS] (:testid) Test cleanup on repeated_table
96+ \else
97+ \echo [FAIL] (:testid) Test cleanup on repeated_table
98+ SELECT (:fail::int + 1 ) AS fail \gset
99+ \endif
100+
101+ -- 'Test cloudsync_set_schema and init on test_schema'
102+ SELECT cloudsync_set_schema(' test_schema' ) AS _set_schema \gset
103+ SELECT cloudsync_init(' repeated_table' , ' CLS' , true) AS _init_repeated_test_schema \gset
104+ SELECT (to_regclass(' test_schema.repeated_table_cloudsync' ) IS NOT NULL ) AS init_repeated_test_schema_ok \gset
105+ \if :init_repeated_test_schema_ok
106+ \echo [PASS] (:testid) Test init on repeated_table in test_schema
107+ \else
108+ \echo [FAIL] (:testid) Test init on repeated_table in test_schema
109+ SELECT (:fail::int + 1 ) AS fail \gset
110+ \endif
111+
112+ -- 'Test that public.repeated_table_cloudsync was not recreated'
113+ SELECT (to_regclass(' public.repeated_table_cloudsync' ) IS NULL ) AS public_still_clean_ok \gset
114+ \if :public_still_clean_ok
115+ \echo [PASS] (:testid) Test public .repeated_table_cloudsync still cleaned up
116+ \else
117+ \echo [FAIL] (:testid) Test public .repeated_table_cloudsync should not exist
118+ SELECT (:fail::int + 1 ) AS fail \gset
119+ \endif
120+
121+ -- reset the current schema to check if the next connection load the correct configuration
122+ -- SELECT cloudsync_set_schema('public') AS _reset_schema \gset
123+
124+ -- Reset the connection to test if if loads the correct configuration for the table on the correct schema
125+ \connect cloudsync_test_repeated
126+ \ir helper_psql_conn_setup .sql
127+
128+ -- 'Test insert on repeated_table in test_schema'
129+ SELECT cloudsync_uuid() AS repeated_id3 \gset
130+ INSERT INTO test_schema .repeated_table (id, data) VALUES (:' repeated_id3' , ' test_schema_data' );
131+ SELECT (COUNT (* ) = 1 ) AS insert_repeated_test_schema_ok
132+ FROM test_schema .repeated_table_cloudsync
133+ WHERE pk = cloudsync_pk_encode(VARIADIC ARRAY[:' repeated_id3' ]::text [])
134+ AND col_name = ' data' \gset
135+ \if :insert_repeated_test_schema_ok
136+ \echo [PASS] (:testid) Test insert metadata on repeated_table in test_schema
137+ \else
138+ \echo [FAIL] (:testid) Test insert metadata on repeated_table in test_schema
139+ SELECT (:fail::int + 1 ) AS fail \gset
140+ \endif
141+
142+ -- 'Test cloudsync_changes view read for test_schema.repeated_table'
143+ SELECT COUNT (* ) AS changes_view_test_schema_count
144+ FROM cloudsync_changes
145+ WHERE tbl = ' repeated_table' \gset
146+ SELECT COUNT (* ) AS changes_meta_test_schema_count
147+ FROM test_schema .repeated_table_cloudsync \gset
148+ SELECT (:changes_view_test_schema_count::int = :changes_meta_test_schema_count::int ) AS changes_read_test_schema_ok \gset
149+ \if :changes_read_test_schema_ok
150+ \echo [PASS] (:testid) Test cloudsync_changes view read for test_schema .repeated_table
151+ \else
152+ \echo [FAIL] (:testid) Test cloudsync_changes view read for test_schema .repeated_table
153+ SELECT (:fail::int + 1 ) AS fail \gset
154+ \endif
155+
156+ -- 'Test cloudsync_changes view write for test_schema.repeated_table'
157+ SELECT cloudsync_uuid() AS repeated_id4 \gset
158+ INSERT INTO cloudsync_changes (tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq)
159+ VALUES (
160+ ' repeated_table' ,
161+ cloudsync_pk_encode(VARIADIC ARRAY[:' repeated_id4' ]::text []),
162+ ' data' ,
163+ -- "testschema_write" encoded as cloudsync text value (type 0x0b + len 0x10)
164+ decode(' 0b1074657374736368656d615f7772697465' , ' hex' ),
165+ 1 ,
166+ cloudsync_db_version_next(),
167+ cloudsync_siteid(),
168+ 1 ,
169+ 0
170+ );
171+ SELECT (COUNT (* ) = 1 ) AS changes_write_test_schema_ok
172+ FROM test_schema .repeated_table
173+ WHERE id = :' repeated_id4' AND data = ' testschema_write' \gset
174+ \if :changes_write_test_schema_ok
175+ \echo [PASS] (:testid) Test cloudsync_changes view write for test_schema .repeated_table
176+ \else
177+ \echo [FAIL] (:testid) Test cloudsync_changes view write for test_schema .repeated_table
178+ SELECT (:fail::int + 1 ) AS fail \gset
179+ \endif
180+
181+ -- 'Test cleanup on repeated_table on test_schema'
182+ SELECT cloudsync_cleanup(' repeated_table' ) AS _cleanup_repeated3 \gset
183+ SELECT (to_regclass(' test_schema.repeated_table_cloudsync' ) IS NULL ) AS cleanup_repeated3_ok \gset
184+ \if :cleanup_repeated3_ok
185+ \echo [PASS] (:testid) Test cleanup on repeated_table on test_schema
186+ \else
187+ \echo [FAIL] (:testid) Test cleanup on repeated_table on test_schema
188+ SELECT (:fail::int + 1 ) AS fail \gset
189+ \endif
190+
191+ -- 'Reset schema to public for subsequent tests'
192+ SELECT cloudsync_set_schema(' public' ) AS _reset_schema \gset
193+ SELECT current_schema() AS current_schema_after_reset \gset
194+ SELECT (:' current_schema_after_reset' = ' public' ) AS schema_reset_ok \gset
195+ \if :schema_reset_ok
196+ \echo [PASS] (:testid) Test schema reset to public
197+ \else
198+ \echo [FAIL] (:testid) Test schema reset to public
199+ SELECT (:fail::int + 1 ) AS fail \gset
200+ \endif
201+
202+ \if :{?DEBUG_MERGE}
203+ \connect postgres
204+ DROP DATABASE IF EXISTS cloudsync_test_repeated;
205+ \endif
0 commit comments