feat: add db management scripts
This commit is contained in:
40
scripts/reset-db/main.go
Normal file
40
scripts/reset-db/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/pgx-learning/internal/config"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func dropTables(db *pgxpool.Pool, ctx context.Context) error {
|
||||
dropTableSQL := `DROP TABLE IF EXISTS tasks`
|
||||
|
||||
_, err := db.Exec(ctx, dropTableSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error droping tasks table: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
|
||||
db, err := pgxpool.New(ctx, config.Db.Url)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to create connection pool: %v", err)
|
||||
}
|
||||
|
||||
if err := dropTables(db, ctx); err != nil {
|
||||
log.Fatalf("Unexpected error: %v", err)
|
||||
} else {
|
||||
fmt.Println("Database reset completed succesfully")
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user