From c649f10893ec8ee1480a6ccac40ad89227e84c08 Mon Sep 17 00:00:00 2001 From: Aris Tzoumas Date: Fri, 22 Mar 2024 17:50:23 +0200 Subject: [PATCH] fixup! fixup! feat: redshift sdk driver --- sqlconnect/config/config.go | 15 ++++++++------- sqlconnect/internal/redshift/config.go | 16 ++++++++-------- sqlconnect/internal/redshift/config_test.go | 10 +++++----- sqlconnect/internal/redshift/db.go | 14 +++++++------- sqlconnect/internal/redshift/integration_test.go | 6 +++--- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/sqlconnect/config/config.go b/sqlconnect/config/config.go index a317d5b..bd1e135 100644 --- a/sqlconnect/config/config.go +++ b/sqlconnect/config/config.go @@ -11,11 +11,12 @@ import ( ) type ( - BigQuery = bigquery.Config - Databricks = databricks.Config - Mysql = mysql.Config - Postgres = postgres.Config - Redshift = redshift.Config - Snowflake = snowflake.Config - Trino = trino.Config + BigQuery = bigquery.Config + Databricks = databricks.Config + Mysql = mysql.Config + Postgres = postgres.Config + Redshift = redshift.PostgresConfig + RedshiftData = redshift.Config + Snowflake = snowflake.Config + Trino = trino.Config ) diff --git a/sqlconnect/internal/redshift/config.go b/sqlconnect/internal/redshift/config.go index 2fcf957..9cc96b1 100644 --- a/sqlconnect/internal/redshift/config.go +++ b/sqlconnect/internal/redshift/config.go @@ -9,13 +9,13 @@ import ( "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/postgres" ) -const SDKConfigType = "sdk" +const RedshiftDataConfigType = "sdk" -// Config is the configuration for a redshift database when using postgres driver -type Config = postgres.Config +// PostgresConfig is the configuration for a redshift database when using the postgres driver +type PostgresConfig = postgres.Config -// SDKConfig is the configuration for a redshift database when using the AWS SDK -type SDKConfig struct { +// Config is the configuration for a redshift database when using the redshift data api driver +type Config struct { ClusterIdentifier string `json:"clusterIdentifier"` Database string `json:"database"` User string `json:"user"` @@ -37,15 +37,15 @@ type SDKConfig struct { UseLegacyMappings bool `json:"useLegacyMappings"` } -func (c *SDKConfig) MarshalJSON() ([]byte, error) { +func (c *Config) MarshalJSON() ([]byte, error) { bytes, err := json.Marshal(*c) if err != nil { return nil, err } - return sjson.SetBytes(bytes, "type", SDKConfigType) + return sjson.SetBytes(bytes, "type", RedshiftDataConfigType) } -func (c *SDKConfig) Parse(input json.RawMessage) error { +func (c *Config) Parse(input json.RawMessage) error { err := json.Unmarshal(input, c) if err != nil { return err diff --git a/sqlconnect/internal/redshift/config_test.go b/sqlconnect/internal/redshift/config_test.go index de86fbb..0b0339a 100644 --- a/sqlconnect/internal/redshift/config_test.go +++ b/sqlconnect/internal/redshift/config_test.go @@ -10,9 +10,9 @@ import ( "github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/redshift" ) -func TestRedshiftSDKConfig(t *testing.T) { - // Create a new SDKConfig - config := redshift.SDKConfig{ +func TestRedshiftConfig(t *testing.T) { + // Create a new redshift config + config := redshift.Config{ ClusterIdentifier: "cluster-identifier", Database: "database", User: "user", @@ -23,10 +23,10 @@ func TestRedshiftSDKConfig(t *testing.T) { } configJSON, err := json.Marshal(&config) require.NoError(t, err) - require.Equal(t, "sdk", gjson.GetBytes(configJSON, "type").String()) + require.Equal(t, redshift.RedshiftDataConfigType, gjson.GetBytes(configJSON, "type").String()) // Unmarshal the JSON back into a new SDKConfig - var newConfig redshift.SDKConfig + var newConfig redshift.Config err = newConfig.Parse(configJSON) require.NoError(t, err) require.Equal(t, config, newConfig) diff --git a/sqlconnect/internal/redshift/db.go b/sqlconnect/internal/redshift/db.go index 1ccad7e..d23325f 100644 --- a/sqlconnect/internal/redshift/db.go +++ b/sqlconnect/internal/redshift/db.go @@ -27,10 +27,10 @@ func NewDB(credentialsJSON json.RawMessage) (*DB, error) { ) useLegacyMappings := gjson.GetBytes(credentialsJSON, "useLegacyMappings").Bool() // Use the SDK if the credentials are for the SDK - if configType := gjson.GetBytes(credentialsJSON, "type").Str; configType == SDKConfigType { - db, err = newSdkDB(credentialsJSON) + if configType := gjson.GetBytes(credentialsJSON, "type").Str; configType == RedshiftDataConfigType { + db, err = newRedshiftDataDB(credentialsJSON) } else { - db, err = newPgDB(credentialsJSON) + db, err = newPostgresDB(credentialsJSON) } if err != nil { return nil, err @@ -57,8 +57,8 @@ func NewDB(credentialsJSON json.RawMessage) (*DB, error) { }, nil } -func newPgDB(credentialsJSON json.RawMessage) (*sql.DB, error) { - var config Config +func newPostgresDB(credentialsJSON json.RawMessage) (*sql.DB, error) { + var config PostgresConfig err := config.Parse(credentialsJSON) if err != nil { return nil, err @@ -67,8 +67,8 @@ func newPgDB(credentialsJSON json.RawMessage) (*sql.DB, error) { return sql.Open(postgres.DatabaseType, config.ConnectionString()) } -func newSdkDB(credentialsJSON json.RawMessage) (*sql.DB, error) { - var config SDKConfig +func newRedshiftDataDB(credentialsJSON json.RawMessage) (*sql.DB, error) { + var config Config err := config.Parse(credentialsJSON) if err != nil { return nil, err diff --git a/sqlconnect/internal/redshift/integration_test.go b/sqlconnect/internal/redshift/integration_test.go index 50ae448..fc1fcdf 100644 --- a/sqlconnect/internal/redshift/integration_test.go +++ b/sqlconnect/internal/redshift/integration_test.go @@ -13,16 +13,16 @@ func TestRedshiftDB(t *testing.T) { t.Run("postgres driver", func(t *testing.T) { configJSON, ok := os.LookupEnv("REDSHIFT_TEST_ENVIRONMENT_CREDENTIALS") if !ok { - t.Skip("skipping redshift pg integration test due to lack of a test environment") + t.Skip("skipping redshift postgres driver integration test due to lack of a test environment") } integrationtest.TestDatabaseScenarios(t, redshift.DatabaseType, []byte(configJSON), strings.ToLower, integrationtest.Options{LegacySupport: true}) }) - t.Run("sdk driver", func(t *testing.T) { + t.Run("redshift data driver", func(t *testing.T) { configJSON, ok := os.LookupEnv("REDSHIFT_SDK_TEST_ENVIRONMENT_CREDENTIALS") if !ok { - t.Skip("skipping redshift sdk integration test due to lack of a test environment") + t.Skip("skipping redshift data driver integration test due to lack of a test environment") } integrationtest.TestDatabaseScenarios(t, redshift.DatabaseType, []byte(configJSON), strings.ToLower, integrationtest.Options{LegacySupport: true}) })