Skip to content

Commit

Permalink
fix test cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Mar 2, 2024
1 parent 7097954 commit 9849c9a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 58 deletions.
112 changes: 55 additions & 57 deletions cli/commands/clean_tests.go
Original file line number Diff line number Diff line change
@@ -1,87 +1,85 @@
package commands

import (
"context"
"database/sql"
"fmt"
"os/user"
"sync"
"time"

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

func ClearDBCommand() *cli.Command {
command := cli.Command{
Name: "clean",
Category: "Database Operations",
Aliases: []string{"c"},
Usage: "Remove databases used for testing",
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
}

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

return nil
},
}

return &command
command := cli.Command{

Check failure on line 16 in cli/commands/clean_tests.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
Name: "clean",
Category: "Database Operations",
Aliases: []string{"c"},
Usage: "Remove databases used for testing",
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
}

return CleanTestDBs(context.Background())
},
}

return &command
}

func CleanTestDBs() error {
fmt.Println("Cleaning test databases")
func CleanTestDBs(ctx context.Context) error {
fmt.Println("Cleaning test databases")

db, err := sql.Open("postgres", CONFIG.Database.WithDb())
if err != nil {
return err
}
db, err := sql.Open("postgres", CONFIG.Database.WithDb())
if err != nil {
return err
}

defer db.Close()
defer db.Close()

currentUser, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %w", err)
}
currentUser, err := user.Current()
if err != nil {
return fmt.Errorf("failed to get current user: %w", err)
}

rows, err := db.Query("SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres' AND datname != $1 AND datname != $2 AND datname LIKE 'sac_test_%';", currentUser.Username, CONFIG.Database.DatabaseName)
if err != nil {
return err
}
query := `SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres' AND datname != $1 AND datname != $2 AND datname LIKE 'sac_test_%';`
rows, err := db.QueryContext(ctx, query, currentUser.Username, CONFIG.Database.DatabaseName)

defer rows.Close()
if err != nil {
return err
}

var wg sync.WaitGroup
defer rows.Close()

for rows.Next() {
var dbName string
var wg sync.WaitGroup

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

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

go func(dbName string) {
defer wg.Done()
wg.Add(1)
go func(dbName string) {
defer wg.Done()

fmt.Printf("Dropping database %s\n", dbName)
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)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // Set a timeout for each drop operation
defer cancel()

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

wg.Wait()
wg.Wait()

return nil
}
return nil
}
3 changes: 2 additions & 1 deletion cli/commands/test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"fmt"
"os/exec"
"sync"
Expand Down Expand Up @@ -105,7 +106,7 @@ func BackendTest() error {

fmt.Println(string(out))

err = CleanTestDBs()
err = CleanTestDBs(context.Background())
if err != nil {
return cli.Exit(err.Error(), 1)
}
Expand Down

0 comments on commit 9849c9a

Please sign in to comment.