dbt-sqlserver 1.12.0rc2__tar.gz → 1.12.0rc4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/PKG-INFO +159 -15
  2. dbt_sqlserver-1.12.0rc4/README.md +466 -0
  3. dbt_sqlserver-1.12.0rc4/dbt/adapters/sqlserver/__version__.py +1 -0
  4. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/relation_configs/index.py +8 -2
  5. dbt_sqlserver-1.12.0rc4/dbt/adapters/sqlserver/sqlserver_adapter.py +1158 -0
  6. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_configs.py +8 -0
  7. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_connections.py +98 -19
  8. dbt_sqlserver-1.12.0rc4/dbt/adapters/sqlserver/sqlserver_deny.py +141 -0
  9. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_relation.py +1 -1
  10. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_runtime.py +5 -0
  11. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/adapters/apply_denies.sql +118 -0
  12. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/apply_masks.sql +10 -2
  13. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/columns.sql +7 -1
  14. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/indexes.sql +21 -7
  15. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/metadata.sql +49 -0
  16. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/schema.sql +41 -15
  17. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/materializations/hooks.sql +58 -0
  18. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/materializations/models/incremental/incremental.sql +270 -0
  19. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/materializations/models/table/columns_spec_ddl.sql +79 -0
  20. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/materializations/models/table/table.sql +259 -0
  21. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/materializations/models/table/table_dml_refresh.sql +200 -0
  22. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/models/unit_test/unit_test_create_table_as.sql +5 -1
  23. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/models/view/view.sql +40 -6
  24. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/snapshots/helpers.sql +31 -16
  25. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/materializations/snapshots/snapshot.sql +206 -0
  26. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/tests.sql +2 -5
  27. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/unit_tests.sql +1 -4
  28. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/relations/table/create.sql +395 -0
  29. dbt_sqlserver-1.12.0rc4/dbt/include/sqlserver/macros/utils/openquery.sql +21 -0
  30. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt_sqlserver.egg-info/PKG-INFO +159 -15
  31. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt_sqlserver.egg-info/SOURCES.txt +3 -0
  32. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/pyproject.toml +42 -6
  33. dbt_sqlserver-1.12.0rc2/README.md +0 -322
  34. dbt_sqlserver-1.12.0rc2/dbt/adapters/sqlserver/__version__.py +0 -1
  35. dbt_sqlserver-1.12.0rc2/dbt/adapters/sqlserver/sqlserver_adapter.py +0 -693
  36. dbt_sqlserver-1.12.0rc2/dbt/include/sqlserver/macros/materializations/hooks.sql +0 -23
  37. dbt_sqlserver-1.12.0rc2/dbt/include/sqlserver/macros/materializations/models/incremental/incremental.sql +0 -196
  38. dbt_sqlserver-1.12.0rc2/dbt/include/sqlserver/macros/materializations/models/table/columns_spec_ddl.sql +0 -30
  39. dbt_sqlserver-1.12.0rc2/dbt/include/sqlserver/macros/materializations/models/table/table.sql +0 -132
  40. dbt_sqlserver-1.12.0rc2/dbt/include/sqlserver/macros/materializations/models/table/table_dml_refresh.sql +0 -133
  41. dbt_sqlserver-1.12.0rc2/dbt/include/sqlserver/macros/materializations/snapshots/snapshot.sql +0 -135
  42. dbt_sqlserver-1.12.0rc2/dbt/include/sqlserver/macros/relations/table/create.sql +0 -264
  43. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/LICENSE +0 -0
  44. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/__init__.py +0 -0
  45. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/__init__.py +0 -0
  46. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/py.typed +0 -0
  47. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/relation_configs/__init__.py +0 -0
  48. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/relation_configs/policies.py +0 -0
  49. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_auth.py +0 -0
  50. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_backend.py +0 -0
  51. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_column.py +0 -0
  52. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_constants.py +0 -0
  53. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_credentials.py +0 -0
  54. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_helpers.py +0 -0
  55. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/adapters/sqlserver/sqlserver_mask.py +0 -0
  56. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/__init__.py +0 -0
  57. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/dbt_project.yml +0 -0
  58. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/apply_grants.sql +0 -0
  59. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/catalog.sql +0 -0
  60. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/persist_docs.sql +0 -0
  61. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/relation.sql +0 -0
  62. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/show.sql +0 -0
  63. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/adapters/validate_sql.sql +0 -0
  64. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/functions/helpers.sql +0 -0
  65. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/functions/scalar.sql +0 -0
  66. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/models/incremental/incremental_strategies.sql +0 -0
  67. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/models/incremental/merge.sql +0 -0
  68. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/models/unit_test/get_fixture_sql.sql +0 -0
  69. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/models/view/create_view_as.sql +0 -0
  70. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/snapshots/snapshot_merge.sql +0 -0
  71. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/materializations/snapshots/strategies.sql +0 -0
  72. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/relations/seeds/helpers.sql +0 -0
  73. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/relations/table/clone.sql +0 -0
  74. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/relations/views/create.sql +0 -0
  75. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/any_value.sql +0 -0
  76. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/array_construct.sql +0 -0
  77. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/cast_bool_to_text.sql +0 -0
  78. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/concat.sql +0 -0
  79. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/date_trunc.sql +0 -0
  80. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/dateadd.sql +0 -0
  81. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/get_tables_by_pattern.sql +0 -0
  82. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/hash.sql +0 -0
  83. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/last_day.sql +0 -0
  84. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/length.sql +0 -0
  85. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/listagg.sql +0 -0
  86. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/position.sql +0 -0
  87. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/safe_cast.sql +0 -0
  88. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/split_part.sql +0 -0
  89. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/macros/utils/timestamps.sql +0 -0
  90. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt/include/sqlserver/profile_template.yml +0 -0
  91. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt_sqlserver.egg-info/dependency_links.txt +0 -0
  92. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt_sqlserver.egg-info/requires.txt +0 -0
  93. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/dbt_sqlserver.egg-info/top_level.txt +0 -0
  94. {dbt_sqlserver-1.12.0rc2 → dbt_sqlserver-1.12.0rc4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dbt-sqlserver
3
- Version: 1.12.0rc2
3
+ Version: 1.12.0rc4
4
4
  Summary: A Microsoft SQL Server adapter plugin for dbt
5
5
  Author: Mikael Ene, Anders Swanson, Sam Debruyn, Cor Zuurmond, Cody Scott
6
6
  License: MIT
@@ -40,8 +40,8 @@ Dynamic: license-file
40
40
 
41
41
  [dbt](https://www.getdbt.com) adapter for Microsoft SQL Server and Azure SQL services.
42
42
 
43
- The adapter supports dbt-core 1.11 or newer and follows the same versioning scheme.
44
- E.g. version 1.11.x of the adapter is compatible with dbt-core 1.11.x.
43
+ The adapter supports dbt-core 1.12 or newer and follows the same versioning scheme.
44
+ E.g. version 1.12.x of the adapter is compatible with dbt-core 1.12.x.
45
45
 
46
46
  ## Supported Python versions
47
47
 
@@ -53,6 +53,7 @@ The adapter is tested against:
53
53
  | 3.11 | Officially supported |
54
54
  | 3.12 | Officially supported |
55
55
  | 3.13 | Officially supported |
56
+ | 3.14 | Officially supported |
56
57
 
57
58
  ## Supported SQL Server versions
58
59
 
@@ -175,23 +176,23 @@ See [the changelog](CHANGELOG.md)
175
176
 
176
177
  ### `dbt_sqlserver_use_default_schema_concat`
177
178
 
178
- *(default: `false`)* Controls schema name generation when a [custom schema](https://docs.getdbt.com/docs/build/custom-schemas) is set on a model.
179
+ *(default: `true`)* Controls schema name generation when a [custom schema](https://docs.getdbt.com/docs/build/custom-schemas) is set on a model.
179
180
 
180
181
  | Flag value | `custom_schema_name` | Result |
181
182
  |---|---|---|
182
- | `false` (default, legacy) | *(none)* | `target.schema` |
183
- | `false` (default, legacy) | `"reporting"` | `reporting` |
184
- | `true` (dbt-core standard) | *(none)* | `target.schema` |
185
- | `true` (dbt-core standard) | `"reporting"` | `target.schema_reporting` |
183
+ | `true` (default, dbt-core standard) | *(none)* | `target.schema` |
184
+ | `true` (default, dbt-core standard) | `"reporting"` | `target.schema_reporting` |
185
+ | `false` (legacy, deprecated) | *(none)* | `target.schema` |
186
+ | `false` (legacy, deprecated) | `"reporting"` | `reporting` |
186
187
 
187
- When `false` (the default), the adapter uses its legacy behaviour: `custom_schema_name` is used **as-is** without being prefixed by `target.schema`.
188
- When `true`, the adapter delegates to dbt-core's `default__generate_schema_name`, which concatenates `target.schema` + `_` + `custom_schema_name`.
188
+ When `true` (the default), the adapter delegates to dbt-core's `default__generate_schema_name`, which concatenates `target.schema` + `_` + `custom_schema_name`. This also matches the behavior dbt-core v2 (Fusion) ships unconditionally.
189
+ When `false`, the adapter uses its legacy behaviour: `custom_schema_name` is used **as-is** without being prefixed by `target.schema`. This legacy behavior is deprecated and will be removed in a future release.
189
190
 
190
191
  **Example usage in `dbt_project.yml`:**
191
192
 
192
193
  ```yaml
193
194
  flags:
194
- dbt_sqlserver_use_default_schema_concat: true # Enable standard schema concatenation
195
+ dbt_sqlserver_use_default_schema_concat: false # Opt back into the deprecated legacy behavior
195
196
  ```
196
197
 
197
198
  The same setting is also honoured via `vars:` for backwards compatibility; the behavior flag under `flags:` takes precedence when both are set.
@@ -214,21 +215,45 @@ The same setting is also honoured via `vars:` for backwards compatibility; the b
214
215
 
215
216
  Safe expansions are further gated by `column_type_expansion_max_rows` (default 1,000,000 rows) to avoid long-running operations on large tables.
216
217
 
218
+ ### `pre_hook_transaction_scope`
219
+
220
+ _(default: `load`)_ Where a `table`, `incremental` or `snapshot` build resolves its schema
221
+ (the tmp view and the empty `CREATE`) relative to its in-transaction pre-hooks.
222
+ `load` stages it before them, so the new table's `Sch-M` lock is released in an
223
+ instant and the load blocks no metadata reader in other sessions; a
224
+ `transaction: true` pre-hook still rolls back with a failed load, except on the
225
+ two paths that commit a full-refresh marker before the load
226
+ (`full_refresh_build: prebuilt`, and an incremental `--full-refresh` of an
227
+ existing table), where neither scope can roll it back. `build` stages
228
+ it inside the hook's transaction, for the one case `load` cannot serve: a
229
+ `transaction: true` pre-hook that creates an object the model reads. See
230
+ [docs/transaction_scope.md](docs/transaction_scope.md) for the full flow and
231
+ the post-hook ordering change.
232
+
217
233
  ### `dbt_sqlserver_use_dbt_transactions`
218
234
 
219
- _(default: `false`)_ When enabled, makes dbt's transaction hooks real at the SQL Server level by emitting `BEGIN TRANSACTION` / `COMMIT TRANSACTION` through the adapter's `add_begin_query` and `add_commit_query` methods.
235
+ _(default: `true`)_ Makes dbt's transaction hooks real at the SQL Server level by emitting `BEGIN TRANSACTION` / `COMMIT TRANSACTION` through the adapter's `add_begin_query` and `add_commit_query` methods.
220
236
 
221
- The default is `false`, preserving existing behavior where `begin`/`commit` hooks are logical no-ops and the ODBC driver auto-commits each statement. When `dbt_sqlserver_use_dbt_transactions: true`, the adapter emits real T-SQL transaction statements, and rollback uses `IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION`.
237
+ The default is `true`, so dbt-managed transaction hooks emit real T-SQL transaction statements and rollback uses `IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION`. Set it to `false` to opt back into the deprecated legacy behavior where `begin`/`commit` hooks are logical no-ops and the driver auto-commits each statement.
222
238
 
223
239
  The driver connection remains in autocommit mode (`autocommit=true`) in both modes.
224
240
 
225
- This mode is opt-in and should be tested carefully with project-specific materializations and hooks.
241
+ This is now the default and should be tested carefully with project-specific materializations and hooks. Projects that depend on autocommit-only behavior should set the flag to `false` during migration.
226
242
 
227
243
  ```yaml
228
244
  # dbt_project.yml
229
245
  flags:
230
246
  dbt_sqlserver_enable_safe_type_expansion: true
231
- dbt_sqlserver_use_dbt_transactions: true # <-- opt-in; default is false
247
+ dbt_sqlserver_use_dbt_transactions: true # default
248
+ ```
249
+
250
+ ### `dbt_sqlserver_use_native_string_types`
251
+
252
+ *(default: `true`)* Controls the SQL Server-native mappings used for dbt string types. With the default enabled, `STRING` maps to `VARCHAR(MAX)`, `NCHAR` maps to `NCHAR(1)`, and `NVARCHAR` maps to `NVARCHAR(4000)`. Set it to `false` to opt back into the deprecated legacy mappings: `STRING` and `NVARCHAR` map to `VARCHAR(8000)`, while `NCHAR` maps to `CHAR(1)`.
253
+
254
+ ```yaml
255
+ flags:
256
+ dbt_sqlserver_use_native_string_types: false # deprecated legacy behavior
232
257
  ```
233
258
 
234
259
  ### `xact_abort`
@@ -295,6 +320,108 @@ You can also set it per model:
295
320
  {{ config(materialized="table", as_columnstore=false) }}
296
321
  ```
297
322
 
323
+ With `table_refresh_method: dml`, a schema change makes the refresh fall back to a rename-swap. On that run — and only that run — the scratch table is rebuilt the way this adapter builds every other table, so it carries the model's columnstore index, and under an enforced contract its `NOT NULL`s and inline constraints, into the swap. That run therefore executes the model's SQL twice — once for the `SELECT … INTO` that probes for the schema change, once for the rebuild — and builds the columnstore index once. Steady-state refreshes are unaffected and keep the single `SELECT … INTO`. A table that lost its columnstore index to this bug before you upgraded is not repaired automatically: its schema still matches, so it stays on the cheap path. To rebuild it, temporarily set `full_refresh_build: prebuilt` and run with `--full-refresh`.
324
+
325
+ ### Constraints
326
+
327
+ Constraints declared in a model's yaml are applied when — and only when — the model's [contract](https://docs.getdbt.com/reference/resource-configs/contract) is enforced, which is what every dbt adapter does and keeps their cost opt-in. (dbt-core does not raise if you declare constraints with the contract off — they are simply never emitted.) `not_null`, `check`, `unique`, `primary_key` and `foreign_key` are all supported.
328
+
329
+ **Where a constraint lands depends on whether you name it.**
330
+
331
+ An unnamed constraint is rendered inline in the `CREATE TABLE` column list and SQL Server names it (`PK__my_model__3213E83F…`). It is validated as the table is built, so a violation fails before the new table is swapped in and the previous one is left untouched.
332
+
333
+ A *model-level* constraint carrying `name:` is applied by `ALTER TABLE … ADD CONSTRAINT` after the build swaps the new table into place and drops the old one. SQL Server scopes constraint names per schema, and a table is built alongside the one it replaces, so that is the first moment the name is free to reuse — naming a constraint inline would collide with the outgoing table (`Msg 2714`) on every rebuild after the first. The trade-off is that this runs after the model has committed: if the data violates the constraint, the model fails with the table already in place but unconstrained, and — as with any failure this late in a build — `post_hook`s declared with `transaction: false` do not run.
334
+
335
+ Name a constraint when you want it stable across environments (schema-comparison tools report the generated names as differences) or need to reference it later. A `name:` on a *column-level* constraint is ignored with a warning — declare it under the model's `constraints:` key instead.
336
+
337
+ The full set of rules, with the test that verifies each, is in [docs/constraints.md](docs/constraints.md).
338
+
339
+ ```yaml
340
+ models:
341
+ - name: fact_sales
342
+ config:
343
+ contract:
344
+ enforced: true
345
+ constraints:
346
+ # named: applied by ALTER TABLE after the swap
347
+ - type: primary_key
348
+ name: PK_fact_sales
349
+ columns: [sale_id]
350
+ - type: foreign_key
351
+ name: FK_fact_sales_customer
352
+ columns: [customer_id]
353
+ to: ref('dim_customer')
354
+ to_columns: [customer_id]
355
+ # unnamed: rendered into the CREATE TABLE
356
+ - type: check
357
+ expression: amount >= 0
358
+ columns:
359
+ - name: sale_id
360
+ data_type: int
361
+ constraints:
362
+ - type: not_null
363
+ - name: customer_id
364
+ data_type: int
365
+ constraints:
366
+ - type: not_null
367
+ - name: amount
368
+ data_type: decimal(18,2)
369
+ ```
370
+
371
+ #### Clustering
372
+
373
+ `primary_key` and `unique` are emitted as `NONCLUSTERED` by default so they can coexist with the clustered columnstore index built for [`as_columnstore`](#as_columnstore). Use dbt's own `expression` field to ask for something else:
374
+
375
+ ```yaml
376
+ constraints:
377
+ - type: primary_key
378
+ name: PK_fact_sales
379
+ columns: [sale_id]
380
+ expression: clustered # requires as_columnstore: false
381
+ ```
382
+
383
+ `clustered` and `nonclustered` are the only values understood here. `expression` is free text that dbt splices between the keyword and the column list, which is the one place T-SQL accepts nothing else — index options such as `with (fillfactor = 90)` belong on a separate index, not on the constraint.
384
+
385
+ #### Foreign keys
386
+
387
+ Two things are worth knowing before adding them:
388
+
389
+ - **They are not free at build time.** Every load is validated against them; add them where you want the guarantee, not everywhere the relationship exists.
390
+ - **A foreign key pointing at a model blocks that model's rebuild.** The build renames the outgoing table to a backup and drops it, but the child's foreign key follows the renamed object, so the drop fails with `Msg 3726`. SQL Server has no `DROP TABLE … CASCADE`.
391
+
392
+ The adapter ships a macro for exactly this, meant as a `pre_hook` on the **referenced** (parent) model:
393
+
394
+ ```sql
395
+ {{ config(pre_hook="{{ drop_fk_constraints() }}") }}
396
+ ```
397
+
398
+ It drops the foreign keys in both directions — the inbound ones other tables hold against this model, and this model's own outbound ones — so the rebuild's backup drop succeeds. The trade-off is explicit and worth stating: **the child's foreign key does not exist between the parent's rebuild and the child's next build.** (dbt-postgres makes the same trade silently, by issuing every `drop table` with `cascade`.)
399
+
400
+ Add the hook *before* the first rebuild. Once a rebuild has failed with `Msg 3726`, the new table is already in place but without its own named constraints — those are applied after the backup drop that failed — and the child's key now points at `<model>__dbt_backup`, because a foreign key follows the object, not the name. A plain `dbt run` does not recover: the parent trips over the same backup and the child is skipped behind it. Rebuild the child alone — that run fails too, since the parent has no key to reference, but its swap drops the old child and the stale key with it — and a plain `dbt run` then rebuilds both in order. Dropping the child's key by hand (`ALTER TABLE <child> DROP CONSTRAINT <name>`) does the same.
401
+
402
+ `table_refresh_method: dml` is *not* a workaround. Its steady-state refresh issues `DELETE FROM <parent>`, which fails with `Msg 547` as soon as the child holds referencing rows, and its schema-change path falls back to the same rename-swap, hitting `Msg 3726` anyway.
403
+
404
+ - **SQL Server has no cross-database foreign keys.** `to: ref(...)` resolves to a fully qualified relation, database included, which SQL Server accepts as long as it names the current database. A target in another database fails with `references invalid table`.
405
+
406
+ A foreign key also does not order the build on its own: add an explicit `-- depends_on: {{ ref('dim_customer') }}` to the child model so the parent is built first.
407
+
408
+ #### Changing a constraint after the first build
409
+
410
+ Named constraints are applied by an `ALTER TABLE` whose `ADD` is guarded on the name already being present on the table, so:
411
+
412
+ - **Adding** a constraint to a model that already exists lands on its next run — no `--full-refresh` needed, on `table` and `incremental` alike.
413
+ - **Changing** an existing constraint's definition while keeping its name is *not* detected: a constraint name, unlike a `dbt_idx_` index name, says nothing about what the constraint does. Run `--full-refresh` to apply the new definition; that rebuilds the table, so the constraint is created fresh. (A `table` model rebuilds on every run and needs nothing special.)
414
+ - **Renaming** a constraint on a table that persists across runs adds the new name beside the old one. For a `check`, `unique` or `foreign_key` that means a duplicate; for a `primary_key` the run fails with `Msg 1779` (*table already has a primary key defined on it*) until the old one is dropped. Rename with `--full-refresh`.
415
+ - **Removing** a constraint from the yaml does not drop it from the database. Drop it yourself, or `--full-refresh`.
416
+
417
+ Every bullet above is about *named* constraints. Unnamed ones ride the `CREATE TABLE`, so they follow the table and change only when the table is rebuilt — on a materialization whose table persists across runs (`incremental` in its steady state, `table_refresh_method: dml`), adding an unnamed constraint to an existing model does nothing until a `--full-refresh`, and the run still reports success. Name it, or full-refresh.
418
+
419
+ #### Build-shape notes
420
+
421
+ An *unnamed* `primary_key` or `unique` constraint on a column that also carries a [data mask](#dynamic-data-masking-masked_with--masks) is rejected by the build. The constraint rides the `CREATE TABLE`, so its index already exists by the time `apply_masks` runs, and the adapter refuses to mask any column that an index has as a key: *is configured for masking but is also an index key column*. Declare that constraint at the model level **with a `name:`** instead — named constraints are applied by `ALTER TABLE` after the masks are in place, which the adapter allows.
422
+
423
+ A contract-enforced model is always loaded as `CREATE TABLE` followed by `INSERT … WITH (TABLOCK)`, on every build path. A `primary_key` or `unique` constraint — named or not — puts a nonclustered index on that table *before* the load, so the index is maintained row by row while the rows go in, and that part of the load is fully logged where an index-free heap would have been minimally logged. The cost lands on every rebuild of the model; it is most visible with `full_refresh_build: prebuilt`, whose point is a cheap bulk load. If a model's load time matters, weigh the key constraints against it; `check`, `not_null` and `foreign_key` do not create indexes and do not carry this cost.
424
+
298
425
  ### Dynamic Data Masking (`masked_with` / `masks`)
299
426
 
300
427
  The adapter can apply SQL Server [Dynamic Data Masking](https://learn.microsoft.com/en-us/sql/relational-databases/security/dynamic-data-masking) (DDM) to columns as part of the materialization, so masks are re-applied on every build and survive dbt's drop-and-recreate on a full refresh. A principal granted `SELECT` but not `UNMASK` then sees masked values instead of real data (dbt's own build principal, being `db_owner`, keeps `UNMASK` and reads real data). Requires **SQL Server 2016+**.
@@ -342,6 +469,23 @@ Behaviour:
342
469
 
343
470
  **Version notes.** All masking DDL the adapter emits (`ADD MASKED`, `MASKED WITH`, `DROP MASKED`) and the functions `default()`, `email()`, `random(a,b)` and `partial(...)` work on 2016+. The `datetime()` partial-date function and granular column/schema/table-scoped `UNMASK` are SQL Server 2022+ only; the adapter never emits them, but mask-function strings are passed through verbatim, so using a 2022-only function on an older server will be rejected by SQL Server.
344
471
 
472
+ ### Object-level DENY (`denies`)
473
+
474
+ In SQL Server, an object-level `DENY` is the only way to carve an exception out of a schema-level `GRANT` — grant a principal `SELECT` on a whole schema, then `DENY SELECT` on the individual PII-bearing models. But a `DENY` is stored against the object's `object_id`, so dbt destroys it every time it drops and recreates the relation (which is *every* run for a view). The schema grant survives; its exceptions silently evaporate, leaving a **fail-open** posture that no run reports.
475
+
476
+ The `denies` config re-applies object-level DENYs after each build, diffed against `sys.database_permissions`, the same way `masks` re-applies Dynamic Data Masking. Its shape mirrors `grants` — a `{privilege: [principals]}` map — and it is settable in the in-file `{{ config() }}`, the model's `.yml` `config:` block, or a directory-wide default in `dbt_project.yml` (merging key-wise across those levels, like `masks`):
477
+
478
+ ```sql
479
+ {{ config(denies={'select': ['Restricted_Read_Only']}) }}
480
+ ```
481
+
482
+ - **Survives rebuilds:** the DENY is present after `dbt run` and *still present after the next run*, for `table`, `view`, `incremental` (append and full-refresh) and `snapshot`. Unlike masks, this **includes views** — a view is a valid securable and is recreated on every run, which is where a DENY is lost most often.
483
+ - **Reconciled:** the adapter diffs the config against the live DENY state and emits only what changed — `DENY` for configured-not-present, `REVOKE` for present-not-configured (a `REVOKE` removes a `DENY`). Removing a principal from `denies` revokes its DENY on the next run; a converged, persisted relation issues no DDL.
484
+ - **Scope:** object-level DENY for the table privileges `select`, `insert`, `update`, `delete`, `references`. An unsupported privilege is warned and skipped (the warning surfaces the likely typo without taking down the build). Column-, database- and server-scoped permissions are out of scope (the latter survive a rebuild already).
485
+ - **Absent principal:** a `DENY` to a non-existent database principal is warned-and-skipped (the run still succeeds), so one config runs unchanged across dev, CI and prod where the principal set differs.
486
+ - **Ordering vs `grants`:** grants are applied first, then denies, so the final state is unambiguous. A `(privilege, principal)` pair appearing in both `grants` and `denies` is warned as a likely mistake (a DENY overrides a GRANT in SQL Server).
487
+ - **Other adapters:** `denies` is a no-op on non-SQL-Server adapters, not an error. This is SQL-Server-specific by design — `DENY` is absent from the SQL standard and does not port.
488
+
345
489
  ## Contributing
346
490
 
347
491
  [![Unit tests](https://github.com/dbt-msft/dbt-sqlserver/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/dbt-msft/dbt-sqlserver/actions/workflows/unit-tests.yml)