Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Postgres in aws rds #459

Closed
ggrover opened this issue Feb 13, 2020 · 2 comments · Fixed by #1465
Closed

Support for Postgres in aws rds #459

ggrover opened this issue Feb 13, 2020 · 2 comments · Fixed by #1465
Labels

Comments

@ggrover
Copy link

ggrover commented Feb 13, 2020

While writing some tests for setting up postgres instance in AWS I noticed that rds.go only currently supports mysql. As a result, I've added support for postgres to my local branch. Just wondering if there are plans to add support for postgres in future? Would it be worthwhile to create PR with the set of changes that I have made.

@brikis98
Copy link
Member

Most likely, yes! Could you tell us a bit more about what changes you've added?

@ggrover
Copy link
Author

ggrover commented Mar 17, 2020

I've added to following functions to modules/aws/rds.go to check whether the specified schema/table name exists in the RDS instance

modified: modules/aws/rds.go
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
@@ -8,6 +8,7 @@ import (
    "github.com/aws/aws-sdk-go/service/rds"
    _ "github.com/go-sql-driver/mysql"
    "github.com/gruntwork-io/terratest/modules/testing"
+   _ "github.com/lib/pq"
 )

 // GetAddressOfRdsInstance gets the address of the given RDS Instance in the given region.
@@ -48,6 +49,36 @@ func GetPortOfRdsInstanceE(t testing.TestingT, dbInstanceID string, awsRegion st
    return *dbInstance.Endpoint.Port, nil
 }

+// GetWhetherSchemaExistsInRdsPostgresInstance checks whether the specified schema/table name exists in the RDS instance
+func GetWhetherSchemaExistsInRdsPostgresInstance(t testing.TestingT, dbUrl string, dbPort int64, dbUsername string, dbPassword string, expectedSchemaName string) bool {
+   output, err := GetWhetherSchemaExistsInRdsPostgresInstanceE(t, dbUrl, dbPort, dbUsername, dbPassword, expectedSchemaName)
+   if err != nil {
+       t.Fatal(err)
+   }
+   return output
+}
+
+// GetWhetherSchemaExistsInRdsPostgresInstanceE checks whether the specified schema/table name exists in the RDS instance
+func GetWhetherSchemaExistsInRdsPostgresInstanceE(t testing.TestingT, dbUrl string, dbPort int64, dbUsername string, dbPassword string, expectedSchemaName string) (bool, error) {
+   connectionString := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", dbUrl, dbPort, dbUsername, dbPassword, expectedSchemaName)
+
+   db, connErr := sql.Open("postgres", connectionString)
+   if connErr != nil {
+       return false, connErr
+   }
+   defer db.Close()
+   var (
+       schemaName string
+   )
+   sqlStatement := `SELECT "catalog_name" FROM "information_schema"."schemata" where catalog_name=$1`
+   row := db.QueryRow(sqlStatement, expectedSchemaName)
+   scanErr := row.Scan(&schemaName)
+   if scanErr != nil {
+       return false, scanErr
+   }
+   return true, nil
+}
+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants