Skip to content

Commit

Permalink
Merge branch 'dev' into update-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl authored Sep 11, 2024
2 parents 64e842d + 97864d6 commit 2c98965
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
16 changes: 9 additions & 7 deletions app/src/server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,13 @@ export const addUserModels: AddUserModels<AddUserModelsPayload, any> = async (ar
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ...args }),
});
const json: any = (await response.json()) as { detail?: string }; // Parse JSON once

const json: any = (await response.json()) as { detail?: string }; // Parse JSON once
if (!response.ok) {
const errorMsg = json.detail || `HTTP error with status code ${response.status}`;
console.error('Server Error:', errorMsg);
throw new Error(errorMsg);
throw new HttpError(
response.status,
JSON.stringify(json.detail) || `HTTP error with status code ${response.status}`
);
}

return json;
Expand Down Expand Up @@ -208,9 +209,10 @@ export const updateUserModels: UpdateUserModels<UpdateUserModelsPayload, void> =
const json: any = (await response.json()) as { detail?: string }; // Parse JSON once

if (!response.ok) {
const errorMsg = json.detail || `HTTP error with status code ${response.status}`;
console.error('Server Error:', errorMsg);
throw new Error(errorMsg);
throw new HttpError(
response.status,
JSON.stringify(json.detail) || `HTTP error with status code ${response.status}`
);
}
} catch (error: any) {
throw new HttpError(500, error.message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Update records with duplicate names for the same user
UPDATE "Model"
SET json_str = jsonb_set(
json_str::jsonb,
'{name}',
-- Append a random 5-digit suffix to the name
to_jsonb(json_str->>'name' || '_' || LPAD(FLOOR(RANDOM() * 100000)::text, 5, '0'))
)
-- Select records with duplicate names
WHERE (user_uuid, json_str->>'name') IN (
SELECT user_uuid, json_str->>'name'
FROM "Model"
GROUP BY user_uuid, json_str->>'name'
HAVING COUNT(*) > 1
)
-- Exclude the first occurrence of each duplicate set
AND uuid NOT IN (
SELECT DISTINCT ON (user_uuid, json_str->>'name') uuid
FROM "Model"
ORDER BY user_uuid, json_str->>'name', created_at
);

0 comments on commit 2c98965

Please sign in to comment.