ALTER TABLE permissions
    ADD COLUMN IF NOT EXISTS resource_type_id INTEGER;
CREATE INDEX IF NOT EXISTS permissions_resource_type_idx ON permissions(resource_type_id);

UPDATE permissions p
SET resource_type_id = r.resource_type_id
FROM resources r
WHERE p.resource_id = r.id
  AND p.resource_type_id IS NULL;
