Skip to content

Commit

Permalink
Merge pull request #480 from ms32035/master
Browse files Browse the repository at this point in the history
fix constraints support
  • Loading branch information
schlich authored Feb 27, 2024
2 parents 80300df + 9f8c592 commit 9b1aabd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions dbt/adapters/sqlserver/sql_server_adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# https://github.com/microsoft/dbt-fabric/blob/main/dbt/adapters/fabric/fabric_adapter.py
from typing import Optional

import dbt.exceptions
from dbt.adapters.fabric import FabricAdapter
from dbt.contracts.graph.nodes import ConstraintType, ModelLevelConstraint

from dbt.adapters.sqlserver.sql_server_column import SQLServerColumn
from dbt.adapters.sqlserver.sql_server_configs import SQLServerConfigs
Expand Down Expand Up @@ -50,6 +54,33 @@ class SQLServerAdapter(FabricAdapter):
# return columns
# endregion

@classmethod
def render_model_constraint(cls, constraint: ModelLevelConstraint) -> Optional[str]:
constraint_prefix = "add constraint "
column_list = ", ".join(constraint.columns)

if constraint.name is None:
raise dbt.exceptions.DbtDatabaseError(
"Constraint name cannot be empty. Provide constraint name - column "
+ column_list
+ " and run the project again."
)

if constraint.type == ConstraintType.unique:
return constraint_prefix + f"{constraint.name} unique nonclustered({column_list})"
elif constraint.type == ConstraintType.primary_key:
return constraint_prefix + f"{constraint.name} primary key nonclustered({column_list})"
elif constraint.type == ConstraintType.foreign_key and constraint.expression:
return (
constraint_prefix
+ f"{constraint.name} foreign key({column_list}) references "
+ constraint.expression
)
elif constraint.type == ConstraintType.custom and constraint.expression:
return f"{constraint_prefix}{constraint.expression}"
else:
return None

@classmethod
def date_function(cls):
return "getdate()"

0 comments on commit 9b1aabd

Please sign in to comment.