├── LICENSE ├── README.md ├── cicd.sh ├── cloudbuild.view.yaml ├── cloudbuild.yaml ├── view.sh └── views └── dataset_example └── view_cart_latest.sql /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 robertsahlin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bigquery-views-builder 2 | clone to get an easy setup for version control and automatic builds of your bigquery views. Add your view DDLs under the folders that represent the respective dataset the view belongs to. Set up a trigger in cloud build that listens to changes of views/** in the branch of your choice. 3 | 4 | Read more at [https://robertsahlin.com/automatic-builds-and-version-control-of-your-bigquery-views/](https://robertsahlin.com/automatic-builds-and-version-control-of-your-bigquery-views/) 5 | 6 | -------------------------------------------------------------------------------- /cicd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #./cicd.sh mathem-ml-datahem-test views 3 | project_id=$1 4 | views_dir=$2 5 | location=${3:-EU} 6 | 7 | bq_safe_mk() { 8 | dataset=$1 9 | exists=$(bq ls -d | grep -w $dataset) 10 | if [ -n "$exists" ]; then 11 | echo "Not creating $dataset since it already exists" 12 | else 13 | echo "Creating dataset $project_id:$dataset with location: $location" 14 | bq --location=$location mk $project_id:$dataset 15 | fi 16 | } 17 | 18 | for dir_entry in $(find ./$views_dir -mindepth 1 -maxdepth 1 -type d -printf '%f\n') 19 | do 20 | echo "$dir_entry" 21 | bq_safe_mk $dir_entry 22 | done 23 | 24 | for file_entry in $(find ./$views_dir -type f -follow -print) 25 | do 26 | echo "$file_entry" 27 | gcloud builds submit --async --config=cloudbuild.view.yaml --substitutions=_SQL_FILE="$file_entry" . 28 | done -------------------------------------------------------------------------------- /cloudbuild.view.yaml: -------------------------------------------------------------------------------- 1 | # gcloud builds submit --config=cloudbuild.yaml . 2 | steps: 3 | - name: gcr.io/cloud-builders/gcloud 4 | entrypoint: 'bash' 5 | args: ['view.sh', '$PROJECT_ID', '${_SQL_FILE}'] 6 | timeout: 1200s 7 | substitutions: 8 | _SQL_FILE: none 9 | options: 10 | substitution_option: 'ALLOW_LOOSE' -------------------------------------------------------------------------------- /cloudbuild.yaml: -------------------------------------------------------------------------------- 1 | # gcloud builds submit --config=cloudbuild.yaml . 2 | steps: 3 | - name: gcr.io/cloud-builders/gcloud 4 | entrypoint: 'bash' 5 | args: ['cicd.sh', '$PROJECT_ID', '${_VIEWS_DIR}'] 6 | timeout: 1200s 7 | substitutions: 8 | _VIEWS_DIR: views -------------------------------------------------------------------------------- /view.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | project_id=$1 3 | sql_file=$2 4 | echo "$sql_file" 5 | query="$(cat $sql_file)" 6 | echo "${query///$project_id}" 7 | bq query --batch --use_legacy_sql=false "${query///$project_id}" -------------------------------------------------------------------------------- /views/dataset_example/view_cart_latest.sql: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE VIEW `.dataset_example.view_cart_latest` 2 | OPTIONS( 3 | description="the latest representation of each cart" 4 | ) 5 | AS 6 | SELECT * EXCEPT(row_number) 7 | FROM ( 8 | SELECT 9 | *, 10 | ROW_NUMBER() OVER(PARTITION BY Id ORDER BY PartitionTimestamp DESC) row_number 11 | FROM( 12 | SELECT * FROM `.dataset_example.cart_timeseries` 13 | UNION ALL 14 | SELECT * FROM `.dataset_example.cart_staging` WHERE _PARTITIONDATE >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 DAY) 15 | ) 16 | ) 17 | WHERE row_number = 1 --------------------------------------------------------------------------------