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

CLI: Clean Testing Databases with Goroutines #62

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 57 additions & 11 deletions cli/commands/clean_tests.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package commands

import (
"database/sql"
"os/user"

"fmt"
"os/exec"
"sync"

_ "github.com/lib/pq"
"github.com/urfave/cli/v2"
)

Expand All @@ -16,28 +20,70 @@ func ClearDBCommand() *cli.Command {
return cli.Exit("Invalid arguments", 1)
}

ClearDB()
err := CleanTestDBs()

if err != nil {
return cli.Exit(err.Error(), 1)
}
return nil
},
}

return &command
}

func ClearDB() error {
func CleanTestDBs() error {
db, err := sql.Open("postgres", CONFIG.Database.WithDb())

fmt.Println("Clearing databases")
if err != nil {
return err
}

defer db.Close()

cmd := exec.Command("./scripts/clean_old_test_dbs.sh")
cmd.Dir = ROOT_DIR
currentUser, err := user.Current()

out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
return cli.Exit("Failed to clean old test databases", 1)
return fmt.Errorf("failed to get current user: %w", err)
}

fmt.Println("Databases cleared")

rows, err := db.Query("SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres' AND datname != $1 AND datname != $2", currentUser.Username, CONFIG.Database.DatabaseName)

if err != nil {
return err
}

defer rows.Close()

var wg sync.WaitGroup

for rows.Next() {
var dbName string

if err := rows.Scan(&dbName); err != nil {
return err
}

wg.Add(1)

go func(dbName string) {

defer wg.Done()

fmt.Printf("Dropping database %s\n", dbName)

if _, err := db.Exec(fmt.Sprintf("DROP DATABASE %s", dbName)); err != nil {
fmt.Printf("Failed to drop database %s: %v\n", dbName, err)
}

}(dbName)
}

if err := rows.Err(); err != nil {
return err
}

wg.Wait()

return nil
}
2 changes: 2 additions & 0 deletions cli/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package commands
import (
"path/filepath"

"github.com/GenerateNU/sac/backend/src/config"
"github.com/GenerateNU/sac/cli/utils"
)

var ROOT_DIR, _ = utils.GetRootDir()
var FRONTEND_DIR = filepath.Join(ROOT_DIR, "/frontend")
var BACKEND_DIR = filepath.Join(ROOT_DIR, "/backend/src")
var CONFIG, _ = config.GetConfiguration(filepath.Join(ROOT_DIR, "/config"))
18 changes: 4 additions & 14 deletions cli/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,20 @@ func Test(folder string, runFrontend bool, runBackend bool) error {
}

func BackendTest() error {
// rootDir, err := utils.GetRootDir()
// if err != nil {
// return cli.Exit("Couldn't find the project root", 1)
// }

cmd := exec.Command("go", "test", "./...")
cmd.Dir = BACKEND_DIR + "/.."
cmd.Dir = fmt.Sprintf("%s/..", BACKEND_DIR)

defer CleanTestDBs()

out, err := cmd.CombinedOutput()

if err != nil {
fmt.Println(string(out))
return cli.Exit("Failed to run backend tests", 1)
}

fmt.Println(string(out))

cmd = exec.Command("./scripts/clean_old_test_dbs.sh")
cmd.Dir = ROOT_DIR

err = cmd.Run()
if err != nil {
return cli.Exit("Failed to clean old test databases", 1)
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require github.com/urfave/cli/v2 v2.27.1

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/lib/pq v1.10.9
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
2 changes: 2 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
Expand Down
2 changes: 0 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ set -o pipefail
# Get the absolute path to the current script
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# /Users/davidoduneye/northeastern/org-generate/sac

# Build the Go CLI tool
go build -o "sac-cli" "cli/main.go"

Expand Down
17 changes: 0 additions & 17 deletions scripts/clean_old_test_dbs.sh

This file was deleted.

17 changes: 0 additions & 17 deletions scripts/clean_prefixed_test_dbs.sh

This file was deleted.

Loading