Skip to content

Commit 2265315

Browse files
authored
Merge pull request #138 from planetscale/seed-data
merging of #132
2 parents 1cbb134 + 16d27e4 commit 2265315

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

docs/resources/branch.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ resource "planetscale_branch" "example" {
3434
### Optional
3535

3636
- `production` (Boolean) Whether or not the branch is a production branch.
37+
- `seed_data` (String) Seed data using the Data Branching® feature. Valid value is `last_successful_backup`
3738

3839
### Read-Only
3940

internal/provider/branch_resource.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type branchResource struct {
3737

3838
// branchResourceModelV* and branchSchemaV* are defined in branch_resource_migration.go
3939

40-
func branchResourceFromClient(ctx context.Context, branch *planetscale.Branch, organization, database types.String, diags diag.Diagnostics) *branchResourceModelV1 {
40+
func branchResourceFromClient(ctx context.Context, branch *planetscale.Branch, organization, database types.String, seedData types.String, diags diag.Diagnostics) *branchResourceModelV1 {
4141
if branch == nil {
4242
return nil
4343
}
@@ -50,6 +50,7 @@ func branchResourceFromClient(ctx context.Context, branch *planetscale.Branch, o
5050
return &branchResourceModelV1{
5151
Organization: organization,
5252
Database: database,
53+
SeedData: seedData,
5354

5455
Actor: actor,
5556
Region: region,
@@ -108,6 +109,7 @@ func (r *branchResource) Create(ctx context.Context, req resource.CreateRequest,
108109
org := data.Organization
109110
database := data.Database
110111
name := data.Name
112+
seedData := data.SeedData
111113

112114
if org.IsNull() || org.IsUnknown() || org.ValueString() == "" {
113115
resp.Diagnostics.AddAttributeError(path.Root("organization"), "organization is required", "an organization must be provided and cannot be empty")
@@ -131,6 +133,7 @@ func (r *branchResource) Create(ctx context.Context, req resource.CreateRequest,
131133
createReq := planetscale.CreateBranchReq{
132134
Name: name.ValueString(),
133135
ParentBranch: *parentBranch,
136+
SeedData: seedData.ValueStringPointer(),
134137
}
135138
if !data.RestoredFromBranch.IsNull() && !data.RestoredFromBranch.IsUnknown() {
136139
var rfb restoredFromBranchResource
@@ -199,7 +202,7 @@ func (r *branchResource) Create(ctx context.Context, req resource.CreateRequest,
199202
branch = res.Branch
200203
}
201204

202-
data = branchResourceFromClient(ctx, &branch, data.Organization, data.Database, resp.Diagnostics)
205+
data = branchResourceFromClient(ctx, &branch, data.Organization, data.Database, data.SeedData, resp.Diagnostics)
203206
if resp.Diagnostics.HasError() {
204207
return
205208
}
@@ -244,7 +247,7 @@ func (r *branchResource) Read(ctx context.Context, req resource.ReadRequest, res
244247
return
245248
}
246249

247-
data = branchResourceFromClient(ctx, &res.Branch, data.Organization, data.Database, resp.Diagnostics)
250+
data = branchResourceFromClient(ctx, &res.Branch, data.Organization, data.Database, data.SeedData, resp.Diagnostics)
248251
if resp.Diagnostics.HasError() {
249252
return
250253
}
@@ -303,7 +306,7 @@ func (r *branchResource) Update(ctx context.Context, req resource.UpdateRequest,
303306
branch = res.Branch
304307
}
305308
}
306-
data = branchResourceFromClient(ctx, &branch, data.Organization, data.Database, resp.Diagnostics)
309+
data = branchResourceFromClient(ctx, &branch, data.Organization, data.Database, data.SeedData, resp.Diagnostics)
307310
if resp.Diagnostics.HasError() {
308311
return
309312
}

internal/provider/branch_resource_migration.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type branchResourceModelV1 struct {
3838
SchemaLastUpdatedAt types.String `tfsdk:"schema_last_updated_at"`
3939
ShardCount types.Float64 `tfsdk:"shard_count"`
4040
Sharded types.Bool `tfsdk:"sharded"`
41+
SeedData types.String `tfsdk:"seed_data"`
4142
UpdatedAt types.String `tfsdk:"updated_at"`
4243
}
4344

@@ -141,6 +142,13 @@ func branchSchemaV1() *schema.Schema {
141142
Description: "When the branch was last updated.",
142143
Computed: true,
143144
},
145+
"seed_data": schema.StringAttribute{
146+
Description: "Seed data using the Data Branching® feature. Valid value is `last_successful_backup`",
147+
Optional: true,
148+
PlanModifiers: []planmodifier.String{
149+
stringplanmodifier.RequiresReplace(),
150+
},
151+
},
144152
},
145153
}
146154
}
@@ -170,6 +178,7 @@ type branchResourceModelV0 struct {
170178
ShardCount types.Float64 `tfsdk:"shard_count"`
171179
Sharded types.Bool `tfsdk:"sharded"`
172180
UpdatedAt types.String `tfsdk:"updated_at"`
181+
SeedData types.String `tfsdk:"seed_data"`
173182
}
174183

175184
// branchSchemaV0 defines the schema for version 0.
@@ -271,6 +280,13 @@ func branchSchemaV0() *schema.Schema {
271280
Description: "When the branch was last updated.",
272281
Computed: true,
273282
},
283+
"seed_data": schema.StringAttribute{
284+
Description: "Seed data using the Data Branching® feature. Valid value is `last_successful_backup`",
285+
Optional: true,
286+
PlanModifiers: []planmodifier.String{
287+
stringplanmodifier.RequiresReplace(),
288+
},
289+
},
274290
},
275291
}
276292
}
@@ -304,6 +320,7 @@ func upgradeBranchStateV0toCurrent(ctx context.Context, req resource.UpgradeStat
304320
ShardCount: priorStateData.ShardCount,
305321
Sharded: priorStateData.Sharded,
306322
UpdatedAt: priorStateData.UpdatedAt,
323+
SeedData: priorStateData.SeedData,
307324
}
308325

309326
resp.Diagnostics.Append(resp.State.Set(ctx, upgradedStateData)...)

internal/provider/branch_resource_migration_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestUpgradeBranchStateV0toCurrent(t *testing.T) {
4242
"shard_count": tftypes.NewValue(tftypes.Number, 1),
4343
"sharded": tftypes.NewValue(tftypes.Bool, false),
4444
"updated_at": tftypes.NewValue(tftypes.String, "2023-01-01T03:00:00Z"),
45+
"seed_data": tftypes.NewValue(tftypes.String, "last_successful_backup"),
4546
}),
4647
Schema: branchSchemaV0(),
4748
},
@@ -78,6 +79,7 @@ func TestUpgradeBranchStateV0toCurrent(t *testing.T) {
7879
"shard_count": tftypes.NewValue(tftypes.Number, 1),
7980
"sharded": tftypes.NewValue(tftypes.Bool, false),
8081
"updated_at": tftypes.NewValue(tftypes.String, "2023-01-01T03:00:00Z"),
82+
"seed_data": tftypes.NewValue(tftypes.String, "last_successful_backup"),
8183
}),
8284
Schema: branchSchemaV1(),
8385
},

0 commit comments

Comments
 (0)