-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: cleanup test schemas periodically
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: cleanup test schemas | ||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: '15 3 * * 6' | ||
|
||
permissions: | ||
id-token: write # allows the JWT to be requested from GitHub's OIDC provider | ||
contents: read # This is required for actions/checkout | ||
|
||
jobs: | ||
cleanup: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: '1.22' | ||
check-latest: true | ||
- run: go version | ||
- run: go mod download | ||
- name: cleanup | ||
env: | ||
REDSHIFT_TEST_ENVIRONMENT_CREDENTIALS: ${{ secrets.REDSHIFT_TEST_ENVIRONMENT_CREDENTIALS }} | ||
REDSHIFT_DATA_TEST_ENVIRONMENT_CREDENTIALS: ${{ secrets.REDSHIFT_DATA_TEST_ENVIRONMENT_CREDENTIALS }} | ||
REDSHIFT_DATA_TEST_ENVIRONMENT_ROLE_ARN_CREDENTIALS: ${{ secrets.REDSHIFT_DATA_TEST_ENVIRONMENT_ROLE_ARN_CREDENTIALS }} | ||
SNOWFLAKE_TEST_ENVIRONMENT_CREDENTIALS: ${{ secrets.SNOWFLAKE_TEST_ENVIRONMENT_CREDENTIALS }} | ||
BIGQUERY_TEST_ENVIRONMENT_CREDENTIALS: ${{ secrets.BIGQUERY_TEST_ENVIRONMENT_CREDENTIALS }} | ||
DATABRICKS_TEST_ENVIRONMENT_CREDENTIALS: ${{ secrets.DATABRICKS_TEST_ENVIRONMENT_CREDENTIALS }} | ||
TRINO_TEST_ENVIRONMENT_CREDENTIALS: ${{ secrets.TRINO_TEST_ENVIRONMENT_CREDENTIALS }} | ||
SNOWFLAKE_TEST_AUTH_KEYPAIR_ENCRYPTED_CREDENTIALS: ${{ secrets.SNOWFLAKE_TEST_AUTH_KEYPAIR_ENCRYPTED_CREDENTIALS }} | ||
SNOWFLAKE_TEST_AUTH_KEYPAIR_UNENCRYPTED_CREDENTIALS: ${{ secrets.SNOWFLAKE_TEST_AUTH_KEYPAIR_UNENCRYPTED_CREDENTIALS }} | ||
run: | | ||
go run sqlconnect/cmd/cleanup/cleanup.go | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/tidwall/sjson" | ||
|
||
"github.com/rudderlabs/sqlconnect-go/sqlconnect" | ||
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/bigquery" | ||
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/databricks" | ||
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/redshift" | ||
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/snowflake" | ||
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/trino" | ||
) | ||
|
||
func main() { | ||
cleanupConfigs := []cleanupConfig{ | ||
{Env: "BIGQUERY_TEST_ENVIRONMENT_CREDENTIALS", Type: bigquery.DatabaseType}, | ||
{Env: "DATABRICKS_TEST_ENVIRONMENT_CREDENTIALS", Type: databricks.DatabaseType, Fn: func(s string) string { | ||
s, _ = sjson.Set(s, "catalog", "hive_metastore") | ||
return s | ||
}}, | ||
{Env: "DATABRICKS_TEST_ENVIRONMENT_CREDENTIALS", Type: databricks.DatabaseType, Fn: func(s string) string { | ||
s, _ = sjson.Set(s, "catalog", "sqlconnect") | ||
return s | ||
}}, | ||
{Env: "REDSHIFT_DATA_TEST_ENVIRONMENT_CREDENTIALS", Type: redshift.DatabaseType}, | ||
{Env: "REDSHIFT_TEST_ENVIRONMENT_CREDENTIALS", Type: redshift.DatabaseType}, | ||
{Env: "SNOWFLAKE_TEST_ENVIRONMENT_CREDENTIALS", Type: snowflake.DatabaseType}, | ||
{Env: "TRINO_TEST_ENVIRONMENT_CREDENTIALS", Type: trino.DatabaseType}, | ||
} | ||
|
||
for _, c := range cleanupConfigs { | ||
configJSON, ok := os.LookupEnv(c.Env) | ||
if !ok { | ||
log.Fatalf("%s environment variable not set", c.Env) | ||
} | ||
if c.Fn != nil { | ||
configJSON = c.Fn(configJSON) | ||
} | ||
db, err := sqlconnect.NewDB(c.Type, []byte(configJSON)) | ||
if err != nil { | ||
log.Fatalf("[%s] failed to create db: %v", c.Type, err) | ||
} | ||
schemas, err := db.ListSchemas(context.Background()) | ||
if err != nil { | ||
log.Fatalf("[%s] failed to list schemas: %v", c.Type, err) | ||
} | ||
for _, schema := range schemas { | ||
if strings.Contains(strings.ToLower(schema.Name), "tsqlcon_") { | ||
err := db.DropSchema(context.Background(), schema) | ||
if err != nil { | ||
log.Printf("[%s] failed to drop schema: %v", c.Type, err) | ||
} else { | ||
log.Printf("[%s] dropped schema %s", c.Type, schema) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
type cleanupConfig struct { | ||
Type string | ||
Env string | ||
Fn func(string) string | ||
} |