【Snowflake】【トラブル】所有権変更後にエラー「current role has no privileges on it」

■ はじめに

Snowflakeでトラブったので、
自戒の意味を込めてもメモ、、、

目次

【1】トラブル内容
 1)エラーまでのSQL文
【2】エラー内容
【3】原因
【4】解決案
【5】Tips
 1)SHOW GRANTS
 2)GRANT

【1】トラブル内容

テーブルの普及作業でゴミがでてしまったので、
そのゴミデータを削除する必要があり、
テーブルデータ削除するためには、
所有権を変更した後に削除した。

しかし、翌日の定期処理で
エラー「current role has no privileges on it」が発生した
(詳細は、以下の「【2】エラー内容」参照)

1)エラーまでのSQL

use databae test_db;
use schema pulblic;

-- delete 前に テーブルの削除権限がないので所有権を変更
grant ownership on table sample_table
  to role operation_user revoke current grants;

-- 削除
delete from sample_table where create_at='2023-04-13';

/*
 * 翌日の定期処理、、、
 */

-- ここでエラー★
create table sample_table if not exists (
  sample_id varchar,
  ...
);

【2】エラー内容

SQL compilation error Table 'SAMPLE_TABLE' already exists.
but current role has no privileges on it.
If this is unexpected and you cannot resolve this problem,
contact your system administrator.
ACCOUNTADMIN role may be required to manage the privileges on the object.

【3】原因

* 所有権変更した際に「revoke current grants」を指定し、
 現在のアウトバウンド権限すべての破棄してしまったため。

発生原因のSQL

-- 「revoke current grants」:現在のアウトバウンド権限すべての破棄
grant ownership on table sample_table
  to role operation_user revoke current grants;

本来行わなければならなかったSQL

-- 「copy current grants」:現在のアウトバウンド権限すべてのコピー
grant ownership on table sample_table
  to role operation_user copy current grants;

【4】解決案

 「revoke current grants」した後に、
「copy current grants」しても、時すでに遅しなので
復旧例を載せておく

[1] 「show grants on table」での権限の確認

-- sample_table テーブルに付与された権限を一覧表示
show grants on table sample_table;
-- privilege が OWNERSHIP しかなく
-- INSERT/SELECTなどは削除されている

-- 他のテーブルの確認
show grants on table other_table;
-- privilege に INSERT/SELECTなどが表示

[2] 権限を追加していく

grant
 insert
 ,delete
 ,rebuild
 ,references
 ,truncate
 ,update
 ,select
 --他にもあれば、、、
 on table sample_table to role operation_user;

【5】Tips

1)SHOW GRANTS

* アクセス権限の確認

https://docs.snowflake.com/ja/sql-reference/sql/show-grants

コマンド例

-- sales テーブルで付与されたすべての権限をリスト
SHOW GRANTS ON TABLE user;

2)GRANT

* 権限付与

https://docs.snowflake.com/ja/sql-reference/sql/grant-privilege

関連記事

Snowflake ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/02/130111
Snowflake ~ 入門編 / Hello world
https://dk521123.hatenablog.com/entry/2021/11/22/212520
Snowflake ~ 基本編 / アクセス制御 ~
https://dk521123.hatenablog.com/entry/2021/11/16/231010
Snowflake ~ GRANT OWNERSHIP ~
https://dk521123.hatenablog.com/entry/2022/02/25/094250
権限トラブル時のTips
https://dk521123.hatenablog.com/entry/2022/08/19/131922
エラー「Insufficient privileges to operate」時の対応
https://dk521123.hatenablog.com/entry/2022/08/02/090439