-
Notifications
You must be signed in to change notification settings - Fork 380
Create example project during repo initialization. Various improvements to repo validation #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1289f4b
Create example project durign repo initialization. Various improvemen…
izeigerman a85cb13
Make sure that the start date / time doesn't exceed the end date / time
izeigerman 1d2029b
add incremental to full with dependency
eakmanrq 7f33943
In scheduler start snapshot progress in the console
izeigerman 627f0fc
Rename files in the generate example project
izeigerman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import typing as t | ||
| from functools import wraps | ||
|
|
||
| import click | ||
| from sqlglot.errors import SqlglotError | ||
|
|
||
| from sqlmesh.utils.concurrency import NodeExecutionFailedError | ||
| from sqlmesh.utils.errors import SQLMeshError | ||
|
|
||
|
|
||
| def error_handler(func: t.Callable) -> t.Callable: | ||
| @wraps(func) | ||
| def wrapper(*args, **kwargs): | ||
| try: | ||
| return func(*args, **kwargs) | ||
| except NodeExecutionFailedError as ex: | ||
| raise click.ClickException(str(ex.__cause__)) | ||
| except (SQLMeshError, SqlglotError, ValueError) as ex: | ||
| raise click.ClickException(str(ex)) | ||
|
|
||
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import typing as t | ||
| from enum import Enum | ||
| from pathlib import Path | ||
|
|
||
| import click | ||
|
|
||
| DEFAULT_CONFIG = """import duckdb | ||
| from sqlmesh.core.config import Config | ||
|
|
||
| config = Config( | ||
| engine_connection_factory=duckdb.connect, | ||
| engine_dialect="duckdb", | ||
| ) | ||
|
|
||
|
|
||
| test_config = config | ||
| """ | ||
|
|
||
|
|
||
| DEFAULT_AIRFLOW_CONFIG = """import duckdb | ||
| from sqlmesh.core.config import AirflowSchedulerBackend, Config | ||
|
|
||
| config = Config( | ||
| scheduler_backend=AirflowSchedulerBackend( | ||
| airflow_url="http://localhost:8080/", | ||
| username="airflow", | ||
| password="airflow", | ||
| ), | ||
| backfill_concurrent_tasks=4, | ||
| ddl_concurrent_tasks=4, | ||
| ) | ||
|
|
||
|
|
||
| test_config = Config( | ||
| engine_connection_factory=duckdb.connect, | ||
| engine_dialect="duckdb", | ||
| ) | ||
| """ | ||
|
|
||
| EXAMPLE_SCHEMA_NAME = "sqlmesh_example" | ||
| EXAMPLE_FULL_MODEL_NAME = f"{EXAMPLE_SCHEMA_NAME}.example_full_model" | ||
| EXAMPLE_INCREMENTAL_MODEL_NAME = f"{EXAMPLE_SCHEMA_NAME}.example_incremental_model" | ||
|
|
||
|
|
||
| EXAMPLE_FULL_MODEL_DEF = f"""MODEL ( | ||
| name {EXAMPLE_FULL_MODEL_NAME}, | ||
| kind full, | ||
| cron '@daily', | ||
| ); | ||
|
|
||
| SELECT | ||
| item_id, | ||
| count(distinct id) AS num_orders, | ||
| FROM | ||
| {EXAMPLE_INCREMENTAL_MODEL_NAME} | ||
| GROUP BY item_id | ||
| """ | ||
|
|
||
| EXAMPLE_INCREMENTAL_MODEL_DEF = f"""MODEL ( | ||
| name {EXAMPLE_INCREMENTAL_MODEL_NAME}, | ||
| kind incremental, | ||
| time_column ds, | ||
| start '2020-01-01', | ||
| batch_size 1, | ||
| cron '@daily', | ||
| ); | ||
|
|
||
| SELECT | ||
| id, | ||
| item_id, | ||
| ds, | ||
| FROM | ||
| (VALUES | ||
| (1, 1, '2020-01-01'), | ||
| (1, 2, '2020-01-01'), | ||
| (2, 1, '2020-01-01'), | ||
| (3, 3, '2020-01-03'), | ||
| (4, 1, '2020-01-04'), | ||
| (5, 1, '2020-01-05'), | ||
| (6, 1, '2020-01-06'), | ||
| (7, 1, '2020-01-07') | ||
| ) AS t (id, item_id, ds) | ||
| WHERE | ||
| ds between @start_ds and @end_ds | ||
| """ | ||
|
|
||
| EXAMPLE_AUDIT = f"""AUDIT ( | ||
| name asset_positive_order_ids, | ||
| model {EXAMPLE_FULL_MODEL_NAME} | ||
| ); | ||
|
|
||
| SELECT * | ||
| FROM {EXAMPLE_FULL_MODEL_NAME} | ||
| WHERE | ||
| item_id < 0 | ||
| """ | ||
|
|
||
|
|
||
| EXAMPLE_TEST = f"""test_example_full_model: | ||
| model: {EXAMPLE_FULL_MODEL_NAME} | ||
| inputs: | ||
| {EXAMPLE_INCREMENTAL_MODEL_NAME}: | ||
| rows: | ||
| - id: 1 | ||
| item_id: 1 | ||
| ds: '2020-01-01' | ||
| - id: 2 | ||
| item_id: 1 | ||
| ds: '2020-01-02' | ||
| - id: 3 | ||
| item_id: 2 | ||
| ds: '2020-01-03' | ||
| outputs: | ||
| query: | ||
| rows: | ||
| - item_id: 1 | ||
| num_orders: 2 | ||
| - item_id: 2 | ||
| num_orders: 1 | ||
| """ | ||
|
|
||
|
|
||
| class ProjectTemplate(Enum): | ||
| AIRFLOW = "airflow" | ||
| DEFAULT = "default" | ||
|
|
||
|
|
||
| def init_example_project( | ||
| path: t.Union[str, Path], template: ProjectTemplate = ProjectTemplate.DEFAULT | ||
| ) -> None: | ||
| root_path = Path(path) | ||
| config_path = root_path / "config.py" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. omg i didn't know you could do this
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 I will start using this. |
||
| audits_path = root_path / "audits" | ||
| macros_path = root_path / "macros" | ||
| models_path = root_path / "models" | ||
| tests_path = root_path / "tests" | ||
|
|
||
| if config_path.exists(): | ||
| raise click.ClickException(f"Found an existing config in '{config_path}'") | ||
|
|
||
| _create_folders([audits_path, macros_path, models_path, tests_path]) | ||
| _create_config(config_path, template) | ||
| _create_audits(audits_path) | ||
| _create_models(models_path) | ||
| _create_tests(tests_path) | ||
|
|
||
|
|
||
| def _create_folders(target_folders: t.Sequence[Path]) -> None: | ||
| for folder_path in target_folders: | ||
| folder_path.mkdir() | ||
| (folder_path / ".gitkeep").touch() | ||
|
|
||
|
|
||
| def _create_config(config_path: Path, template: ProjectTemplate) -> None: | ||
| _write_file( | ||
| config_path, | ||
| DEFAULT_AIRFLOW_CONFIG | ||
| if template == ProjectTemplate.AIRFLOW | ||
| else DEFAULT_CONFIG, | ||
| ) | ||
|
|
||
|
|
||
| def _create_audits(audits_path: Path) -> None: | ||
| _write_file(audits_path / "example_full_model.sql", EXAMPLE_AUDIT) | ||
|
|
||
|
|
||
| def _create_models(models_path: Path) -> None: | ||
| for model_name, model_def in [ | ||
| (EXAMPLE_FULL_MODEL_NAME, EXAMPLE_FULL_MODEL_DEF), | ||
| (EXAMPLE_INCREMENTAL_MODEL_NAME, EXAMPLE_INCREMENTAL_MODEL_DEF), | ||
| ]: | ||
| _write_file(models_path / f"{model_name.split('.')[-1]}.sql", model_def) | ||
|
|
||
|
|
||
| def _create_tests(tests_path: Path) -> None: | ||
| _write_file(tests_path / "test_example_full_model.yaml", EXAMPLE_TEST) | ||
|
|
||
|
|
||
| def _write_file(path: Path, payload: str) -> None: | ||
| with open(path, "w", encoding="utf-8") as fd: | ||
| fd.write(payload) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tobymao @eakmanrq can you please help make sure that this is a reasonable enough dummy project to initialize the repo with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to expand this a bit to include two models (1 incremental and 1 full) and have a dependency between the incremental and full. The thinking is to provide two examples with a dependency.