Skip to content

Commit

Permalink
[WIP] Allow schema rename
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Jan 30, 2024
1 parent a4212a0 commit b469950
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/materialize/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion pkg/resources/resource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func SchemaNameSchema(resource string, required bool) *schema.Schema {
Description: fmt.Sprintf("The identifier for the %s schema. Defaults to `public`.", resource),
Required: required,
Optional: !required,
ForceNew: true,
ForceNew: false,
Default: defaultSchema,
}
}
Expand Down

0 comments on commit b469950

Please sign in to comment.