|
| 1 | +-- UUID Primary Key Roundtrip Test |
| 2 | +-- Tests roundtrip with a UUID primary key (single column). |
| 3 | + |
| 4 | +\set testid '17' |
| 5 | +\ir helper_test_init.sql |
| 6 | + |
| 7 | +\connect postgres |
| 8 | +\ir helper_psql_conn_setup.sql |
| 9 | + |
| 10 | +-- Cleanup and create test databases |
| 11 | +DROP DATABASE IF EXISTS cloudsync_test_17a; |
| 12 | +DROP DATABASE IF EXISTS cloudsync_test_17b; |
| 13 | +CREATE DATABASE cloudsync_test_17a; |
| 14 | +CREATE DATABASE cloudsync_test_17b; |
| 15 | + |
| 16 | +-- ============================================================================ |
| 17 | +-- Setup Database A with UUID primary key |
| 18 | +-- ============================================================================ |
| 19 | + |
| 20 | +\connect cloudsync_test_17a |
| 21 | +\ir helper_psql_conn_setup.sql |
| 22 | +CREATE EXTENSION IF NOT EXISTS cloudsync; |
| 23 | + |
| 24 | +CREATE TABLE products ( |
| 25 | + id UUID PRIMARY KEY, |
| 26 | + name TEXT NOT NULL DEFAULT '', |
| 27 | + price DOUBLE PRECISION NOT NULL DEFAULT 0.0, |
| 28 | + stock INTEGER NOT NULL DEFAULT 0, |
| 29 | + metadata BYTEA |
| 30 | +); |
| 31 | + |
| 32 | +-- Initialize CloudSync |
| 33 | +SELECT cloudsync_init('products', 'CLS', false) AS _init_a \gset |
| 34 | + |
| 35 | +-- ============================================================================ |
| 36 | +-- Insert test data with UUIDs |
| 37 | +-- ============================================================================ |
| 38 | + |
| 39 | +INSERT INTO products VALUES ('550e8400-e29b-41d4-a716-446655440000', 'Product A', 99.99, 100, E'\\xDEADBEEF'); |
| 40 | +INSERT INTO products VALUES ('6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'Product B', 49.50, 50, NULL); |
| 41 | +INSERT INTO products VALUES ('6ba7b811-9dad-11d1-80b4-00c04fd430c8', 'Product C', 0.0, 0, E'\\x00'); |
| 42 | +INSERT INTO products VALUES ('6ba7b812-9dad-11d1-80b4-00c04fd430c8', 'Product D', 123.45, 999, E'\\xCAFEBABE'); |
| 43 | +INSERT INTO products VALUES ('6ba7b813-9dad-11d1-80b4-00c04fd430c8', '', -1.0, -1, E'\\x010203'); |
| 44 | + |
| 45 | +-- ============================================================================ |
| 46 | +-- Compute hash of Database A data |
| 47 | +-- ============================================================================ |
| 48 | + |
| 49 | +SELECT md5( |
| 50 | + COALESCE( |
| 51 | + string_agg( |
| 52 | + id::text || ':' || |
| 53 | + COALESCE(name, 'NULL') || ':' || |
| 54 | + COALESCE(price::text, 'NULL') || ':' || |
| 55 | + COALESCE(stock::text, 'NULL') || ':' || |
| 56 | + COALESCE(encode(metadata, 'hex'), 'NULL'), |
| 57 | + '|' ORDER BY id |
| 58 | + ), |
| 59 | + '' |
| 60 | + ) |
| 61 | +) AS hash_a FROM products \gset |
| 62 | + |
| 63 | +\echo [INFO] (:testid) Database A hash: :hash_a |
| 64 | + |
| 65 | +-- ============================================================================ |
| 66 | +-- Encode payload from Database A |
| 67 | +-- ============================================================================ |
| 68 | + |
| 69 | +SELECT encode( |
| 70 | + cloudsync_payload_encode(tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq), |
| 71 | + 'hex' |
| 72 | +) AS payload_a_hex |
| 73 | +FROM cloudsync_changes |
| 74 | +WHERE site_id = cloudsync_siteid() \gset |
| 75 | + |
| 76 | +-- Verify payload was created |
| 77 | +SELECT (length(:'payload_a_hex') > 0) AS payload_created \gset |
| 78 | +\if :payload_created |
| 79 | +\echo [PASS] (:testid) Payload encoded from Database A |
| 80 | +\else |
| 81 | +\echo [FAIL] (:testid) Payload encoded from Database A - Empty payload |
| 82 | +SELECT (:fail::int + 1) AS fail \gset |
| 83 | +\endif |
| 84 | + |
| 85 | +-- ============================================================================ |
| 86 | +-- Setup Database B with same schema |
| 87 | +-- ============================================================================ |
| 88 | + |
| 89 | +\connect cloudsync_test_17b |
| 90 | +\ir helper_psql_conn_setup.sql |
| 91 | +CREATE EXTENSION IF NOT EXISTS cloudsync; |
| 92 | + |
| 93 | +CREATE TABLE products ( |
| 94 | + id UUID PRIMARY KEY, |
| 95 | + name TEXT NOT NULL DEFAULT '', |
| 96 | + price DOUBLE PRECISION NOT NULL DEFAULT 0.0, |
| 97 | + stock INTEGER NOT NULL DEFAULT 0, |
| 98 | + metadata BYTEA |
| 99 | +); |
| 100 | + |
| 101 | +-- Initialize CloudSync |
| 102 | +SELECT cloudsync_init('products', 'CLS', false) AS _init_b \gset |
| 103 | + |
| 104 | +-- ============================================================================ |
| 105 | +-- Apply payload to Database B |
| 106 | +-- ============================================================================ |
| 107 | + |
| 108 | +SELECT cloudsync_payload_apply(decode(:'payload_a_hex', 'hex')) AS apply_result \gset |
| 109 | + |
| 110 | +-- Verify application succeeded |
| 111 | +SELECT (:apply_result >= 0) AS payload_applied \gset |
| 112 | +\if :payload_applied |
| 113 | +\echo [PASS] (:testid) Payload applied to Database B |
| 114 | +\else |
| 115 | +\echo [FAIL] (:testid) Payload applied to Database B - Apply returned :apply_result |
| 116 | +SELECT (:fail::int + 1) AS fail \gset |
| 117 | +\endif |
| 118 | + |
| 119 | +-- ============================================================================ |
| 120 | +-- Verify data integrity after roundtrip |
| 121 | +-- ============================================================================ |
| 122 | + |
| 123 | +SELECT md5( |
| 124 | + COALESCE( |
| 125 | + string_agg( |
| 126 | + id::text || ':' || |
| 127 | + COALESCE(name, 'NULL') || ':' || |
| 128 | + COALESCE(price::text, 'NULL') || ':' || |
| 129 | + COALESCE(stock::text, 'NULL') || ':' || |
| 130 | + COALESCE(encode(metadata, 'hex'), 'NULL'), |
| 131 | + '|' ORDER BY id |
| 132 | + ), |
| 133 | + '' |
| 134 | + ) |
| 135 | +) AS hash_b FROM products \gset |
| 136 | + |
| 137 | +\echo [INFO] (:testid) Database B hash: :hash_b |
| 138 | + |
| 139 | +SELECT (:'hash_a' = :'hash_b') AS hashes_match \gset |
| 140 | +\if :hashes_match |
| 141 | +\echo [PASS] (:testid) Data integrity verified - hashes match |
| 142 | +\else |
| 143 | +\echo [FAIL] (:testid) Data integrity check failed - Database A hash: :hash_a, Database B hash: :hash_b |
| 144 | +SELECT (:fail::int + 1) AS fail \gset |
| 145 | +\endif |
| 146 | + |
| 147 | +-- ============================================================================ |
| 148 | +-- Verify row count |
| 149 | +-- ============================================================================ |
| 150 | + |
| 151 | +SELECT COUNT(*) AS count_b FROM products \gset |
| 152 | +\connect cloudsync_test_17a |
| 153 | +SELECT COUNT(*) AS count_a_orig FROM products \gset |
| 154 | + |
| 155 | +\connect cloudsync_test_17b |
| 156 | +SELECT (:count_b = :count_a_orig) AS row_counts_match \gset |
| 157 | +\if :row_counts_match |
| 158 | +\echo [PASS] (:testid) Row counts match (:count_b rows) |
| 159 | +\else |
| 160 | +\echo [FAIL] (:testid) Row counts mismatch - Database A: :count_a_orig, Database B: :count_b |
| 161 | +SELECT (:fail::int + 1) AS fail \gset |
| 162 | +\endif |
| 163 | + |
| 164 | +-- ============================================================================ |
| 165 | +-- Verify UUID primary keys preserved |
| 166 | +-- ============================================================================ |
| 167 | + |
| 168 | +SELECT COUNT(DISTINCT id) = 5 AS uuid_count_ok FROM products \gset |
| 169 | +\if :uuid_count_ok |
| 170 | +\echo [PASS] (:testid) UUID primary keys preserved |
| 171 | +\else |
| 172 | +\echo [FAIL] (:testid) UUID primary keys not all preserved |
| 173 | +SELECT (:fail::int + 1) AS fail \gset |
| 174 | +\endif |
| 175 | + |
| 176 | +-- ============================================================================ |
| 177 | +-- Test specific UUID values |
| 178 | +-- ============================================================================ |
| 179 | + |
| 180 | +SELECT COUNT(*) = 1 AS uuid_test_ok |
| 181 | +FROM products |
| 182 | +WHERE id = '550e8400-e29b-41d4-a716-446655440000' |
| 183 | + AND name = 'Product A' |
| 184 | + AND price = 99.99 \gset |
| 185 | +\if :uuid_test_ok |
| 186 | +\echo [PASS] (:testid) Specific UUID record verified |
| 187 | +\else |
| 188 | +\echo [FAIL] (:testid) Specific UUID record not found or incorrect |
| 189 | +SELECT (:fail::int + 1) AS fail \gset |
| 190 | +\endif |
| 191 | + |
| 192 | +-- ============================================================================ |
| 193 | +-- Test bidirectional sync (B -> A) |
| 194 | +-- ============================================================================ |
| 195 | + |
| 196 | +\connect cloudsync_test_17b |
| 197 | + |
| 198 | +INSERT INTO products VALUES ('7ba7b814-9dad-11d1-80b4-00c04fd430c8', 'From B', 77.77, 777, E'\\xBEEF'); |
| 199 | + |
| 200 | +SELECT encode( |
| 201 | + cloudsync_payload_encode(tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq), |
| 202 | + 'hex' |
| 203 | +) AS payload_b_hex |
| 204 | +FROM cloudsync_changes |
| 205 | +WHERE site_id = cloudsync_siteid() \gset |
| 206 | + |
| 207 | +\connect cloudsync_test_17a |
| 208 | +SELECT cloudsync_payload_apply(decode(:'payload_b_hex', 'hex')) AS apply_b_to_a \gset |
| 209 | + |
| 210 | +SELECT COUNT(*) = 1 AS bidirectional_ok |
| 211 | +FROM products |
| 212 | +WHERE id = '7ba7b814-9dad-11d1-80b4-00c04fd430c8' |
| 213 | + AND name = 'From B' |
| 214 | + AND price = 77.77 \gset |
| 215 | +\if :bidirectional_ok |
| 216 | +\echo [PASS] (:testid) Bidirectional sync works (B to A) |
| 217 | +\else |
| 218 | +\echo [FAIL] (:testid) Bidirectional sync failed |
| 219 | +SELECT (:fail::int + 1) AS fail \gset |
| 220 | +\endif |
| 221 | + |
| 222 | +-- ============================================================================ |
| 223 | +-- Cleanup: Drop test databases if not in DEBUG mode and no failures |
| 224 | +-- ============================================================================ |
| 225 | + |
| 226 | +\ir helper_test_cleanup.sql |
| 227 | +\if :should_cleanup |
| 228 | +DROP DATABASE IF EXISTS cloudsync_test_17a; |
| 229 | +DROP DATABASE IF EXISTS cloudsync_test_17b; |
| 230 | +\endif |
0 commit comments