@@ -100,6 +100,88 @@ jobs:
100100 (psql -p 10563 -U dspace -d postgres -c \"CREATE ROLE dspace5 IN ROLE dspace;\" || true) &&
101101 psql -p 10563 -U dspace -d dspace -f /tmp/$FNAME &&
102102 psql -p 10563 -U dspace -d dspace -c 'CREATE EXTENSION IF NOT EXISTS pgcrypto;' &&
103+ psql -p 10563 -U dspace -d dspace -c \"
104+ -- Comprehensive cleanup of duplicate entries before migration
105+
106+ -- First, let's check what tables actually exist and their names
107+ \\\\echo 'Checking existing tables...';
108+
109+ -- Clean up epersongroup2eperson duplicates (case variations)
110+ DO \\\$cleanup\\\$
111+ BEGIN
112+ -- Try different case variations of the table name
113+ IF EXISTS (SELECT 1 FROM information_schema.tables WHERE LOWER(table_name) = 'epersongroup2eperson') THEN
114+ \\\\echo 'Cleaning epersongroup2eperson duplicates...';
115+
116+ -- First show how many duplicates we have
117+ PERFORM (SELECT COUNT(*) FROM (
118+ SELECT eperson_group_id, eperson_id, COUNT(*)
119+ FROM epersongroup2eperson
120+ GROUP BY eperson_group_id, eperson_id
121+ HAVING COUNT(*) > 1
122+ ) dups);
123+
124+ -- Delete duplicates, keeping only the first occurrence
125+ WITH ranked AS (
126+ SELECT ctid, row_number() OVER (PARTITION BY eperson_group_id, eperson_id ORDER BY ctid) AS rn
127+ FROM epersongroup2eperson
128+ )
129+ DELETE FROM epersongroup2eperson
130+ WHERE ctid IN (
131+ SELECT ctid FROM ranked WHERE rn > 1
132+ );
133+
134+ -- Verify cleanup worked
135+ IF EXISTS (
136+ SELECT 1 FROM (
137+ SELECT eperson_group_id, eperson_id, COUNT(*)
138+ FROM epersongroup2eperson
139+ GROUP BY eperson_group_id, eperson_id
140+ HAVING COUNT(*) > 1
141+ ) remaining_dups
142+ ) THEN
143+ RAISE EXCEPTION 'Failed to clean all duplicates from epersongroup2eperson';
144+ END IF;
145+
146+ \\\\echo 'epersongroup2eperson cleanup completed successfully';
147+ END IF;
148+
149+ -- Check for other possible table name variations
150+ IF EXISTS (SELECT 1 FROM information_schema.tables WHERE LOWER(table_name) = 'epersongroup2workspace') THEN
151+ WITH ranked AS (
152+ SELECT ctid, row_number() OVER (PARTITION BY eperson_group_id, workspace_item_id ORDER BY ctid) AS rn
153+ FROM epersongroup2workspace
154+ )
155+ DELETE FROM epersongroup2workspace WHERE ctid IN (SELECT ctid FROM ranked WHERE rn > 1);
156+ END IF;
157+
158+ IF EXISTS (SELECT 1 FROM information_schema.tables WHERE LOWER(table_name) = 'collection2item') THEN
159+ WITH ranked AS (
160+ SELECT ctid, row_number() OVER (PARTITION BY collection_id, item_id ORDER BY ctid) AS rn
161+ FROM collection2item
162+ )
163+ DELETE FROM collection2item WHERE ctid IN (SELECT ctid FROM ranked WHERE rn > 1);
164+ END IF;
165+
166+ IF EXISTS (SELECT 1 FROM information_schema.tables WHERE LOWER(table_name) = 'community2collection') THEN
167+ WITH ranked AS (
168+ SELECT ctid, row_number() OVER (PARTITION BY community_id, collection_id ORDER BY ctid) AS rn
169+ FROM community2collection
170+ )
171+ DELETE FROM community2collection WHERE ctid IN (SELECT ctid FROM ranked WHERE rn > 1);
172+ END IF;
173+
174+ IF EXISTS (SELECT 1 FROM information_schema.tables WHERE LOWER(table_name) = 'community2community') THEN
175+ WITH ranked AS (
176+ SELECT ctid, row_number() OVER (PARTITION BY parent_comm_id, child_comm_id ORDER BY ctid) AS rn
177+ FROM community2community
178+ )
179+ DELETE FROM community2community WHERE ctid IN (SELECT ctid FROM ranked WHERE rn > 1);
180+ END IF;
181+ END \\\$cleanup\\\$;
182+
183+ \\\\echo 'All duplicate cleanup completed';
184+ \" &&
103185 rm /tmp/$FNAME
104186 "
105187 echo "====="
0 commit comments