From fd371717c08554a59df62e409415148b1db624f2 Mon Sep 17 00:00:00 2001 From: Bobby Iliev Date: Wed, 21 Feb 2024 09:48:53 +0200 Subject: [PATCH] Allow schema rename (#450) * [WIP] Allow schema rename * Revert forcenew to true --- pkg/materialize/schema.go | 6 ++++++ pkg/resources/resource_schema.go | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/materialize/schema.go b/pkg/materialize/schema.go index 85c066d6..e29d4bd5 100644 --- a/pkg/materialize/schema.go +++ b/pkg/materialize/schema.go @@ -32,6 +32,12 @@ func (b *SchemaBuilder) Create() error { return b.ddl.exec(q) } +func (b *SchemaBuilder) Rename(newName string) error { + old := b.QualifiedName() + new := QualifiedName(newName) + return b.ddl.rename(old, new) +} + func (b *SchemaBuilder) Drop() error { qn := b.QualifiedName() return b.ddl.drop(qn) diff --git a/pkg/resources/resource_schema.go b/pkg/resources/resource_schema.go index 3edc7442..43896bf2 100644 --- a/pkg/resources/resource_schema.go +++ b/pkg/resources/resource_schema.go @@ -13,7 +13,7 @@ import ( ) var schemaSchema = map[string]*schema.Schema{ - "name": ObjectNameSchema("schema", true, true), + "name": ObjectNameSchema("schema", true, false), "database_name": DatabaseNameSchema("schema", false), "qualified_sql_name": QualifiedNameSchema("schema"), "comment": CommentSchema(false), @@ -153,6 +153,15 @@ func schemaUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) } } + if d.HasChange("name") { + oldName, newName := d.GetChange("name") + o := materialize.MaterializeObject{ObjectType: "SCHEMA", Name: oldName.(string), DatabaseName: databaseName} + b := materialize.NewSchemaBuilder(metaDb, o) + if err := b.Rename(newName.(string)); err != nil { + return diag.FromErr(err) + } + } + return schemaRead(ctx, d, meta) }