Commit 818891646af5fcbfdd0a765554ff80d778f552b5
1 parent
61e1ce44
Add constraint 'device_credentials_device_id_unq_key' to upgrade script.
Showing
1 changed file
with
18 additions
and
0 deletions
... | ... | @@ -455,7 +455,25 @@ BEGIN |
455 | 455 | |
456 | 456 | data_type := get_column_type(table_name, column_device_id); |
457 | 457 | IF data_type = 'character varying' THEN |
458 | + ALTER TABLE device_credentials DROP CONSTRAINT IF EXISTS device_credentials_device_id_unq_key; | |
458 | 459 | PERFORM column_type_to_uuid(table_name, column_device_id); |
460 | + -- remove duplicate credentials with same device_id | |
461 | + DELETE from device_credentials where id in ( | |
462 | + select dc.id | |
463 | + from ( | |
464 | + SELECT id, device_id, | |
465 | + ROW_NUMBER() OVER ( | |
466 | + PARTITION BY | |
467 | + device_id | |
468 | + ORDER BY | |
469 | + created_time DESC | |
470 | + ) row_num | |
471 | + FROM | |
472 | + device_credentials | |
473 | + ) as dc | |
474 | + WHERE dc.row_num > 1 | |
475 | + ); | |
476 | + ALTER TABLE device_credentials ADD CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id); | |
459 | 477 | RAISE NOTICE 'Table % column % updated!', table_name, column_device_id; |
460 | 478 | ELSE |
461 | 479 | RAISE NOTICE 'Table % column % already updated!', table_name, column_device_id; | ... | ... |