Skip to content

Commit

Permalink
feat: Use sdk in table constraint resource (#2466)
Browse files Browse the repository at this point in the history
- Removed old implementation.
- Removed not working `NOT NULL` from the resource definition with
proper note.
  • Loading branch information
sfc-gh-asawicki authored Feb 7, 2024
1 parent 682e28c commit d685603
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 343 deletions.
10 changes: 10 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ This document is meant to help you migrate your Terraform config to the new newe
describe deprecations or breaking changes and help you to change your configuration to keep the same (or similar) behavior
across different versions.

## v0.85.0 ➞ v0.86.0
### snowflake_table_constraint resource changes

#### *(behavior change)* NOT NULL removed from possible types
The `type` of the constraint was limited back to `UNIQUE`, `PRIMARY KEY`, and `FOREIGN KEY`.
The reason for that is, that syntax for Out-of-Line constraint ([docs](https://docs.snowflake.com/en/sql-reference/sql/create-table-constraint#out-of-line-unique-primary-foreign-key)) does not contain `NOT NULL`.
It is noted as a behavior change but in some way it is not; with the previous implementation it did not work at all with `type` set to `NOT NULL` because the generated statement was not a valid Snowflake statement.

We will consider adding `NOT NULL` back because it can be set by `ALTER COLUMN columnX SET NOT NULL`, but first we want to revisit the whole resource design.

## vX.XX.X -> v0.85.0

### Migration from old (grant) resources to new ones
Expand Down
14 changes: 7 additions & 7 deletions docs/resources/table_constraint.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ resource "snowflake_table" "fk_t" {
resource "snowflake_table_constraint" "primary_key" {
name = "myconstraint"
type = "PRIMARY KEY"
table_id = snowflake_table.t.id
table_id = snowflake_table.t.qualified_name
columns = ["col1"]
comment = "hello world"
}
resource "snowflake_table_constraint" "foreign_key" {
name = "myconstraintfk"
type = "FOREIGN KEY"
table_id = snowflake_table.t.id
table_id = snowflake_table.t.qualified_name
columns = ["col2"]
foreign_key_properties {
references {
table_id = snowflake_table.fk_t.id
table_id = snowflake_table.fk_t.qualified_name
columns = ["fk_col1"]
}
}
Expand All @@ -92,7 +92,7 @@ resource "snowflake_table_constraint" "foreign_key" {
resource "snowflake_table_constraint" "unique" {
name = "unique"
type = "UNIQUE"
table_id = snowflake_table.t.id
table_id = snowflake_table.t.qualified_name
columns = ["col3"]
comment = "hello unique"
}
Expand All @@ -105,12 +105,12 @@ resource "snowflake_table_constraint" "unique" {

- `columns` (List of String) Columns to use in constraint key
- `name` (String) Name of constraint
- `table_id` (String) Idenfifier for table to create constraint on. Must be of the form Note: format must follow: "<db_name>"."<schema_name>"."<table_name>" or "<db_name>.<schema_name>.<table_name>" or "<db_name>|<schema_name>.<table_name>" (snowflake_table.my_table.id)
- `type` (String) Type of constraint, one of 'UNIQUE', 'PRIMARY KEY', 'FOREIGN KEY', or 'NOT NULL'
- `table_id` (String) Identifier for table to create constraint on. Format must follow: "\"<db_name>\".\"<schema_name>\".\"<table_name>\"" or "<db_name>.<schema_name>.<table_name>" (snowflake_table.my_table.id)
- `type` (String) Type of constraint, one of 'UNIQUE', 'PRIMARY KEY', or 'FOREIGN KEY'

### Optional

- `comment` (String) Comment for the table constraint
- `comment` (String, Deprecated) Comment for the table constraint
- `deferrable` (Boolean) Whether the constraint is deferrable
- `enable` (Boolean) Specifies whether the constraint is enabled or disabled. These properties are provided for compatibility with Oracle.
- `enforced` (Boolean) Whether the constraint is enforced
Expand Down
8 changes: 4 additions & 4 deletions examples/resources/snowflake_table_constraint/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ resource "snowflake_table" "fk_t" {
resource "snowflake_table_constraint" "primary_key" {
name = "myconstraint"
type = "PRIMARY KEY"
table_id = snowflake_table.t.id
table_id = snowflake_table.t.qualified_name
columns = ["col1"]
comment = "hello world"
}

resource "snowflake_table_constraint" "foreign_key" {
name = "myconstraintfk"
type = "FOREIGN KEY"
table_id = snowflake_table.t.id
table_id = snowflake_table.t.qualified_name
columns = ["col2"]
foreign_key_properties {
references {
table_id = snowflake_table.fk_t.id
table_id = snowflake_table.fk_t.qualified_name
columns = ["fk_col1"]
}
}
Expand All @@ -77,7 +77,7 @@ resource "snowflake_table_constraint" "foreign_key" {
resource "snowflake_table_constraint" "unique" {
name = "unique"
type = "UNIQUE"
table_id = snowflake_table.t.id
table_id = snowflake_table.t.qualified_name
columns = ["col3"]
comment = "hello unique"
}
9 changes: 0 additions & 9 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ func ListToSnowflakeString(list []string) string {
return fmt.Sprintf("%v", strings.Join(list, ", "))
}

// IPListToString formats a list of IPs into a Snowflake-DDL friendly string, e.g. ('192.168.1.0', '192.168.1.100').
func IPListToSnowflakeString(ips []string) string {
for index, element := range ips {
ips[index] = fmt.Sprintf(`'%v'`, element)
}

return fmt.Sprintf("(%v)", strings.Join(ips, ", "))
}

// ListContentToString strips list elements of double quotes or brackets.
func ListContentToString(listString string) string {
re := regexp.MustCompile(`[\"\[\]]`)
Expand Down
Loading

0 comments on commit d685603

Please sign in to comment.