Skip to content

Commit 924be53

Browse files
Add database parameter on function resource (#216)
* Add database parameter on function resource * update documentation * add skipIfNotAcc
1 parent 4c52d3a commit 924be53

File tree

3 files changed

+121
-11
lines changed

3 files changed

+121
-11
lines changed

postgresql/resource_postgresql_function.go

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
funcArgAttr = "arg"
1717
funcReturnsAttr = "returns"
1818
funcDropCascadeAttr = "drop_cascade"
19+
funcDatabaseAttr = "database"
1920

2021
funcArgTypeAttr = "type"
2122
funcArgNameAttr = "name"
@@ -99,6 +100,13 @@ func resourcePostgreSQLFunction() *schema.Resource {
99100
Optional: true,
100101
Default: false,
101102
},
103+
funcDatabaseAttr: {
104+
Type: schema.TypeString,
105+
Optional: true,
106+
Computed: true,
107+
ForceNew: true,
108+
Description: "The database where the function is located. If not specified, the provider default database is used.",
109+
},
102110
},
103111
}
104112
}
@@ -129,9 +137,22 @@ func resourcePostgreSQLFunctionExists(db *DBConnection, d *schema.ResourceData)
129137
signature := getFunctionSignature(d)
130138
functionExists := false
131139

140+
txn, err := startTransaction(db.client, d.Get(funcDatabaseAttr).(string))
141+
if err != nil {
142+
return false, err
143+
}
144+
defer deferredRollback(txn)
145+
132146
query := fmt.Sprintf("SELECT to_regprocedure('%s') IS NOT NULL", signature)
133-
err := db.QueryRow(query).Scan(&functionExists)
134-
return functionExists, err
147+
if err := txn.QueryRow(query).Scan(&functionExists); err != nil {
148+
return false, err
149+
}
150+
151+
if err := txn.Commit(); err != nil {
152+
return false, err
153+
}
154+
155+
return functionExists, nil
135156
}
136157

