-- +goose Up

INSERT INTO permissions (code, name, resource, action) VALUES
('listing:edit_external', '修改外部上传商品', 'listing', 'edit_external')
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  resource = VALUES(resource),
  action = VALUES(action);

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r, permissions p
WHERE r.code IN ('super_admin', 'ops', 'cs') AND p.code = 'listing:edit_external';

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r, permissions p
WHERE r.code = 'cs' AND p.code = 'listing:view';

-- +goose Down

DELETE rp FROM role_permissions rp
JOIN roles r ON r.id = rp.role_id
JOIN permissions p ON p.id = rp.permission_id
WHERE r.code = 'cs' AND p.code = 'listing:view';

DELETE rp FROM role_permissions rp
JOIN permissions p ON p.id = rp.permission_id
WHERE p.code = 'listing:edit_external';

DELETE FROM permissions WHERE code = 'listing:edit_external';
