|
1 | 1 | package postgresql |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "regexp" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | func TestAccPostgresqlScript_basic(t *testing.T) { |
@@ -227,3 +229,85 @@ func TestAccPostgresqlScript_timeout(t *testing.T) { |
227 | 229 | }, |
228 | 230 | }) |
229 | 231 | } |
| 232 | + |
| 233 | +func TestAccPostgresqlScript_withDatabase(t *testing.T) { |
| 234 | + config := ` |
| 235 | + resource "postgresql_database" "test_db" { |
| 236 | + name = "test_script_db" |
| 237 | + } |
| 238 | +
|
| 239 | + resource "postgresql_script" "test" { |
| 240 | + database = postgresql_database.test_db.name |
| 241 | + commands = [ |
| 242 | + "CREATE TABLE test_table (id INT);", |
| 243 | + "INSERT INTO test_table VALUES (1);" |
| 244 | + ] |
| 245 | + depends_on = [postgresql_database.test_db] |
| 246 | + } |
| 247 | + ` |
| 248 | + |
| 249 | + resource.Test(t, resource.TestCase{ |
| 250 | + PreCheck: func() { testAccPreCheck(t) }, |
| 251 | + Providers: testAccProviders, |
| 252 | + Steps: []resource.TestStep{ |
| 253 | + { |
| 254 | + Config: config, |
| 255 | + Check: resource.ComposeTestCheckFunc( |
| 256 | + resource.TestCheckResourceAttr("postgresql_script.test", "database", "test_script_db"), |
| 257 | + resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "CREATE TABLE test_table (id INT);"), |
| 258 | + resource.TestCheckResourceAttr("postgresql_script.test", "commands.1", "INSERT INTO test_table VALUES (1);"), |
| 259 | + testAccCheckTableExistsInDatabase("test_script_db", "test_table"), |
| 260 | + testAccCheckTableHasRecords("test_script_db", "test_table", 1), |
| 261 | + ), |
| 262 | + }, |
| 263 | + }, |
| 264 | + }) |
| 265 | +} |
| 266 | + |
| 267 | +func testAccCheckTableExistsInDatabase(dbName, tableName string) resource.TestCheckFunc { |
| 268 | + return func(s *terraform.State) error { |
| 269 | + client := testAccProvider.Meta().(*Client) |
| 270 | + dbClient := client.config.NewClient(dbName) |
| 271 | + db, err := dbClient.Connect() |
| 272 | + if err != nil { |
| 273 | + return fmt.Errorf("Error connecting to database %s: %s", dbName, err) |
| 274 | + } |
| 275 | + |
| 276 | + var exists bool |
| 277 | + query := "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1)" |
| 278 | + err = db.QueryRow(query, tableName).Scan(&exists) |
| 279 | + if err != nil { |
| 280 | + return fmt.Errorf("Error checking if table %s exists: %s", tableName, err) |
| 281 | + } |
| 282 | + |
| 283 | + if !exists { |
| 284 | + return fmt.Errorf("Table %s does not exist in database %s", tableName, dbName) |
| 285 | + } |
| 286 | + |
| 287 | + return nil |
| 288 | + } |
| 289 | +} |
| 290 | + |
| 291 | +func testAccCheckTableHasRecords(dbName, tableName string, expectedCount int) resource.TestCheckFunc { |
| 292 | + return func(s *terraform.State) error { |
| 293 | + client := testAccProvider.Meta().(*Client) |
| 294 | + dbClient := client.config.NewClient(dbName) |
| 295 | + db, err := dbClient.Connect() |
| 296 | + if err != nil { |
| 297 | + return fmt.Errorf("Error connecting to database %s: %s", dbName, err) |
| 298 | + } |
| 299 | + |
| 300 | + var count int |
| 301 | + query := fmt.Sprintf("SELECT COUNT(*) FROM %s", tableName) |
| 302 | + err = db.QueryRow(query).Scan(&count) |
| 303 | + if err != nil { |
| 304 | + return fmt.Errorf("Error counting records in table %s: %s", tableName, err) |
| 305 | + } |
| 306 | + |
| 307 | + if count != expectedCount { |
| 308 | + return fmt.Errorf("Expected %d records but got %d in table %s", expectedCount, count, tableName) |
| 309 | + } |
| 310 | + |
| 311 | + return nil |
| 312 | + } |
| 313 | +} |
0 commit comments