|
1 | 1 | # https://github.com/microsoft/dbt-fabric/blob/main/dbt/adapters/fabric/fabric_adapter.py
|
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import dbt.exceptions |
2 | 5 | from dbt.adapters.fabric import FabricAdapter
|
| 6 | +from dbt.contracts.graph.nodes import ConstraintType, ModelLevelConstraint |
3 | 7 |
|
4 | 8 | from dbt.adapters.sqlserver.sql_server_column import SQLServerColumn
|
5 | 9 | from dbt.adapters.sqlserver.sql_server_configs import SQLServerConfigs
|
@@ -50,6 +54,33 @@ class SQLServerAdapter(FabricAdapter):
|
50 | 54 | # return columns
|
51 | 55 | # endregion
|
52 | 56 |
|
| 57 | + @classmethod |
| 58 | + def render_model_constraint(cls, constraint: ModelLevelConstraint) -> Optional[str]: |
| 59 | + constraint_prefix = "add constraint " |
| 60 | + column_list = ", ".join(constraint.columns) |
| 61 | + |
| 62 | + if constraint.name is None: |
| 63 | + raise dbt.exceptions.DbtDatabaseError( |
| 64 | + "Constraint name cannot be empty. Provide constraint name - column " |
| 65 | + + column_list |
| 66 | + + " and run the project again." |
| 67 | + ) |
| 68 | + |
| 69 | + if constraint.type == ConstraintType.unique: |
| 70 | + return constraint_prefix + f"{constraint.name} unique nonclustered({column_list})" |
| 71 | + elif constraint.type == ConstraintType.primary_key: |
| 72 | + return constraint_prefix + f"{constraint.name} primary key nonclustered({column_list})" |
| 73 | + elif constraint.type == ConstraintType.foreign_key and constraint.expression: |
| 74 | + return ( |
| 75 | + constraint_prefix |
| 76 | + + f"{constraint.name} foreign key({column_list}) references " |
| 77 | + + constraint.expression |
| 78 | + ) |
| 79 | + elif constraint.type == ConstraintType.custom and constraint.expression: |
| 80 | + return f"{constraint_prefix}{constraint.expression}" |
| 81 | + else: |
| 82 | + return None |
| 83 | + |
53 | 84 | @classmethod
|
54 | 85 | def date_function(cls):
|
55 | 86 | return "getdate()"
|
0 commit comments