Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayrnt committed Nov 3, 2024
0 parents commit da2eea6
Show file tree
Hide file tree
Showing 38 changed files with 1,347 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
METHOD=oauth
PROJECT=test-project
EXECUTION_PROJECT=test-execution-project
DATASET=dbt_test
#GOOGLE_APPLICATION_CREDENTIALS=
DBT_PROFILES_DIR=
37 changes: 37 additions & 0 deletions .github/workflows/sqlmesh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: SQLMesh Bot
run-name: 🚀SQLMesh Bot 🚀
on:
pull_request:
types:
- synchronize
- opened
# The latest commit is the one that will be used to create the PR environment and deploy to production
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref_name }}
cancel-in-progress: true
jobs:
sqlmesh:
name: SQLMesh Actions Workflow
runs-on: ubuntu-latest
permissions:
# Required to access code in PR
contents: write
# Required to post comments
issues: write
# Required to update check runs
checks: write
# Required to merge
pull-requests: write
steps:
- name: Setup Python
uses: actions/setup-python@v4
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: refs/pull/${{ github.event.issue.pull_request && github.event.issue.number || github.event.pull_request.number }}/merge
- name: Install SQLMesh + Dependencies
run: pip install -r requirements.txt
shell: bash
- name: Run CI/CD Bot
run: |
sqlmesh_cicd -p ${{ github.workspace }}/dbt_project -p ${{ github.workspace }}/sqlmesh_project github --token ${{ secrets.GITHUB_TOKEN }} run-all
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.env
.venv
*/target/
.cache
*.lock
*.old
*.db
logs/*
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 BQ Booster

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.DEFAULT_GOAL := sqlmesh

plan:
sqlmesh -p ./dbt_project -p ./sqlmesh_project plan

test:
sqlmesh -p ./dbt_project -p ./sqlmesh_project test

ui:
sqlmesh -p ./dbt_project -p ./sqlmesh_project ui

sqlmesh:
sqlmesh -p ./dbt_project -p ./sqlmesh_project $(filter-out $@,$(MAKECMDGOALS))

env:
export $(cat .env | xargs)

# Prevent make from interpreting the arguments as make targets
%:
@:
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# dbt sqlmesh multi project

This project is POC to combine dbt with SQLMesh to create a multi project dbt setup.

## Setup

1. Clone the repo
2. Install with peotry: `pip install -r requirements.txt`
3. Copy .env.sample to .env: `cp .env.sample .env`
4. Update the .env file with your configuration
5. Export the .env file: `export $(cat .env | xargs)`
6. Run SQLMesh: `sqlmesh plan`
4 changes: 4 additions & 0 deletions dbt_project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

target/
dbt_packages/
logs/
1 change: 1 addition & 0 deletions dbt_project/.user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 81673b64-3691-402a-a42e-b6085a6702c2
Binary file added dbt_project/__pycache__/config.cpython-311.pyc
Binary file not shown.
Empty file added dbt_project/analyses/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions dbt_project/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file is used to load the sqlmesh configuration
from pathlib import Path

from sqlmesh.core.config import DuckDBConnectionConfig
from sqlmesh.dbt.loader import sqlmesh_config

config = sqlmesh_config(
Path(__file__).parent,
state_connection=DuckDBConnectionConfig(),
)
23 changes: 23 additions & 0 deletions dbt_project/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

name: 'dbt_sqlmesh_multi_project'
version: '1.0.0'

profile: 'dbt_sqlmesh_multi_project'

model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

clean-targets:
- "target"
- "dbt_packages"


models:
+start: Jan 1 2017
dbt_sqlmesh_multi_project:
example:
+materialized: view
Empty file added dbt_project/macros/.gitkeep
Empty file.
11 changes: 11 additions & 0 deletions dbt_project/models/example/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

version: 2

models:
- name: views_per_domain
description: "Views per domain"
columns:
- name: domain
description: "The primary key for this table"
- name: views_count
description: "The number of views"
4 changes: 4 additions & 0 deletions dbt_project/models/example/views_per_domain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ config(materialized='table') }}
SELECT domain, sum(views_count) as views_count
from {{ ref('views_data') }}
GROUP BY ALL
22 changes: 22 additions & 0 deletions dbt_project/models/example/views_per_domain_and_top_browsers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{ config(materialized='table') }}
{%- call set_sql_header(config) %}
CREATE TEMP FUNCTION TOP_BROWSERS(browser STRING) AS (
IF(

browser IN UNNEST(
[
'Chrome',
'Safari',
]
),
browser,
'other'
)
);
{%- endcall %}
SELECT
domain,
TOP_BROWSERS(browser) as browser,
sum(views_count) as views_count
from {{ ref('views_data') }}
GROUP BY ALL
15 changes: 15 additions & 0 deletions dbt_project/profiles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
dbt_sqlmesh_multi_project:
target: dev
outputs:
dev:
type: bigquery
method: "{{ env_var('METHOD', 'oauth') }}"
project: "{{ env_var('PROJECT', 'project-to-update') }}"
execution_project: "{{ env_var('EXECUTION_PROJECT', 'execution-project-to-update') }}"
dataset: "{{ env_var('DATASET', 'dbt_my_username') }}"
threads: "{{ env_var('THREADS', 8) | as_number }}"
keyfile: "{{ env_var('GOOGLE_APPLICATION_CREDENTIALS', 'gcp_credentials.json') }}"
timeout_seconds: "{{ env_var('TIMEOUT_SECONDS', 10800) | as_number }}"
priority: "{{ env_var('PRIORITY', 'interactive') }}"
location: "{{ env_var('LOCATION', 'US') }}"
retries: "{{ env_var('RETRIES', 1) | as_number }}"
Empty file added dbt_project/seeds/.gitkeep
Empty file.
22 changes: 22 additions & 0 deletions dbt_project/seeds/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

version: 2

seeds:
- name: views_data
description: "Views"
columns:
- name: ts
description: "The timestamp"
type: timestamp
- name: domain
description: "The domain"
type: string
- name: os
description: "The operating system"
type: string
- name: browser
description: "The browser"
type: string
- name: views_count
description: "The number of views"
type: integer
9 changes: 9 additions & 0 deletions dbt_project/seeds/views_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ts,domain,browser,os,views_count
2024-11-01 01:07:10,teads.com,Chrome,Android,10
2024-11-02 01:06:10,teads.com,Safari,macOS,8
2024-11-01 01:02:10,teads.com,Chrome,iOS,4
2024-11-01 01:01:10,bqbooster.com,Chrome,Android,3
2024-11-01 01:03:10,bqbooster.com,Firefox,Windows,5
2024-11-02 01:07:10,bqbooster.com,Firefox,Android,6
2024-11-01 01:01:10,kayrnt.fr,Chrome,Android,2
2024-11-01 02:01:10,kayrnt.fr,Edge,Windows,7
Empty file added dbt_project/snapshots/.gitkeep
Empty file.
Empty file added dbt_project/tests/.gitkeep
Empty file.
19 changes: 19 additions & 0 deletions export_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# File containing the environment variables
ENV_FILE=".env"

# Check if the file exists
if [[ ! -f "$ENV_FILE" ]]; then
echo "File $ENV_FILE does not exist."
exit 1
fi

# Export each variable in the file
while IFS= read -r line; do
# Skip empty lines and comments
[[ -z "$line" || "$line" =~ ^# ]] && continue
export "$line"
done < "$ENV_FILE"

echo "Environment variables exported from $ENV_FILE"
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "dbt-sqlmesh-multi-project"
version = "0.1.0"
description = "dbt SQLMesh multi project"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"agate>=1.9.1",
"dbt-bigquery>=1.8.3",
"sqlmesh[bigquery,dbt,github,web]>=0.130.1",
]
Loading

0 comments on commit da2eea6

Please sign in to comment.