|
| 1 | +import typing as t |
| 2 | +from enum import Enum |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import click |
| 6 | + |
| 7 | +DEFAULT_CONFIG = """import duckdb |
| 8 | +from sqlmesh.core.config import Config |
| 9 | +
|
| 10 | +config = Config( |
| 11 | + engine_connection_factory=duckdb.connect, |
| 12 | + engine_dialect="duckdb", |
| 13 | +) |
| 14 | +
|
| 15 | +
|
| 16 | +test_config = config |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +DEFAULT_AIRFLOW_CONFIG = """import duckdb |
| 21 | +from sqlmesh.core.config import AirflowSchedulerBackend, Config |
| 22 | +
|
| 23 | +config = Config( |
| 24 | + scheduler_backend=AirflowSchedulerBackend( |
| 25 | + airflow_url="http://localhost:8080/", |
| 26 | + username="airflow", |
| 27 | + password="airflow", |
| 28 | + ), |
| 29 | + backfill_concurrent_tasks=4, |
| 30 | + ddl_concurrent_tasks=4, |
| 31 | +) |
| 32 | +
|
| 33 | +
|
| 34 | +test_config = Config( |
| 35 | + engine_connection_factory=duckdb.connect, |
| 36 | + engine_dialect="duckdb", |
| 37 | +) |
| 38 | +""" |
| 39 | + |
| 40 | +EXAMPLE_SCHEMA_NAME = "sqlmesh_example" |
| 41 | +EXAMPLE_FULL_MODEL_NAME = f"{EXAMPLE_SCHEMA_NAME}.example_full_model" |
| 42 | +EXAMPLE_INCREMENTAL_MODEL_NAME = f"{EXAMPLE_SCHEMA_NAME}.example_incremental_model" |
| 43 | + |
| 44 | + |
| 45 | +EXAMPLE_FULL_MODEL_DEF = f"""MODEL ( |
| 46 | + name {EXAMPLE_FULL_MODEL_NAME}, |
| 47 | + kind full, |
| 48 | + cron '@daily', |
| 49 | +); |
| 50 | +
|
| 51 | +SELECT |
| 52 | + item_id, |
| 53 | + count(distinct id) AS num_orders, |
| 54 | +FROM |
| 55 | + {EXAMPLE_INCREMENTAL_MODEL_NAME} |
| 56 | +GROUP BY item_id |
| 57 | +""" |
| 58 | + |
| 59 | +EXAMPLE_INCREMENTAL_MODEL_DEF = f"""MODEL ( |
| 60 | + name {EXAMPLE_INCREMENTAL_MODEL_NAME}, |
| 61 | + kind incremental, |
| 62 | + time_column ds, |
| 63 | + start '2020-01-01', |
| 64 | + batch_size 1, |
| 65 | + cron '@daily', |
| 66 | +); |
| 67 | +
|
| 68 | +SELECT |
| 69 | + id, |
| 70 | + item_id, |
| 71 | + ds, |
| 72 | +FROM |
| 73 | + (VALUES |
| 74 | + (1, 1, '2020-01-01'), |
| 75 | + (1, 2, '2020-01-01'), |
| 76 | + (2, 1, '2020-01-01'), |
| 77 | + (3, 3, '2020-01-03'), |
| 78 | + (4, 1, '2020-01-04'), |
| 79 | + (5, 1, '2020-01-05'), |
| 80 | + (6, 1, '2020-01-06'), |
| 81 | + (7, 1, '2020-01-07') |
| 82 | + ) AS t (id, item_id, ds) |
| 83 | +WHERE |
| 84 | + ds between @start_ds and @end_ds |
| 85 | +""" |
| 86 | + |
| 87 | +EXAMPLE_AUDIT = f"""AUDIT ( |
| 88 | + name asset_positive_order_ids, |
| 89 | + model {EXAMPLE_FULL_MODEL_NAME} |
| 90 | +); |
| 91 | +
|
| 92 | +SELECT * |
| 93 | +FROM {EXAMPLE_FULL_MODEL_NAME} |
| 94 | +WHERE |
| 95 | + item_id < 0 |
| 96 | +""" |
| 97 | + |
| 98 | + |
| 99 | +EXAMPLE_TEST = f"""test_example_full_model: |
| 100 | + model: {EXAMPLE_FULL_MODEL_NAME} |
| 101 | + inputs: |
| 102 | + {EXAMPLE_INCREMENTAL_MODEL_NAME}: |
| 103 | + rows: |
| 104 | + - id: 1 |
| 105 | + item_id: 1 |
| 106 | + ds: '2020-01-01' |
| 107 | + - id: 2 |
| 108 | + item_id: 1 |
| 109 | + ds: '2020-01-02' |
| 110 | + - id: 3 |
| 111 | + item_id: 2 |
| 112 | + ds: '2020-01-03' |
| 113 | + outputs: |
| 114 | + query: |
| 115 | + rows: |
| 116 | + - item_id: 1 |
| 117 | + num_orders: 2 |
| 118 | + - item_id: 2 |
| 119 | + num_orders: 1 |
| 120 | +""" |
| 121 | + |
| 122 | + |
| 123 | +class ProjectTemplate(Enum): |
| 124 | + AIRFLOW = "airflow" |
| 125 | + DEFAULT = "default" |
| 126 | + |
| 127 | + |
| 128 | +def init_example_project( |
| 129 | + path: t.Union[str, Path], template: ProjectTemplate = ProjectTemplate.DEFAULT |
| 130 | +) -> None: |
| 131 | + root_path = Path(path) |
| 132 | + config_path = root_path / "config.py" |
| 133 | + audits_path = root_path / "audits" |
| 134 | + macros_path = root_path / "macros" |
| 135 | + models_path = root_path / "models" |
| 136 | + tests_path = root_path / "tests" |
| 137 | + |
| 138 | + if config_path.exists(): |
| 139 | + raise click.ClickException(f"Found an existing config in '{config_path}'") |
| 140 | + |
| 141 | + _create_folders([audits_path, macros_path, models_path, tests_path]) |
| 142 | + _create_config(config_path, template) |
| 143 | + _create_audits(audits_path) |
| 144 | + _create_models(models_path) |
| 145 | + _create_tests(tests_path) |
| 146 | + |
| 147 | + |
| 148 | +def _create_folders(target_folders: t.Sequence[Path]) -> None: |
| 149 | + for folder_path in target_folders: |
| 150 | + folder_path.mkdir() |
| 151 | + (folder_path / ".gitkeep").touch() |
| 152 | + |
| 153 | + |
| 154 | +def _create_config(config_path: Path, template: ProjectTemplate) -> None: |
| 155 | + _write_file( |
| 156 | + config_path, |
| 157 | + DEFAULT_AIRFLOW_CONFIG |
| 158 | + if template == ProjectTemplate.AIRFLOW |
| 159 | + else DEFAULT_CONFIG, |
| 160 | + ) |
| 161 | + |
| 162 | + |
| 163 | +def _create_audits(audits_path: Path) -> None: |
| 164 | + _write_file(audits_path / "example_full_model.sql", EXAMPLE_AUDIT) |
| 165 | + |
| 166 | + |
| 167 | +def _create_models(models_path: Path) -> None: |
| 168 | + for model_name, model_def in [ |
| 169 | + (EXAMPLE_FULL_MODEL_NAME, EXAMPLE_FULL_MODEL_DEF), |
| 170 | + (EXAMPLE_INCREMENTAL_MODEL_NAME, EXAMPLE_INCREMENTAL_MODEL_DEF), |
| 171 | + ]: |
| 172 | + _write_file(models_path / f"{model_name.split('.')[-1]}.sql", model_def) |
| 173 | + |
| 174 | + |
| 175 | +def _create_tests(tests_path: Path) -> None: |
| 176 | + _write_file(tests_path / "test_example_full_model.yaml", EXAMPLE_TEST) |
| 177 | + |
| 178 | + |
| 179 | +def _write_file(path: Path, payload: str) -> None: |
| 180 | + with open(path, "w", encoding="utf-8") as fd: |
| 181 | + fd.write(payload) |
0 commit comments