Skip to content

Commit f2e8afb

Browse files
test
1 parent a9386ec commit f2e8afb

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

postgresql/resource_postgresql_script_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,85 @@ func testAccCheckTableHasRecords(dbName, tableName string, expectedCount int) re
352352
return nil
353353
}
354354
}
355+
356+
func TestAccPostgresqlScript_setLocalRoleWorks(t *testing.T) {
357+
// This test demonstrates that SET LOCAL ROLE works across separate commands
358+
// because they are concatenated into a single SQL statement to be executed on a single connection
359+
config := `
360+
resource "postgresql_role" "test_role" {
361+
name = "test_owner_role"
362+
}
363+
364+
resource "postgresql_script" "grant_role" {
365+
commands = [
366+
"GRANT test_owner_role TO CURRENT_USER",
367+
"GRANT CREATE ON SCHEMA public TO test_owner_role"
368+
]
369+
depends_on = [postgresql_role.test_role]
370+
}
371+
372+
resource "postgresql_script" "test_with_set_local_separate" {
373+
commands = [
374+
"BEGIN",
375+
"SET LOCAL ROLE test_owner_role",
376+
"CREATE TABLE test_set_locals (id INT)",
377+
"COMMIT"
378+
]
379+
depends_on = [postgresql_script.grant_role]
380+
}
381+
`
382+
383+
resource.Test(t, resource.TestCase{
384+
PreCheck: func() { testAccPreCheck(t) },
385+
Providers: testAccProviders,
386+
CheckDestroy: testAccCheckSetLocalRoleTablesDestroyed,
387+
Steps: []resource.TestStep{
388+
{
389+
Config: config,
390+
Check: resource.ComposeTestCheckFunc(
391+
testAccCheckTableExistsInDatabase("postgres", "test_set_locals"),
392+
// Both commands should now work with SET LOCAL ROLE since commands are concatenated
393+
testAccCheckTableOwner("postgres", "test_set_locals", "test_owner_role"),
394+
),
395+
},
396+
},
397+
})
398+
}
399+
400+
func testAccCheckTableOwner(dbName, tableName, expectedOwner string) resource.TestCheckFunc {
401+
return func(s *terraform.State) error {
402+
client := testAccProvider.Meta().(*Client)
403+
dbClient := client.config.NewClient(dbName)
404+
db, err := dbClient.Connect()
405+
if err != nil {
406+
return fmt.Errorf("Error connecting to database %s: %s", dbName, err)
407+
}
408+
409+
var owner string
410+
query := `SELECT tableowner FROM pg_tables WHERE schemaname = 'public' AND tablename = $1`
411+
err = db.QueryRow(query, tableName).Scan(&owner)
412+
if err != nil {
413+
return fmt.Errorf("Error checking owner of table %s: %s", tableName, err)
414+
}
415+
416+
if owner != expectedOwner {
417+
return fmt.Errorf("Expected table %s to be owned by %s but got %s", tableName, expectedOwner, owner)
418+
}
419+
420+
return nil
421+
}
422+
}
423+
424+
func testAccCheckSetLocalRoleTablesDestroyed(s *terraform.State) error {
425+
client := testAccProvider.Meta().(*Client)
426+
db, err := client.Connect()
427+
if err != nil {
428+
return nil // Skip if we can't connect
429+
}
430+
431+
_, _ = db.Exec("DROP TABLE IF EXISTS test_set_local_separate")
432+
_, _ = db.Exec("DROP TABLE IF EXISTS test_set_local_single")
433+
_, _ = db.Exec("DROP ROLE IF EXISTS test_owner_role")
434+
435+
return nil
436+
}

0 commit comments

Comments
 (0)