diff --git a/pkg/resources/schema.go b/pkg/resources/schema.go index 3a0dea423b..d355caa0c2 100644 --- a/pkg/resources/schema.go +++ b/pkg/resources/schema.go @@ -25,7 +25,6 @@ var schemaSchema = map[string]*schema.Schema{ Type: schema.TypeString, Required: true, Description: "Specifies the identifier for the schema; must be unique for the database in which the schema is created.", - ForceNew: true, }, "database": { Type: schema.TypeString, @@ -272,6 +271,16 @@ func UpdateSchema(d *schema.ResourceData, meta interface{}) error { builder := snowflake.Schema(schema).WithDB(dbName) db := meta.(*sql.DB) + if d.HasChange("name") { + name := d.Get("name") + q := builder.Rename(name.(string)) + err := snowflake.Exec(db, q) + if err != nil { + return errors.Wrapf(err, "error updating schema name on %v", d.Id()) + } + d.SetId(fmt.Sprintf("%v|%v", dbName, name.(string))) + } + if d.HasChange("comment") { comment := d.Get("comment") q := builder.ChangeComment(comment.(string)) diff --git a/pkg/resources/schema_acceptance_test.go b/pkg/resources/schema_acceptance_test.go index ad70cba6ff..17bd008568 100644 --- a/pkg/resources/schema_acceptance_test.go +++ b/pkg/resources/schema_acceptance_test.go @@ -10,16 +10,17 @@ import ( ) func TestAcc_Schema(t *testing.T) { - accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) resource.ParallelTest(t, resource.TestCase{ Providers: providers(), Steps: []resource.TestStep{ { - Config: schemaConfig(accName), + Config: schemaConfig(databaseName, schemaName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("snowflake_schema.test", "name", accName), - resource.TestCheckResourceAttr("snowflake_schema.test", "database", accName), + resource.TestCheckResourceAttr("snowflake_schema.test", "name", schemaName), + resource.TestCheckResourceAttr("snowflake_schema.test", "database", databaseName), resource.TestCheckResourceAttr("snowflake_schema.test", "comment", "Terraform acceptance test"), checkBool("snowflake_schema.test", "is_transient", false), // this is from user_acceptance_test.go checkBool("snowflake_schema.test", "is_managed", false), @@ -29,7 +30,39 @@ func TestAcc_Schema(t *testing.T) { }) } -func schemaConfig(n string) string { +func TestAcc_SchemaRename(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + oldSchemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + newSchemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + + resource.ParallelTest(t, resource.TestCase{ + Providers: providers(), + Steps: []resource.TestStep{ + { + Config: schemaConfig(databaseName, oldSchemaName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_schema.test", "name", oldSchemaName), + resource.TestCheckResourceAttr("snowflake_schema.test", "database", databaseName), + resource.TestCheckResourceAttr("snowflake_schema.test", "comment", "Terraform acceptance test"), + checkBool("snowflake_schema.test", "is_transient", false), // this is from user_acceptance_test.go + checkBool("snowflake_schema.test", "is_managed", false), + ), + }, + { + Config: schemaConfig(databaseName, newSchemaName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_schema.test", "name", newSchemaName), + resource.TestCheckResourceAttr("snowflake_schema.test", "database", databaseName), + resource.TestCheckResourceAttr("snowflake_schema.test", "comment", "Terraform acceptance test"), + checkBool("snowflake_schema.test", "is_transient", false), // this is from user_acceptance_test.go + checkBool("snowflake_schema.test", "is_managed", false), + ), + }, + }, + }) +} + +func schemaConfig(databaseName string, schemaName string) string { return fmt.Sprintf(` resource "snowflake_database" "test" { name = "%v" @@ -41,5 +74,5 @@ resource "snowflake_schema" "test" { database = snowflake_database.test.name comment = "Terraform acceptance test" } -`, n, n) +`, databaseName, schemaName) } diff --git a/pkg/snowflake/schema.go b/pkg/snowflake/schema.go index a4e47dc982..bad87d0c81 100644 --- a/pkg/snowflake/schema.go +++ b/pkg/snowflake/schema.go @@ -133,13 +133,17 @@ func (sb *SchemaBuilder) Create() string { // Rename returns the SQL query that will rename the schema. func (sb *SchemaBuilder) Rename(newName string) string { - return fmt.Sprintf(`ALTER SCHEMA %v RENAME TO "%v"`, sb.QualifiedName(), newName) + oldName := sb.QualifiedName() + sb.name = newName + return fmt.Sprintf(`ALTER SCHEMA %v RENAME TO %v`, oldName, sb.QualifiedName()) } // Swap returns the SQL query that Swaps all objects (tables, views, etc.) and // metadata, including identifiers, between the two specified schemas. func (sb *SchemaBuilder) Swap(targetSchema string) string { - return fmt.Sprintf(`ALTER SCHEMA %v SWAP WITH "%v"`, sb.QualifiedName(), targetSchema) + sourceSchema := sb.QualifiedName() + sb.name = targetSchema + return fmt.Sprintf(`ALTER SCHEMA %v SWAP WITH %v`, sourceSchema, sb.QualifiedName()) } // ChangeComment returns the SQL query that will update the comment on the schema.