START TRANSACTION;

INSERT IGNORE INTO permissions (code, name, module, created_at) VALUES
('spk.manage', 'Kelola Data SPK', 'spk', NOW());

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT DISTINCT rp.role_id, target.id
FROM role_permissions rp
JOIN permissions source ON source.id = rp.permission_id
JOIN permissions target ON target.code = 'spk.manage'
WHERE source.code IN ('spk.create', 'spk.edit');

COMMIT;
