DEPT SQL runner
The DEPT SQL runner has three basic functionalities
- executing SQL code in a specific order
runner --execute {RUNNER_FILE_1}, {RUNNER_FILE_2} ..
- executing SQL code in a specific order, in staging mode (on test schema, tables and data)
runner --staging {RUNNER_FILE_1}, {RUNNER_FILE_2} ..
- quickly testing SQL code through temporary creation of views
runner --test {RUNNER_FILE_1}, {RUNNER_FILE_2} ..
- plotting of a dependency graph
runner --deps
An alias for the runner command is sqlrunner, for legacy purposes.
Using run_sql will run in interactive mode. run_sql /path/to/config.json
The supported databases are Redshift, Snowflake and Postgres.
Installation
SQL-Runner has the following optional dependencies that have to be mentioned when needed, during the installation process with pip:
azuredwh- for work with Azure SQL Data Warehousesnowflake- for working with Snowflake DBredshift- for working with AWS Redshiftbigquery- for working with Google BigQuerys3- for enabling AWS S3 API access (for saving dependencies SVG graph)
Additionally for Azure DWH, it's required to install the Microsoft ODBC Driver. For Ubuntu 18.04 this is sufficient:
# In case any of these gest stuck, simply run `sudo su` once, to cache the password, then exit using Ctrl+D
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list > /dev/null
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
sudo apt-get install unixodbc-dev
Another dependency is graphviz:
sudo apt install graphviz
It is highly recommend it to install it in a virtual environment.
To create a virtual environment, run this:
sudo apt-get install python3-virtualenv
python3 -m virtualenv -p python3 venv
To install in a virtual environment, run this:
source venv/bin/activate
# Install with dependencies, ex. s3 and azuredwh
pip install git+https://github.com/leroi-marketing/sql-runner.git#egg=sql-runner[azuredwh]
But if you really want to install it globally, run this:
sudo apt install python3-pip
# Install with dependencies, ex. s3 and azuredwh
sudo pip install git+https://github.com/leroi-marketing/sql-runner.git#egg=sql-runner[azuredwh]
Configuration
Two configuration files are needed to use the sqlrunner.
- A config.json file that specifies all the necessary configuration variables. The default path is
auth/config.jsonrelative to the directory that this is run from.
{
"sql_path": "{PATH}",
"database_type": "[snowflake|redshift|postgres|bigquery|azuredwh]",
"auth": {
// For Azure Synapse Analytics only
"server": "url.of.azuredwh.server",
// for BigQuery only
"credentials_path": "/path/to/google-generated-credentials.json",
// for Snowflake only
"account": "{SNOWFLAKE_ACCOUNT}",
// Azure Synapse Analytics DB, or Snowflake DB, or BigQuery DB
"database": "{DATABASE}",
// Postgresql or Redshift
"dbname": "{POSTGRES_DATABASE} OR {REDSHIFT_DATABASE}",
"host": "{POSTGRES_HOSTNAME} OR {REDSHIFT_HOSTNAME}",
"port": "{POSTGRES_PORT} OR {REDSHIFT_PORT}"
// Snowflake, postgres, redshift
"user": "{USERNAME}",
// Azure Synapse Analytics
"username": "{USERNAME}",
// All except Google BigQuery
"password": "{PASSWORD}",
},
// configure staging environments as database suffix for all but the source data objects
"staging": {
"override": {
"database": {
"suffix": "_STAGING1"
}
},
// python3 code that exposes `re` - regular expressions module, `database`, `schema`, `relation` being referenced
"except": "not re.match('dwh', database.lower()) or re.search('^x', schema)"
},
// configure test schema creation locations as a schema prefix for all but the source data objects
"test": {
"override": {
"schema": {
"prefix": "zz_"
}
},
// python3 code that exposes `re` - regular expressions module, `database`, `schema`, `relation` being referenced
"except": "not re.match('dwh', database.lower()) or re.search('^x', schema)"
},
// Add a dependency cache file, to speed up run initialization
"deps_cache": {
"type": "filesystem",
"location": "/path/to/local/cache/dependencies.csv"
},
"deps_schema": "{DEPENDENCY_SCHEMA_NAME}",
"exclude_dependencies": [
"EXCLUDED_SCHEMA_1",
"EXCLUDED_SCHEMA_2"
],
"graphviz_path": "{GRAPHVIZ_PATH_FOR_WINDOWS}"
}
- One or more csv files specifying the name of the the tables and views and their respective schemas.
{SCHEMA_1};{SQL_FILENAME_1};e
{SCHEMA_1};{SQL_FILENAME_2};e
{SCHEMA_1};{SQL_FILENAME_3};e
{SCHEMA_2};{SQL_FILENAME_4};e
{SCHEMA_3};{SQL_FILENAME_5};e
..
Per schema one directory is expected. The name of the SQL files should correspond to thename of the respective table or view. The last columns specifies the desired action.
e: execute the query
t: create table
v: create view
m: materialize view
check: run assertions on query result
Development
To set up dependencies locally for development:
# Install virtualenv (if your default python is python2, specify also `-p python3`)
python3 -m virtualenv -p python3 venv
source venv/bin/activate
pip install -e .[azuredwh] # and other optional dependencies
# Run local (non-build) version:
python debug.py [arg1 arg2 ...]
Functional comments
Queries can have functional comments on the top. These comments can either specify data distribution for Azure Synapse Analytics or RedShift, or can contain assertions for check queries.
This needs better documentation, but for now you can check the source code for the DB-specific Query classes in sql_runner/db.
Metadata
Release files for sql-runner 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sql-runner-0.4.3.tar.gz | 22.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sql_runner-0.4.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 69.0 kB
Release files / sql-runner-0.4.3.tar.gz
| Download URL | sql-runner-0.4.3.tar.gz |
|---|---|
| Size | 22.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b9174bb65c6b3645a00ece68fbdad3f5d5d4564a363dd6ae3413ef95145a90a
|
|
BLAKE2b-256 checksum How to use checksums |
2bbbe239807c6a34ba00696c94df83f7dcf4438e4eef6075614ad4be7e2f7283
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10
|
Release files / sql_runner-0.4.3-py3-none-any.whl
| Download URL | sql_runner-0.4.3-py3-none-any.whl |
|---|---|
| Size | 46.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9fe3227bb1497ea52b20218db05f8b01f4dc535af578e79a4b896a044d273cce
|
|
BLAKE2b-256 checksum How to use checksums |
06492731a659294dde4658608f32448e8da0ae8c76d0fcb6b5e3fcb1b48bbfcb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10
|