Skip to content

Commit

Permalink
fix: read binlog retention when null. Unique id
Browse files Browse the repository at this point in the history
  • Loading branch information
cemdorst committed Jan 10, 2023
1 parent 1fb4571 commit 7f6d2b1
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions mysql/resource_binlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package mysql

import (
"context"
"database/sql"
"fmt"
"log"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

Expand Down Expand Up @@ -44,7 +47,9 @@ func CreateBinLog(ctx context.Context, d *schema.ResourceData, meta interface{})
return diag.Errorf("failed running SQL to set binlog retention period: %v", err)
}

d.SetId(d.Get("retention_period").(string))
id := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)

d.SetId(id)

return ReadBinLog(ctx, d, meta)
}
Expand Down Expand Up @@ -86,15 +91,19 @@ func ReadBinLog(ctx context.Context, d *schema.ResourceData, meta interface{}) d
return diag.Errorf("Error verifying binlog retention period: %s", err)
}

results := make(map[string]string)
results := make(map[string]interface{})
for rows.Next() {
var name, value, description string
var name, description string
var value sql.NullString

if err := rows.Scan(&name, &value, &description); err != nil {
return diag.Errorf("failed reading binlog retention period: %v", err)
}
results[name] = value
}
if results["binlog retention hours"] == "NULL" {
results["binlog retention hours"] = "0"
}

d.Set("retention_period", results["binlog retention hours"])

Expand All @@ -121,9 +130,14 @@ func DeleteBinLog(ctx context.Context, d *schema.ResourceData, meta interface{})

func binlogConfigSQL(d *schema.ResourceData) string {
retention_period := d.Get("retention_period").(string)

return fmt.Sprintf(
"call mysql.rds_set_configuration('binlog retention hours', %s)",
retention_period,
)
if retention_period == "0" {
return fmt.Sprintf(
"call mysql.rds_set_configuration('binlog retention hours', %s)",
"NULL")
} else {
return fmt.Sprintf(
"call mysql.rds_set_configuration('binlog retention hours', %s)",
retention_period,
)
}
}

0 comments on commit 7f6d2b1

Please sign in to comment.