diff --git a/cli/commands/clean_tests.go b/cli/commands/clean_tests.go index a4c3ad3c6..2acef2bbf 100644 --- a/cli/commands/clean_tests.go +++ b/cli/commands/clean_tests.go @@ -1,9 +1,13 @@ package commands import ( + "database/sql" + "os/user" + "fmt" - "os/exec" + "sync" + _ "github.com/lib/pq" "github.com/urfave/cli/v2" ) @@ -16,7 +20,11 @@ func ClearDBCommand() *cli.Command { return cli.Exit("Invalid arguments", 1) } - ClearDB() + err := CleanTestDBs() + + if err != nil { + return cli.Exit(err.Error(), 1) + } return nil }, } @@ -24,20 +32,58 @@ func ClearDBCommand() *cli.Command { 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 } diff --git a/cli/commands/config.go b/cli/commands/config.go index 06ca14e6b..e4039f347 100644 --- a/cli/commands/config.go +++ b/cli/commands/config.go @@ -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")) diff --git a/cli/commands/test.go b/cli/commands/test.go index 50a58c934..3c912ecb6 100644 --- a/cli/commands/test.go +++ b/cli/commands/test.go @@ -74,15 +74,13 @@ 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) @@ -90,14 +88,6 @@ func BackendTest() error { 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 } diff --git a/cli/go.mod b/cli/go.mod index 3c4a30d00..279a6c129 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -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 ) diff --git a/cli/go.sum b/cli/go.sum index 31517c04a..b030e3334 100644 --- a/cli/go.sum +++ b/cli/go.sum @@ -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= diff --git a/install.sh b/install.sh index 9a5b8e7bf..e7b0bee9d 100755 --- a/install.sh +++ b/install.sh @@ -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" diff --git a/scripts/clean_old_test_dbs.sh b/scripts/clean_old_test_dbs.sh deleted file mode 100755 index 04c97262b..000000000 --- a/scripts/clean_old_test_dbs.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" - -PGHOST="127.0.0.1" -PGPORT="5432" -PGUSER="postgres" -PGPASSWORD="postgres" -PGDATABASE="sac" - -DATABASES=$(psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" -t -c "SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres' AND datname != 'template0' AND datname != '$PGDATABASE' AND datname !='$(whoami)';") - - -for db in $DATABASES; do - echo "Dropping database $db" - psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" -c "DROP DATABASE $db;" -done diff --git a/scripts/clean_prefixed_test_dbs.sh b/scripts/clean_prefixed_test_dbs.sh deleted file mode 100755 index 61797e999..000000000 --- a/scripts/clean_prefixed_test_dbs.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" - -PGHOST="127.0.0.1" -PGPORT="5432" -PGUSER="postgres" -PGPASSWORD="postgres" -PGDATABASE="sac" -PREFIX="sac_test_" - -DATABASES=$(psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" -t -c "SELECT datname FROM pg_database WHERE datistemplate = false AND datname like '$PREFIX%';") - -for db in $DATABASES; do - echo "Dropping database $db" - psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" -c "DROP DATABASE $db;" -done