2021-11-22 13:57:03 -05:00
|
|
|
-- Delete the empty ap_ids
|
2023-08-02 12:44:51 -04:00
|
|
|
DELETE FROM activity
|
|
|
|
WHERE ap_id IS NULL;
|
2021-11-22 13:57:03 -05:00
|
|
|
|
|
|
|
-- Make it required
|
2023-08-02 12:44:51 -04:00
|
|
|
ALTER TABLE activity
|
|
|
|
ALTER COLUMN ap_id SET NOT NULL;
|
2021-11-22 13:57:03 -05:00
|
|
|
|
|
|
|
-- Delete dupes, keeping the first one
|
2023-08-02 12:44:51 -04:00
|
|
|
DELETE FROM activity a USING (
|
|
|
|
SELECT
|
|
|
|
min(id) AS id,
|
|
|
|
ap_id
|
|
|
|
FROM
|
|
|
|
activity
|
|
|
|
GROUP BY
|
|
|
|
ap_id
|
|
|
|
HAVING
|
|
|
|
count(*) > 1) b
|
|
|
|
WHERE
|
|
|
|
a.ap_id = b.ap_id
|
|
|
|
AND a.id <> b.id;
|
2021-11-22 13:57:03 -05:00
|
|
|
|
|
|
|
-- The index
|
2023-08-02 12:44:51 -04:00
|
|
|
CREATE UNIQUE INDEX idx_activity_ap_id ON activity (ap_id);
|
2021-11-22 13:57:03 -05:00
|
|
|
|
|
|
|
-- Drop the old index
|
2023-08-02 12:44:51 -04:00
|
|
|
DROP INDEX idx_activity_unique_apid;
|
2021-11-22 13:57:03 -05:00
|
|
|
|