-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Add support for constraints in databricks_sql_table resource #4205
base: main
Are you sure you want to change the base?
[Feature] Add support for constraints in databricks_sql_table resource #4205
Conversation
there is a #4155 in progress - can you coordinate with that user to avoid duplications? |
oh, indeed! I see the other user decided on slightly different approach to configuration of the constraint. And seems that the syntax is not compliant with Databricks SQL, maybe that's why there are some test failures. I will ask in that PR if we can somehow cooperate :) otherwise, is there something I could do to proceed with the feature? |
seems the feature should be developed within this PR - I'm open to review and suggestions |
Could you add functionality for the "RELY" optimization of Primary Keys?
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would like the RELY optimization support for primary keys, please
Sure, optional configuration of a constraint with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @wiatrak2 for the PR, it looks good, I left some comments, can you please take a look?
catalog/resource_sql_table.go
Outdated
@@ -89,6 +99,9 @@ func (ti SqlTableInfo) CustomizeSchema(s *common.CustomizableSchema) *common.Cus | |||
s.SchemaPath("column", "type").SetCustomSuppressDiff(func(k, old, new string, d *schema.ResourceData) bool { | |||
return getColumnType(old) == getColumnType(new) | |||
}) | |||
s.SchemaPath("constraint", "type").SetCustomSuppressDiff(func(k, old, new string, d *schema.ResourceData) bool { | |||
return getColumnType(old) == getColumnType(new) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would work because we are essentially checking for case insensitivity and ignoring the mapping here:
if alias, ok := columnTypeAliases[caseInsensitiveColumnType]; ok {
return alias
}
We shouldn't use getColumnType here.
catalog/resource_sql_table.go
Outdated
@@ -242,6 +255,39 @@ func (ti *SqlTableInfo) serializeColumnInfos() string { | |||
return strings.Join(columnFragments[:], ", ") // id INT NOT NULL, name STRING, age INT | |||
} | |||
|
|||
func serializePrimaryKeyConstraint(constraint ConstraintInfo) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make this as part of the ConstraintInfo struct that we do for getWrappedConstraintName as it will only be used for each constraint info and internally uses ConstraintInfo. getWrappedConstraintName, getWrappedKeyColumnNames and Rely?
We do the same for SqlTableInfo. serializeColumnInfos, serializeColumnInfo
for i, constraint := range ti.Constraints { | ||
if constraint.Type == "PRIMARY KEY" { | ||
constraintFragments[i] = serializePrimaryKeyConstraint(constraint) | ||
} else if constraint.Type == "FOREIGN KEY" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we only support PRIMARY KEY
and FOREIGN KEY
, we should fail client side if some other value is used.
If integration tests don't run automatically, an authorized user can run them manually by following the instructions below: Trigger: Inputs:
Checks will be approved automatically on success. |
Hi @tanmay-db Thank you for your review. I adjusted the implementation to your comments:
Please let me know what you think about these recent changes |
@wiatrak2 there are CRUD APIs for PK/FK Table Constraints - https://docs.databricks.com/api/workspace/tableconstraints/create, does it make sense to have them as separate resources? wdyt @tanmay-db ? |
Changes
Hi!
I'd like to extend the
databricks_sql_table
resource with constraints feature.Within this pull request I extend the table's resource by allowing user to specify
PRIMARY KEY
andFOREIGN KEY
constraints. I haven't implemented theCHECK
constraint, as this one is more tricky and is not allowed during table creation - it can only be added later in the table's lifecycle. If the proposed changed are accepted, I can try to add the implementation forCHECK
, as well as cover other constraint options.I'm aware that the amount of implemented tests may not be satisfying. If this is the case - please let me know and guide my, what kind of tests should be added.
Tests
make test
run locallydocs/
folderinternal/acceptance