Skip to content

Instantly share code, notes, and snippets.

View philerooski's full-sized avatar

Phil Snyder philerooski

  • San Francisco, CA
View GitHub Profile
"""Incrementally backfills COPY_FILES_TASK / COPY_CHANGES_TASK / COPY_SENT_MESSAGES_TASK's
RDS_SNAPSHOTS_STAGE backlog one snapshot_date at a time (SNOW-580).
These tasks time out trying to COPY INTO their entire backlog (150K+ files,
1TB+) in a single statement, even on COMPUTE_MEDIUM. This script drains the
backlog day by day instead: each COPY INTO is scoped to one snapshot_date via
the PATTERN clause, which keeps each run's file count/volume small enough to
finish well inside the warehouse statement timeout. It's safe to re-run --
COPY INTO skips files it has already loaded -- so a partial or interrupted run
can just be re-invoked to pick up where it left off.
@philerooski
philerooski / load_snapshot_data.py
Created June 15, 2026 13:14
Load RDS snapshot data from S3 into Snowflake with column-level masking for raw_table_read_censored
"""
Load RDS snapshot data from S3 via Snowflake external stage into tables.
This script supports two modes:
1. Bootstrap mode (--bootstrap-stack): Creates a new schema, external stage,
file format, and grants privileges before loading data.
2. Manual mode: Loads data into an existing schema with pre-configured stage.
The script dynamically discovers all data types from the S3 stage URL,
creates tables using INFER_SCHEMA from Parquet files, loads the data,
@philerooski
philerooski / verify_pr317_grants.py
Created May 21, 2026 21:06
Verify PR #317 (SNOW-460) grants in Snowflake
"""
Verify that all grants introduced in PR #317 (SNOW-460) are applied.
Checks:
1. Roles exist (new roles added in roles.sql)
2. Schemas exist (schema init migrations)
3. Schema ownership (ownership_grants migrations)
4. Database-level privileges (USAGE, MONITOR on SAGE)
5. Schema-level privileges (USAGE, MONITOR on SAGE.<schema>)
6. Role-to-role grants (analyst roles → DATA_ENGINEER, admin roles → SAGE_ADMIN, etc.)
@philerooski
philerooski / test_verify_default_secondary_roles_summary.sql
Created May 11, 2026 22:30
Snowflake test script: verify default secondary roles summary
USE ROLE USERADMIN;
-- Verification script for DEFAULT_SECONDARY_ROLES test runs.
-- Run this before and after test scripts to compare distribution changes.
-- ============================================================================
-- SECTION 1: SUMMARY COUNT BY DEFAULT_SECONDARY_ROLES BUCKET
-- Captures counts of users in ALL_ENABLED, EMPTY ([]), NULL, and OTHER buckets.
-- ============================================================================
SELECT 'SECTION 1: SUMMARY COUNT BY DSR BUCKET' AS report_section;
@philerooski
philerooski / test_reset_default_secondary_roles_all_users.sql
Created May 11, 2026 22:30
Snowflake test script: reset default secondary roles for all users
USE ROLE USERADMIN;
-- Rollback script for test_set_default_secondary_roles_by_analyst_rules.sql.
-- Resets DEFAULT_SECONDARY_ROLES to empty for all users except SNOWFLAKE.
EXECUTE IMMEDIATE $$
DECLARE
updated_users ARRAY DEFAULT ARRAY_CONSTRUCT();
username STRING;
dsr_value STRING;
user_cursor CURSOR FOR
@philerooski
philerooski / test_set_default_secondary_roles_by_analyst_rules.sql
Created May 11, 2026 22:30
Snowflake test script: set default secondary roles by analyst rules
USE ROLE USERADMIN;
-- Copied from admin/users.sql lines 165-254 for isolated testing.
-- Set DEFAULT_SECONDARY_ROLES based on user type and role access.
-- A user is treated as an analyst if ALL of the following are true:
-- 1) user type is not SERVICE
-- 2) user name does not contain 'service' (case-insensitive)
-- 3) user is not granted any of these roles:
-- DATA_ENGINEER, ACCOUNTADMIN, SYSADMIN, SECURITYADMIN, USERADMIN
-- Analysts get DEFAULT_SECONDARY_ROLES=('ALL').
@philerooski
philerooski / verify_load_snapshot.py
Created February 24, 2026 19:32
A helper script to verify that data loaded by `load_snapshot_data.py` looks as expected
"""
Verify snapshot data load integrity.
This script checks:
1. LOAD_LOG table for any errors or anomalies
2. Compares tables between PROD_576 and PROD_568 schemas
3. Validates record counts (PROD_576 should have more records)
Usage:
python verify_snapshot_load.py --database SYNAPSE_RDS_SNAPSHOT \\
@philerooski
philerooski / download_all_forms.py
Created February 13, 2026 18:54
Download all form data from a specific form group to local directory
#!/usr/bin/env python3
"""
Download all form data from a specific form group to local directory.
"""
import argparse
import sys
import json
import tempfile
import shutil
@philerooski
philerooski / load_snapshot_data.py
Last active May 14, 2026 18:31
The latest version of the script used to load RDS snapshot data into Snowflake
"""
Load RDS snapshot data from S3 via Snowflake external stage into tables.
This script supports two modes:
1. Bootstrap mode (--bootstrap-stack): Creates a new schema, external stage,
file format, and grants privileges before loading data.
2. Manual mode: Loads data into an existing schema with pre-configured stage.
The script dynamically discovers all data types from the S3 stage URL,
creates tables using INFER_SCHEMA from Parquet files, loads the data,
"""
Analyze errors from the LOAD_LOG table.
This script queries the LOAD_LOG table for failed operations,
categorizes errors by type, and groups data types by error category.
This is a complementary script to https://gist.github.com/philerooski/a740b25f066f1ad205344637160aa969
"""
import snowflake.connector