diff --git a/src/meta_api_grabber/views/SETUP_PERMISSIONS.md b/src/meta_api_grabber/views/SETUP_PERMISSIONS.md new file mode 100644 index 0000000..c2ddb62 --- /dev/null +++ b/src/meta_api_grabber/views/SETUP_PERMISSIONS.md @@ -0,0 +1,25 @@ +# View Permissions Setup + +The scheduled grabber needs the `meta_user` to have permissions to create/drop/modify views. + +## One-time Setup (run as superuser or database owner) + +```sql +-- Give meta_user the ability to create views in the public schema +GRANT CREATE ON SCHEMA public TO meta_user; + +-- Alternative: Make meta_user the owner of all views (if they already exist) +-- ALTER MATERIALIZED VIEW account_insights_flattened OWNER TO meta_user; +-- ALTER MATERIALIZED VIEW campaign_insights_flattened OWNER TO meta_user; +-- ALTER MATERIALIZED VIEW adset_insights_flattened OWNER TO meta_user; +``` + +Run these commands once as a superuser/database owner, then the scheduled grabber can manage views normally. + +## Why This Is Needed + +PostgreSQL materialized views must be owned by the user who created them. Since the scheduled grabber recreates views on startup (to apply schema changes), it needs permission to: +- `DROP MATERIALIZED VIEW` - remove old views +- `CREATE MATERIALIZED VIEW` - create new views + +Without proper schema permissions, the `meta_user` cannot perform these operations. diff --git a/src/meta_api_grabber/views/account_insights.sql b/src/meta_api_grabber/views/account_insights.sql index 6e714dd..982a45e 100644 --- a/src/meta_api_grabber/views/account_insights.sql +++ b/src/meta_api_grabber/views/account_insights.sql @@ -1,4 +1,6 @@ -CREATE OR REPLACE MATERIALIZED VIEW account_insights_flattened AS +DROP MATERIALIZED VIEW IF EXISTS account_insights_flattened CASCADE; + +CREATE MATERIALIZED VIEW account_insights_flattened AS SELECT time, account_id, diff --git a/src/meta_api_grabber/views/adset_insights.sql b/src/meta_api_grabber/views/adset_insights.sql index 8b87b21..708feb0 100644 --- a/src/meta_api_grabber/views/adset_insights.sql +++ b/src/meta_api_grabber/views/adset_insights.sql @@ -1,4 +1,6 @@ -CREATE OR REPLACE MATERIALIZED VIEW adset_insights_flattened AS +DROP MATERIALIZED VIEW IF EXISTS adset_insights_flattened CASCADE; + +CREATE MATERIALIZED VIEW adset_insights_flattened AS SELECT time, adset_id, diff --git a/src/meta_api_grabber/views/campaign_insights.sql b/src/meta_api_grabber/views/campaign_insights.sql index 8527548..824baab 100644 --- a/src/meta_api_grabber/views/campaign_insights.sql +++ b/src/meta_api_grabber/views/campaign_insights.sql @@ -1,6 +1,8 @@ --- campaign insights -CREATE OR REPLACE MATERIALIZED VIEW campaign_insights_flattened AS +DROP MATERIALIZED VIEW IF EXISTS campaign_insights_flattened CASCADE; + +CREATE MATERIALIZED VIEW campaign_insights_flattened AS SELECT time, account_id,