@@ -244,26 +244,67 @@ func TestAccPostgresqlScript_withDatabase(t *testing.T) {
244244 ]
245245 depends_on = [postgresql_database.test_db]
246246 }
247+
248+ resource "postgresql_script" "test_default" {
249+ commands = [
250+ "CREATE TABLE default_db_table (id INT);",
251+ "INSERT INTO default_db_table VALUES (1);",
252+ "INSERT INTO default_db_table VALUES (2);"
253+ ]
254+ depends_on = [postgresql_database.test_db]
255+ }
247256 `
248257
249258 resource .Test (t , resource.TestCase {
250- PreCheck : func () { testAccPreCheck (t ) },
251- Providers : testAccProviders ,
259+ PreCheck : func () { testAccPreCheck (t ) },
260+ Providers : testAccProviders ,
261+ CheckDestroy : testAccCheckScriptTablesDestroyed ,
252262 Steps : []resource.TestStep {
253263 {
254264 Config : config ,
255265 Check : resource .ComposeTestCheckFunc (
256266 resource .TestCheckResourceAttr ("postgresql_script.test" , "database" , "test_script_db" ),
257267 resource .TestCheckResourceAttr ("postgresql_script.test" , "commands.0" , "CREATE TABLE test_table (id INT);" ),
258268 resource .TestCheckResourceAttr ("postgresql_script.test" , "commands.1" , "INSERT INTO test_table VALUES (1);" ),
269+ resource .TestCheckResourceAttr ("postgresql_script.test_default" , "database" , "postgres" ),
270+ resource .TestCheckResourceAttr ("postgresql_script.test_default" , "commands.0" , "CREATE TABLE default_db_table (id INT);" ),
271+ resource .TestCheckResourceAttr ("postgresql_script.test_default" , "commands.1" , "INSERT INTO default_db_table VALUES (1);" ),
272+ resource .TestCheckResourceAttr ("postgresql_script.test_default" , "commands.2" , "INSERT INTO default_db_table VALUES (2);" ),
259273 testAccCheckTableExistsInDatabase ("test_script_db" , "test_table" ),
260274 testAccCheckTableHasRecords ("test_script_db" , "test_table" , 1 ),
275+ testAccCheckTableExistsInDatabase ("postgres" , "default_db_table" ),
276+ testAccCheckTableHasRecords ("postgres" , "default_db_table" , 2 ),
261277 ),
262278 },
263279 },
264280 })
265281}
266282
283+ func testAccCheckScriptTablesDestroyed (s * terraform.State ) error {
284+ return testAccDropTables (map [string ][]string {
285+ "test_script_db" : {"test_table" },
286+ "postgres" : {"default_db_table" },
287+ })
288+ }
289+
290+ func testAccDropTables (tablesToDrop map [string ][]string ) error {
291+ client := testAccProvider .Meta ().(* Client )
292+
293+ for dbName , tables := range tablesToDrop {
294+ dbClient := client .config .NewClient (dbName )
295+ db , err := dbClient .Connect ()
296+ if err != nil {
297+ continue // Skip if we can't connect to the database
298+ }
299+
300+ for _ , tableName := range tables {
301+ _ , _ = db .Exec (fmt .Sprintf ("DROP TABLE IF EXISTS %s" , tableName ))
302+ }
303+ }
304+
305+ return nil
306+ }
307+
267308func testAccCheckTableExistsInDatabase (dbName , tableName string ) resource.TestCheckFunc {
268309 return func (s * terraform.State ) error {
269310 client := testAccProvider .Meta ().(* Client )
0 commit comments