137158
func resourcePostgreSQLFunctionRead(db *DBConnection, d *schema.ResourceData) error {
@@ -154,7 +175,14 @@ func resourcePostgreSQLFunctionReadImpl(db *DBConnection, d *schema.ResourceData
154175
`FROM pg_proc p ` +
155176
`LEFT JOIN pg_namespace n ON p.pronamespace = n.oid ` +
156177
`WHERE p.oid = to_regprocedure($1)`
157-
err := db.QueryRow(query, signature).Scan(&funcSchema, &funcName)
178+
179+
txn, err := startTransaction(db.client, d.Get(funcDatabaseAttr).(string))
180+
if err != nil {
181+
return err
182+
}
183+
defer deferredRollback(txn)
184+
185+
err = txn.QueryRow(query, signature).Scan(&funcSchema, &funcName)
158186
switch {
159187
case err == sql.ErrNoRows:
160188
log.Printf("[WARN] PostgreSQL function: %s", signature)
@@ -164,6 +192,16 @@ func resourcePostgreSQLFunctionReadImpl(db *DBConnection, d *schema.ResourceData
164192
return fmt.Errorf("Error reading function: %w", err)
165193
}
166194

195+
if err := txn.Commit(); err != nil {
196+
return err
197+
}
198+
199+
if v, ok := d.GetOk(funcDatabaseAttr); ok {
200+
d.Set(funcDatabaseAttr, v)
201+
} else {
202+
d.Set(funcDatabaseAttr, db.client.databaseName)
203+
}
204+
167205
d.Set(funcNameAttr, funcName)
168206
d.Set(funcSchemaAttr, funcSchema)
169207
d.SetId(signature)
@@ -187,7 +225,18 @@ func resourcePostgreSQLFunctionDelete(db *DBConnection, d *schema.ResourceData)
187225
}
188226

189227
sql := fmt.Sprintf("DROP FUNCTION IF EXISTS %s %s", signature, dropMode)
190-
if _, err := db.Exec(sql); err != nil {
228+
229+
txn, err := startTransaction(db.client, d.Get(funcDatabaseAttr).(string))
230+
if err != nil {
231+
return err
232+
}
233+
defer deferredRollback(txn)
234+
235+
if _, err := txn.Exec(sql); err != nil {
236+
return err
237+
}
238+
239+
if err := txn.Commit(); err != nil {
191240
return err
192241
}
193242

@@ -271,7 +320,18 @@ func createFunction(db *DBConnection, d *schema.ResourceData, replace bool) erro
271320
fmt.Fprint(b, "\n", d.Get(funcBodyAttr).(string))
272321

273322
sql := b.String()
274-
if _, err := db.Exec(sql); err != nil {
323+
324+
txn, err := startTransaction(db.client, d.Get(funcDatabaseAttr).(string))
325+
if err != nil {
326+
return err
327+
}
328+
defer deferredRollback(txn)
329+
330+
if _, err := txn.Exec(sql); err != nil {
331+
return err
332+
}
333+
334+
if err := txn.Commit(); err != nil {
275335
return err
276336
}
277337

postgresql/resource_postgresql_function_test.go

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ resource "postgresql_function" "basic_function" {
3535
{
3636
Config: config,
3737
Check: resource.ComposeTestCheckFunc(
38-
testAccCheckPostgresqlFunctionExists("postgresql_function.basic_function"),
38+
testAccCheckPostgresqlFunctionExists("postgresql_function.basic_function", ""),
3939
resource.TestCheckResourceAttr(
4040
"postgresql_function.basic_function", "name", "basic_function"),
4141
resource.TestCheckResourceAttr(
@@ -46,6 +46,53 @@ resource "postgresql_function" "basic_function" {
4646
})
4747
}
4848

49+
func TestAccPostgresqlFunction_SpecificDatabase(t *testing.T) {
50+
skipIfNotAcc(t)
51+
52+
dbSuffix, teardown := setupTestDatabase(t, true, true)
53+
defer teardown()
54+
55+
dbName, _ := getTestDBNames(dbSuffix)
56+
57+
config := `
58+
resource "postgresql_function" "basic_function" {
59+
name = "basic_function"
60+
database = "%s"
61+
returns = "integer"
62+
body = <<-EOF
63+
AS $$
64+
BEGIN
65+
RETURN 1;
66+
END;
67+
$$ LANGUAGE plpgsql;
68+
EOF
69+
}
70+
`
71+
72+
resource.Test(t, resource.TestCase{
73+
PreCheck: func() {
74+
testAccPreCheck(t)
75+
testCheckCompatibleVersion(t, featureFunction)
76+
},
77+
Providers: testAccProviders,
78+
CheckDestroy: testAccCheckPostgresqlFunctionDestroy,
79+
Steps: []resource.TestStep{
80+
{
81+
Config: fmt.Sprintf(config, dbName),
82+
Check: resource.ComposeTestCheckFunc(
83+
testAccCheckPostgresqlFunctionExists("postgresql_function.basic_function", dbName),
84+
resource.TestCheckResourceAttr(
85+
"postgresql_function.basic_function", "name", "basic_function"),
86+
resource.TestCheckResourceAttr(
87+
"postgresql_function.basic_function", "database", dbName),
88+
resource.TestCheckResourceAttr(
89+
"postgresql_function.basic_function", "schema", "public"),
90+
),
91+
},
92+
},
93+
})
94+
}
95+
4996
func TestAccPostgresqlFunction_MultipleArgs(t *testing.T) {
5097
config := `
5198
resource "postgresql_schema" "test" {
@@ -86,7 +133,7 @@ resource "postgresql_function" "increment" {
86133
{
87134
Config: config,
88135
Check: resource.ComposeTestCheckFunc(
89-
testAccCheckPostgresqlFunctionExists("postgresql_function.increment"),
136+
testAccCheckPostgresqlFunctionExists("postgresql_function.increment", ""),
90137
resource.TestCheckResourceAttr(
91138
"postgresql_function.increment", "name", "increment"),
92139
resource.TestCheckResourceAttr(
@@ -136,7 +183,7 @@ resource "postgresql_function" "func" {
136183
{
137184
Config: configCreate,
138185
Check: resource.ComposeTestCheckFunc(
139-
testAccCheckPostgresqlFunctionExists("postgresql_function.func"),
186+
testAccCheckPostgresqlFunctionExists("postgresql_function.func", ""),
140187
resource.TestCheckResourceAttr(
141188
"postgresql_function.func", "name", "func"),
142189
resource.TestCheckResourceAttr(
@@ -146,7 +193,7 @@ resource "postgresql_function" "func" {
146193
{
147194
Config: configUpdate,
148195
Check: resource.ComposeTestCheckFunc(
149-
testAccCheckPostgresqlFunctionExists("postgresql_function.func"),
196+
testAccCheckPostgresqlFunctionExists("postgresql_function.func", ""),
150197
resource.TestCheckResourceAttr(
151198
"postgresql_function.func", "name", "func"),
152199
resource.TestCheckResourceAttr(
@@ -157,7 +204,7 @@ resource "postgresql_function" "func" {
157204
})
158205
}
159206

160-
func testAccCheckPostgresqlFunctionExists(n string) resource.TestCheckFunc {
207+
func testAccCheckPostgresqlFunctionExists(n string, database string) resource.TestCheckFunc {
161208
return func(s *terraform.State) error {
162209
rs, ok := s.RootModule().Resources[n]
163210
if !ok {
@@ -171,7 +218,7 @@ func testAccCheckPostgresqlFunctionExists(n string) resource.TestCheckFunc {
171218
signature := rs.Primary.ID
172219

173220
client := testAccProvider.Meta().(*Client)
174-
txn, err := startTransaction(client, "")
221+
txn, err := startTransaction(client, database)
175222
if err != nil {
176223
return err
177224
}

website/docs/r/postgresql_function.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ resource "postgresql_function" "increment" {
3838
* `schema` - (Optional) The schema where the function is located.
3939
If not specified, the function is created in the current schema.
4040

41+
* `database` - (Optional) The database where the function is located.
42+
If not specified, the function is created in the current database.
43+
4144
* `arg` - (Optional) List of arguments for the function.
4245
* `type` - (Required) The type of the argument.
4346
* `name` - (Optional) The name of the argument.

0 commit comments

Comments
 (0)