-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-search-index-unique-constraint.sql
More file actions
67 lines (59 loc) · 2.49 KB
/
fix-search-index-unique-constraint.sql
File metadata and controls
67 lines (59 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- Migration Script: Fix Multi-Tenant UNIQUE Constraint on Search Index Tables
-- Issue: ADR-006 - Search index UNIQUE constraints missing ad_client_id
-- Impact: ON CONFLICT clause fails, potential cross-client data corruption
-- Reference: fix/adr-critical-issues branch
-- This script fixes ALL search index tables (idx_*_ts pattern)
-- Run this script on your iDempiere database BEFORE rebuilding search indexes
-- Step 1: Generate list of all search index tables
DO $$
DECLARE
table_rec RECORD;
constraint_name TEXT;
BEGIN
-- Find all tables matching idx_*_ts pattern
FOR table_rec IN
SELECT
schemaname,
tablename
FROM pg_tables
WHERE tablename LIKE 'idx_%_ts'
AND schemaname = 'adempiere' -- Change if using different schema
LOOP
-- Find existing UNIQUE constraint on (ad_table_id, record_id)
SELECT conname INTO constraint_name
FROM pg_constraint
WHERE conrelid = (table_rec.schemaname || '.' || table_rec.tablename)::regclass
AND contype = 'u'
AND array_length(conkey, 1) = 2;
IF constraint_name IS NOT NULL THEN
RAISE NOTICE 'Fixing table: %.%', table_rec.schemaname, table_rec.tablename;
-- Drop old constraint
EXECUTE format('ALTER TABLE %I.%I DROP CONSTRAINT IF EXISTS %I',
table_rec.schemaname,
table_rec.tablename,
constraint_name);
-- Add new constraint with ad_client_id
EXECUTE format('ALTER TABLE %I.%I ADD CONSTRAINT %I UNIQUE (ad_client_id, ad_table_id, record_id)',
table_rec.schemaname,
table_rec.tablename,
table_rec.tablename || '_unique');
RAISE NOTICE 'Fixed constraint on %.%', table_rec.schemaname, table_rec.tablename;
ELSE
RAISE WARNING 'No UNIQUE constraint found on %.% - may need manual intervention',
table_rec.schemaname, table_rec.tablename;
END IF;
END LOOP;
END $$;
-- Step 2: Verify all constraints are correct
SELECT
t.schemaname,
t.tablename,
c.conname as constraint_name,
pg_get_constraintdef(c.oid) as constraint_definition
FROM pg_tables t
LEFT JOIN pg_constraint c ON c.conrelid = (t.schemaname || '.' || t.tablename)::regclass
AND c.contype = 'u'
WHERE t.tablename LIKE 'idx_%_ts'
AND t.schemaname = 'adempiere'
ORDER BY t.tablename;
-- Expected output: Each table should have UNIQUE (ad_client_id, ad_table_id, record_id)