mirror of
https://github.com/zitadel/zitadel
synced 2024-11-23 03:03:39 +00:00
fix: migration for cleanup resource owners (#1516)
* migration for cleanup resource owners * table name
This commit is contained in:
parent
800eeca7ba
commit
18aec74222
46
migrations/cockroach/V1.36__cleanup_resource_owners.sql
Normal file
46
migrations/cockroach/V1.36__cleanup_resource_owners.sql
Normal file
@ -0,0 +1,46 @@
|
||||
WITH resource_owners AS (
|
||||
WITH first_sequences AS (
|
||||
WITH duplicates AS (
|
||||
select
|
||||
aggregate_id,
|
||||
aggregate_type
|
||||
from (
|
||||
select
|
||||
aggregate_type,
|
||||
aggregate_id,
|
||||
resource_owner
|
||||
from eventstore.events
|
||||
where aggregate_type not like '%.%'
|
||||
group by
|
||||
aggregate_type,
|
||||
aggregate_id,
|
||||
resource_owner
|
||||
) group by
|
||||
aggregate_id,
|
||||
aggregate_type
|
||||
having count(resource_owner) > 1
|
||||
)
|
||||
SELECT
|
||||
MIN(event_sequence) AS seq,
|
||||
aggregate_type,
|
||||
aggregate_id
|
||||
FROM
|
||||
eventstore.events
|
||||
WHERE
|
||||
aggregate_id IN (select aggregate_id from duplicates)
|
||||
GROUP BY
|
||||
aggregate_type,
|
||||
aggregate_id
|
||||
ORDER BY aggregate_id, seq
|
||||
)
|
||||
SELECT
|
||||
f.*,
|
||||
e.resource_owner
|
||||
FROM
|
||||
first_sequences f
|
||||
JOIN
|
||||
eventstore.events e ON f.seq = e.event_sequence
|
||||
)
|
||||
UPDATE eventstore.events e
|
||||
SET resource_owner = r.resource_owner
|
||||
FROM resource_owners r where e.aggregate_id = r.aggregate_id;
|
Loading…
Reference in New Issue
Block a user