Skip to content

Commit a660d1c

Browse files
feat: Allow to precise a database to connect to in a script
1 parent 0bda91c commit a660d1c

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

postgresql/resource_postgresql_script.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
const (
1616
scriptCommandsAttr = "commands"
17+
scriptDatabaseAttr = "database"
1718
scriptTriesAttr = "tries"
1819
scriptBackoffDelayAttr = "backoff_delay"
1920
scriptTimeoutAttr = "timeout"
@@ -28,6 +29,12 @@ func resourcePostgreSQLScript() *schema.Resource {
2829
Delete: PGResourceFunc(resourcePostgreSQLScriptDelete),
2930

3031
Schema: map[string]*schema.Schema{
32+
scriptDatabaseAttr: {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
Computed: true,
36+
Description: "The database to execute commands in (defaults to provider's configured database)",
37+
},
3138
scriptCommandsAttr: {
3239
Type: schema.TypeList,
3340
Required: true,
@@ -77,28 +84,60 @@ func resourcePostgreSQLScriptCreateOrUpdate(ctx context.Context, db *DBConnectio
7784
}}
7885
}
7986

80-
sum := shasumCommands(commands)
87+
// Get the target database connection
88+
database := getDatabase(d, db.client.databaseName)
8189

82-
if err := executeCommands(ctx, db, commands, tries, backoffDelay, timeout); err != nil {
90+
targetDB := db
91+
if database != "" && database != db.client.databaseName {
92+
client := db.client.config.NewClient(database)
93+
newDB, err := client.Connect()
94+
if err != nil {
95+
return diag.Diagnostics{diag.Diagnostic{
96+
Severity: diag.Error,
97+
Summary: "Failed to connect to database",
98+
Detail: err.Error(),
99+
}}
100+
}
101+
targetDB = newDB
102+
}
103+
104+
if err := executeCommands(ctx, targetDB, commands, tries, backoffDelay, timeout); err != nil {
83105
return diag.Diagnostics{diag.Diagnostic{
84106
Severity: diag.Error,
85107
Summary: "Commands execution failed",
86108
Detail: err.Error(),
87109
}}
88110
}
89111

90-
d.Set(scriptShasumAttr, sum)
112+
sum := shasumCommands(commands)
91113
d.SetId(sum)
114+
115+
if err := resourcePostgreSQLScriptReadImpl(db, d); err != nil {
116+
return diag.Diagnostics{diag.Diagnostic{
117+
Severity: diag.Error,
118+
Summary: "Failed to read script state",
119+
Detail: err.Error(),
120+
}}
121+
}
122+
92123
return nil
93124
}
94125

95126
func resourcePostgreSQLScriptRead(db *DBConnection, d *schema.ResourceData) error {
127+
return resourcePostgreSQLScriptReadImpl(db, d)
128+
}
129+
130+
func resourcePostgreSQLScriptReadImpl(db *DBConnection, d *schema.ResourceData) error {
96131
commands, err := toStringArray(d.Get(scriptCommandsAttr).([]any))
97132
if err != nil {
98133
return err
99134
}
100135
newSum := shasumCommands(commands)
136+
137+
database := getDatabase(d, db.client.databaseName)
138+
101139
d.Set(scriptShasumAttr, newSum)
140+
d.Set(scriptDatabaseAttr, database)
102141

103142
return nil
104143
}

0 commit comments

Comments
 (0)