@@ -151,60 +151,13 @@ upload_schema() {
151151 return 0
152152 fi
153153
154+ # Note: If DELETE_EXISTING is true, resources should have been deleted in bulk before upload
155+ # So if we still get "already exists", something went wrong with the bulk delete
154156 if [ " $DELETE_EXISTING " = true ]; then
155- echo -e " ${YELLOW} ⚠${NC} Already exists, deleting and recreating (--delete-existing flag set)..."
156-
157- # URL-encode the schema $id for use in the DELETE endpoint
158- local encoded_id=$( printf ' %s' " $schema_id " | jq -sRr @uri)
159-
160- # Delete the existing resource
161- delete_response=$( curl -s -w " \n%{http_code}" -X DELETE \
162- " ${PLATFORM_URL} /data/foundation/schemaregistry/tenant/${endpoint} /${encoded_id} " \
163- -H " Authorization: Bearer ${ACCESS_TOKEN} " \
164- -H " x-api-key: ${CLIENT_ID} " \
165- -H " x-gw-ims-org-id: ${ORG_ID} " \
166- -H " x-sandbox-name: ${SANDBOX_NAME} " )
167-
168- delete_http_code=$( echo " $delete_response " | tail -n1)
169- delete_response_body=$( echo " $delete_response " | sed ' $d' )
170-
171- if [[ " $delete_http_code " =~ ^2[0-9][0-9]$ ]]; then
172- echo -e " ${GREEN} ✓${NC} Deleted (HTTP ${delete_http_code} )"
173- elif [ " $delete_http_code " = " 404" ]; then
174- echo -e " ${YELLOW} ⚠${NC} Not found for deletion (HTTP 404), will try to create anyway"
175- else
176- echo -e " ${RED} ✗${NC} Delete failed (HTTP ${delete_http_code} )"
177- echo -e " ${RED} Response:${NC} "
178- echo " $delete_response_body " | jq ' .' 2> /dev/null || echo " $delete_response_body "
179- return 1
180- fi
181-
182- # Now try to create it (again) with POST
183- response=$( curl -s -w " \n%{http_code}" -X POST \
184- " ${PLATFORM_URL} /data/foundation/schemaregistry/tenant/${endpoint} " \
185- -H ' Content-Type: application/json' \
186- -H " Authorization: Bearer ${ACCESS_TOKEN} " \
187- -H " x-api-key: ${CLIENT_ID} " \
188- -H " x-gw-ims-org-id: ${ORG_ID} " \
189- -H " x-sandbox-name: ${SANDBOX_NAME} " \
190- -d @" ${file_path} " )
191-
192- http_code=$( echo " $response " | tail -n1)
193- response_body=$( echo " $response " | sed ' $d' )
194-
195- if [[ " $http_code " =~ ^2[0-9][0-9]$ ]]; then
196- echo -e " ${GREEN} ✓${NC} Recreated (HTTP ${http_code} )"
197- schema_id=$( echo " $response_body " | grep -o ' "$id":"[^"]*"' | cut -d' "' -f4)
198- if [ -n " $schema_id " ]; then
199- echo -e " ${BLUE} ID:${NC} ${schema_id} "
200- fi
201- return 0
202- else
203- echo -e " ${RED} ✗${NC} Recreate failed (HTTP ${http_code} )"
204- echo -e " ${RED} Response:${NC} "
205- echo " $response_body " | jq ' .' 2> /dev/null || echo " $response_body "
206- return 1
207- fi
157+ echo -e " ${RED} ✗${NC} Already exists even after bulk delete attempt"
158+ echo -e " ${RED} Response:${NC} "
159+ echo " $response_body " | jq ' .' 2> /dev/null || echo " $response_body "
160+ return 1
208161 fi
209162
210163 echo -e " ${YELLOW} ⚠${NC} Already exists, attempting to update with PUT..."
@@ -251,6 +204,126 @@ upload_schema() {
251204 fi
252205}
253206
207+ # Function to delete a schema resource
208+ delete_schema () {
209+ local file_path=" $1 "
210+ local endpoint=" $2 "
211+ local file_name=$( basename " $file_path " )
212+
213+ echo -e " Deleting: ${YELLOW}${file_name}${NC} "
214+
215+ # Extract the $id from the schema file
216+ local schema_id=$( jq -r ' .["$id"]' " $file_path " )
217+
218+ # URL-encode the schema $id for use in the DELETE endpoint
219+ local encoded_id=$( printf ' %s' " $schema_id " | jq -sRr @uri)
220+
221+ # Delete the resource
222+ response=$( curl -s -w " \n%{http_code}" -X DELETE \
223+ " ${PLATFORM_URL} /data/foundation/schemaregistry/tenant/${endpoint} /${encoded_id} " \
224+ -H " Authorization: Bearer ${ACCESS_TOKEN} " \
225+ -H " x-api-key: ${CLIENT_ID} " \
226+ -H " x-gw-ims-org-id: ${ORG_ID} " \
227+ -H " x-sandbox-name: ${SANDBOX_NAME} " )
228+
229+ http_code=$( echo " $response " | tail -n1)
230+ response_body=$( echo " $response " | sed ' $d' )
231+
232+ if [[ " $http_code " =~ ^2[0-9][0-9]$ ]]; then
233+ echo -e " ${GREEN} ✓${NC} Deleted (HTTP ${http_code} )"
234+ return 0
235+ elif [ " $http_code " = " 404" ]; then
236+ echo -e " ${YELLOW} ⚠${NC} Not found (HTTP 404), already deleted or never existed"
237+ return 0
238+ else
239+ echo -e " ${RED} ✗${NC} Delete failed (HTTP ${http_code} )"
240+ echo -e " ${RED} Response:${NC} "
241+ echo " $response_body " | jq ' .' 2> /dev/null || echo " $response_body "
242+ return 1
243+ fi
244+ }
245+
246+ # If --delete-existing flag is set, delete all resources in reverse order first
247+ if [ " $DELETE_EXISTING " = true ]; then
248+ echo -e " ${BLUE} Deleting existing resources in reverse order...${NC} "
249+ echo " "
250+
251+ # Delete schemas first (they depend on fieldgroups)
252+ if [ -d " schemas/paid-media" ]; then
253+ echo -e " ${GREEN} Deleting schemas...${NC} "
254+ file_count=0
255+ for file in schemas/paid-media/* .schema.json; do
256+ if [ -f " $file " ]; then
257+ if ! delete_schema " $file " " schemas" ; then
258+ echo -e " ${RED} Delete failed. Exiting.${NC} "
259+ exit 1
260+ fi
261+ (( file_count++ ))
262+ echo " "
263+ fi
264+ done
265+ echo -e " Deleted ${GREEN}${file_count}${NC} schema files"
266+ echo " "
267+ fi
268+
269+ # Delete fieldgroups (they depend on datatypes)
270+ if [ -d " components/fieldgroups/paid-media" ]; then
271+ echo -e " ${GREEN} Deleting fieldgroups...${NC} "
272+ file_count=0
273+ for file in components/fieldgroups/paid-media/* .schema.json; do
274+ if [ -f " $file " ]; then
275+ if ! delete_schema " $file " " fieldgroups" ; then
276+ echo -e " ${RED} Delete failed. Exiting.${NC} "
277+ exit 1
278+ fi
279+ (( file_count++ ))
280+ echo " "
281+ fi
282+ done
283+ echo -e " Deleted ${GREEN}${file_count}${NC} fieldgroup files"
284+ echo " "
285+ fi
286+
287+ # Delete datatypes (they depend on nothing, but classes might depend on them)
288+ if [ -d " components/datatypes/paid-media" ]; then
289+ echo -e " ${GREEN} Deleting datatypes...${NC} "
290+ file_count=0
291+ for file in components/datatypes/paid-media/* .schema.json; do
292+ if [ -f " $file " ]; then
293+ if ! delete_schema " $file " " datatypes" ; then
294+ echo -e " ${RED} Delete failed. Exiting.${NC} "
295+ exit 1
296+ fi
297+ (( file_count++ ))
298+ echo " "
299+ fi
300+ done
301+ echo -e " Deleted ${GREEN}${file_count}${NC} datatype files"
302+ echo " "
303+ fi
304+
305+ # Delete classes last
306+ if [ -d " components/classes" ]; then
307+ echo -e " ${GREEN} Deleting classes...${NC} "
308+ class_count=0
309+ for file in components/classes/* .schema.json; do
310+ if [ -f " $file " ]; then
311+ if ! delete_schema " $file " " classes" ; then
312+ echo -e " ${RED} Delete failed. Exiting.${NC} "
313+ exit 1
314+ fi
315+ (( class_count++ ))
316+ echo " "
317+ fi
318+ done
319+ echo -e " Deleted ${GREEN}${class_count}${NC} class files"
320+ echo " "
321+ fi
322+
323+ echo -e " ${GREEN} ✓ All resources deleted successfully!${NC} "
324+ echo " "
325+ fi
326+
254327# Upload classes first (needed by fieldgroups and schemas)
255328if [ -d " components/classes" ]; then
256329 echo -e " ${GREEN} Uploading classes...${NC} "
0 commit comments