Skip to content

Commit 2602dba

Browse files
committed
fix(workers_script): Obtain migrations directly from config instead of plan
Fixes #5898
1 parent a3b6816 commit 2602dba

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

internal/services/workers_script/resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/cloudflare/terraform-provider-cloudflare/internal/importpath"
2020
"github.com/cloudflare/terraform-provider-cloudflare/internal/logging"
2121
"github.com/hashicorp/terraform-plugin-framework/diag"
22+
"github.com/hashicorp/terraform-plugin-framework/path"
2223
"github.com/hashicorp/terraform-plugin-framework/resource"
2324
"github.com/hashicorp/terraform-plugin-framework/types"
2425
"github.com/jinzhu/copier"
@@ -65,6 +66,7 @@ func (r *WorkersScriptResource) Create(ctx context.Context, req resource.CreateR
6566
var data *WorkersScriptModel
6667

6768
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
69+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("migrations"), &data.Migrations)...)
6870

6971
if resp.Diagnostics.HasError() {
7072
return
@@ -126,6 +128,7 @@ func (r *WorkersScriptResource) Update(ctx context.Context, req resource.UpdateR
126128
var data *WorkersScriptModel
127129

128130
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
131+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("migrations"), &data.Migrations)...)
129132

130133
if resp.Diagnostics.HasError() {
131134
return

internal/services/workers_script/resource_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,38 @@ func TestAccCloudflareWorkerScript_PythonWorker(t *testing.T) {
339339
})
340340
}
341341

342+
func TestAccCloudflareWorkerScript_ModuleWithDurableObject(t *testing.T) {
343+
t.Parallel()
344+
345+
rnd := utils.GenerateRandomResourceName()
346+
name := "cloudflare_workers_script." + rnd
347+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
348+
349+
resource.Test(t, resource.TestCase{
350+
PreCheck: func() {
351+
acctest.TestAccPreCheck(t)
352+
acctest.TestAccPreCheck_AccountID(t)
353+
},
354+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
355+
Steps: []resource.TestStep{
356+
{
357+
Config: acctest.LoadTestCase("module_with_durable_object.tf", rnd, accountID),
358+
ConfigStateChecks: []statecheck.StateCheck{
359+
statecheck.ExpectKnownValue(name, tfjsonpath.New("script_name"), knownvalue.StringExact(rnd)),
360+
statecheck.ExpectKnownValue(name, tfjsonpath.New("main_module"), knownvalue.StringExact("worker.js")),
361+
},
362+
},
363+
{
364+
ResourceName: name,
365+
ImportStateIdPrefix: fmt.Sprintf("%s/", accountID),
366+
ImportState: true,
367+
ImportStateVerify: true,
368+
ImportStateVerifyIgnore: []string{"bindings.0.namespace_id", "has_modules", "main_module", "startup_time_ms"},
369+
},
370+
},
371+
})
372+
}
373+
342374
func testAccCheckCloudflareWorkerScriptConfigServiceWorkerInitial(rnd, accountID string) string {
343375
return acctest.LoadTestCase("service_worker_initial.tf", rnd, scriptContent1, accountID)
344376
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "cloudflare_workers_script" "%[1]s" {
2+
account_id = "%[2]s"
3+
script_name = "%[1]s"
4+
content = <<-EOT
5+
import {DurableObject} from "cloudflare:workers"
6+
export class MyDurableObject extends DurableObject {}
7+
export default { fetch() {return new Response()} }
8+
EOT
9+
main_module = "worker.js"
10+
migrations = {
11+
new_tag = "v1"
12+
new_sqlite_classes = ["MyDurableObject"]
13+
}
14+
bindings = [
15+
{
16+
name = "MY_DO"
17+
type = "durable_object_namespace"
18+
class_name = "MyDurableObject"
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)