You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/configuration.md
+81-28Lines changed: 81 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -244,7 +244,9 @@ This only applies to the _physical tables_ that SQLMesh creates - the views are
244
244
245
245
SQLMesh stores `prod` environment views in the schema in a model's name - for example, the `prod` views for a model `my_schema.users` will be located in `my_schema`.
246
246
247
-
By default, for non-prod environments SQLMesh creates a new schema that appends the environment name to the model name's schema. For example, by default the view for a model `my_schema.users` in a SQLMesh environment named `dev` will be located in the schema `my_schema__dev`.
247
+
By default, for non-prod environments SQLMesh creates a new schema that appends the environment name to the model name's schema. For example, by default the view for a model `my_schema.users` in a SQLMesh environment named `dev` will be located in the schema `my_schema__dev` as `my_schema__dev.users`.
248
+
249
+
##### Show at the table level instead
248
250
249
251
This behavior can be changed to append a suffix at the end of a _table/view_ name instead. Appending the suffix to a table/view name means that non-prod environment views will be created in the same schema as the `prod` environment. The prod and non-prod views are differentiated by non-prod view names ending with `__<env>`.
250
252
@@ -260,7 +262,7 @@ Config example:
260
262
261
263
=== "Python"
262
264
263
-
The Python `environment_suffix_target` argument takes an `EnvironmentSuffixTarget` enumeration with a value of `EnvironmentSuffixTarget.TABLE` or `EnvironmentSuffixTarget.SCHEMA` (default).
265
+
The Python `environment_suffix_target` argument takes an `EnvironmentSuffixTarget` enumeration with a value of `EnvironmentSuffixTarget.TABLE`, `EnvironmentSuffixTarget.CATALOG` or `EnvironmentSuffixTarget.SCHEMA` (default).
264
266
265
267
```python linenums="1"
266
268
from sqlmesh.core.config import Config, ModelDefaultsConfig, EnvironmentSuffixTarget
@@ -271,20 +273,68 @@ Config example:
271
273
)
272
274
```
273
275
274
-
The default behavior of appending the suffix to schemas is recommended because it leaves production with a single clean interface for accessing the views. However, if you are deploying SQLMesh in an environment with tight restrictions on schema creation then this can be a useful way of reducing the number of schemas SQLMesh uses.
276
+
!!! info "Default behavior"
277
+
The default behavior of appending the suffix to schemas is recommended because it leaves production with a single clean interface for accessing the views. However, if you are deploying SQLMesh in an environment with tight restrictions on schema creation then this can be a useful way of reducing the number of schemas SQLMesh uses.
278
+
279
+
##### Show at the catalog level instead
280
+
281
+
If neither the schema (default) nor the table level are sufficient for your use case, you may indicate the environment at the catalog level instead.
282
+
283
+
This can be useful if you have downstream BI reporting tools and you would like to point them at a development environment to test something out without renaming all the table / schema references within the report query.
284
+
285
+
In order to achieve this, you may configure [environment_suffix_target](../reference/configuration.md#environments) like so:
286
+
287
+
=== "YAML"
288
+
289
+
```yaml linenums="1"
290
+
environment_suffix_target: catalog
291
+
```
292
+
293
+
=== "Python"
294
+
295
+
The Python `environment_suffix_target` argument takes an `EnvironmentSuffixTarget` enumeration with a value of `EnvironmentSuffixTarget.TABLE`, `EnvironmentSuffixTarget.CATALOG` or `EnvironmentSuffixTarget.SCHEMA` (default).
296
+
297
+
```python linenums="1"
298
+
from sqlmesh.core.config import Config, ModelDefaultsConfig, EnvironmentSuffixTarget
Given the example of a model called `my_schema.users`, this will cause the following behavior:
307
+
308
+
- For the `prod` environment, the default catalog as configured in the gateway will be used
309
+
- For any other environment, eg `dev`, the environment name will be used as the catalog
310
+
311
+
Therefore, a model named `my_schema.users` in an environment called `dev` will have its view created as `dev.my_schema.users`.
312
+
313
+
If you would like more control over the catalog name to use for a given environment, you may optionally configure [environment_catalog_mapping](#environment-view-catalogs) as described below.
314
+
315
+
!!! warning "Caveats"
316
+
- Using `environment_suffix_target: catalog` only works on engines that support querying across different catalogs. If your engine does not support cross-catalog queries then you will need to use `environment_suffix_target: schema` or `environment_suffix_target: table` instead.
317
+
- SQLMesh will not attempt to create catalogs on demand or drop them as part of janitor cleanup. Using `environment_suffix_target: catalog` assumes the catalogs already exist in the target database and are being managed outside of SQLMesh.
318
+
- When using `environment_catalog_mapping`, you need to ensure that your regex patterns do not conflict. If you map multiple environments to the same catalog, SQLMesh will overwrite existing views. To prevent this, it's recommended to make use of the `@{environment_name}` placeholder to ensure that environment catalog names are always unique.
319
+
275
320
276
321
#### Environment view catalogs
277
322
278
323
By default, SQLMesh creates an environment view in the same [catalog](../concepts/glossary.md#catalog) as the physical table the view points to. The physical table's catalog is determined by either the catalog specified in the model name or the default catalog defined in the connection.
279
324
280
-
Some companies fully segregate `prod` and non-prod environment objects by catalog. For example, they might have a "prod" catalog that contains all `prod` environment physical tables and views and a separate "dev" catalog that contains all `dev` environment physical tables and views.
325
+
It can be desirable to create `prod` and non-prod virtual layer objects in separate catalogs instead. For example, there might be a "prod" catalog that contains all `prod` environment views and a separate "dev" catalog that contains all `dev` environment views.
281
326
282
327
Separate prod and non-prod catalogs can also be useful if you have a CI/CD pipeline that creates environments, like the [SQLMesh Github Actions CI/CD Bot](../integrations/github.md). You might want to store the CI/CD environment objects in a dedicated catalog since there can be many of them.
283
328
329
+
!!! info "Virtual layer only"
330
+
Note that the following setting only affects the [virtual layer](../concepts/glossary.md#virtual-layer). If you need full segregation by catalog between environments in the [physical layer](../concepts/glossary.md#physical-layer) as well, see the [Isolated Systems Guide](../guides/isolated_systems.md).
331
+
284
332
To configure separate catalogs, provide a mapping from [regex patterns](https://en.wikipedia.org/wiki/Regular_expression) to catalog names. SQLMesh will compare the name of an environment to the regex patterns; when it finds a match it will store the environment's objects in the corresponding catalog.
285
333
286
334
SQLMesh evaluates the regex patterns in the order defined in the configuration; it uses the catalog for the first matching pattern. If no match is found, the catalog defined in the model or the default catalog defined on the connection will be used.
287
335
336
+
In addition, you may specify an `@{environment_name}` placeholder in the mapping. It will be substituted with the target environment name at runtime.
337
+
288
338
Config example:
289
339
290
340
=== "YAML"
@@ -294,6 +344,7 @@ Config example:
294
344
'^prod$': prod
295
345
'^dev.*': dev
296
346
'^analytics_repo.*': cicd
347
+
'.*': 'user_@{environment_name}'
297
348
```
298
349
299
350
=== "Python"
@@ -307,6 +358,7 @@ Config example:
307
358
'^prod$': 'prod',
308
359
'^dev.*': 'dev',
309
360
'^analytics_repo.*': 'cicd',
361
+
'.*': 'user_@{environment_name}'
310
362
},
311
363
)
312
364
```
@@ -316,6 +368,7 @@ With the example configuration above, SQLMesh would evaluate environment names a
316
368
* If the environment name is `prod`, the catalog will be `prod`.
317
369
* If the environment name starts with `dev`, the catalog will be `dev`.
318
370
* If the environment name starts with `analytics_repo`, the catalog will be `cicd`.
371
+
* If the environment name is anything else, eg `homer_simpson`, the catalog will be `user_homer_simpson`.
319
372
320
373
*Note:* This feature is only available for engines that support querying across catalogs. At the time of writing, the following engines are **NOT** supported:
Even though the second change should have been a metadata change (thus not requiring a backfill), it will still be classified as a breaking change because the comparison is against production instead of the previous development state. This is intentional and may cause additional backfills as more changes are accumulated.
545
+
Even though the second change should have been a metadata change (thus not requiring a backfill), it will still be classified as a breaking change because the comparison is against production instead of the previous development state. This is intentional and may cause additional backfills as more changes are accumulated.
0 commit comments