Skip to content

Commit

Permalink
Add SQL deployment scripts for Snowflake object hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
philerooski committed Jun 27, 2024
1 parent 73e1bb9 commit 902ea04
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 1 deletion.
17 changes: 17 additions & 0 deletions snowflake/objects/databases/deploy.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
The current maximum allowed execution depth of EXECUTE IMMEDIATE FROM
statements is 5. Unfortunately, that makes a call stack which looks like:
depth
0 deploy.sql =>
1 databases/deploy.sql =>
2 databases/recover/deploy.sql =>
3 databases/recover/schemas/deploy.sql =>
4 databases/recover/schemas/parquet/deploy.sql =>
5 databases/recover/schemas/parquet/tables/deploy.sql =>
6 databases/recover/schemas/parquet/tables/enrolled_participants.sql
not possible. To circumvent this issue, we omit the highest level of
abstraction (databases/deploy.sql) and instead EXECUTE IMMEDIATE FROM
database deployments individually in the primary deployment script (deploy.sql)
*/
10 changes: 10 additions & 0 deletions snowflake/objects/databases/recover/deploy.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Create a recover database (if it doesn't yet exist) for an environment and
deploy all child objects.
*/
CREATE DATABASE IF NOT EXISTS recover_{{ environment }};
USE DATABASE recover_{{ environment }};

EXECUTE IMMEDIATE
FROM './schemas/deploy.sql'
USING (environment => '{{ environment }}');
6 changes: 6 additions & 0 deletions snowflake/objects/databases/recover/schemas/deploy.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
Deploy schemas and their child objects.
*/
EXECUTE IMMEDIATE
FROM './parquet/deploy.sql'
USING (environment => '{{ environment }}');
26 changes: 26 additions & 0 deletions snowflake/objects/databases/recover/schemas/parquet/deploy.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Create a parquet schema (if it doesn't yet exist) and deploy all child objects.
*/
CREATE SCHEMA IF NOT EXISTS parquet;
USE SCHEMA parquet;

SET parquet_file_format_name = 'parquet_format';
SET parquet_stage_name = 'parquet_s3';

EXECUTE IMMEDIATE
FROM './file_format/deploy.sql'
USING (
parquet_file_format_name => $parquet_file_format_name
);
EXECUTE IMMEDIATE
FROM './stages/deploy.sql'
USING (
environment => '{{ environment }}',
parquet_stage_name => $parquet_stage_name
);
EXECUTE IMMEDIATE
FROM './tables/deploy.sql'
USING (
parquet_file_format_name => $parquet_file_format_name,
parquet_stage_name => $parquet_stage_name
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Deploy all file formats
*/
EXECUTE IMMEDIATE
FROM './parquet_format.sql'
USING (
parquet_file_format_name => '{{ parquet_file_format_name }}'
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Create the Parquet file format
*/
CREATE OR REPLACE FILE FORMAT {{ parquet_file_format_name }}
TYPE = PARQUET
COMPRESSION = AUTO
USE_VECTORIZED_SCANNER = TRUE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Deploy all stages under the `parquet` schema.
*/
EXECUTE IMMEDIATE
FROM './parquet_s3.sql'
USING (
environment => '{{ environment }}',
parquet_stage_name => '{{ parquet_stage_name }}'
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
Create an external stage over the Parquet data in S3
*/
CREATE OR REPLACE STAGE parquet_s3
URL = 's3://recover-processed-data/{{ environment }}/parquet/'
STORAGE_INTEGRATION = recover_prod_s3;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
CREATE OR ALTER all tables
*/
CREATE OR ALTER TABLE enrolledparticipants
USING TEMPLATE (
SELECT ARRAY_AGG(OBJECT_CONSTRUCT(*))
WITHIN GROUP(ORDER BY order_id)
FROM TABLE(
INFER_SCHEMA(
LOCATION => '@{{ parquet_stage_name }}/dataset_enrolledparticipants',
FILE_FORMAT => '{{ parquet_file_format_name }}'
)
)
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
create or alter TABLE enrolled_participants_empty (
CREATE OR ALTER TABLE enrolled_participants (
"id" NUMBER(38,0),
"index" NUMBER(38,0),
"GlobalKey" VARCHAR(16777216),
Expand Down

0 comments on commit 902ea04

Please sign in to comment.