-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
58 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 |
---|---|---|
@@ -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{ | ||
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 | ||
} |
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