43 lines
713 B
Go
43 lines
713 B
Go
package pgutil
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
)
|
|
|
|
const dropTablesQueryQuery = `
|
|
SELECT 'DROP TABLE IF EXISTS "' || tablename || '" CASCADE;'
|
|
FROM
|
|
pg_tables
|
|
WHERE
|
|
schemaname='public'`
|
|
|
|
// Deletes all tables in the database. Useful for testing.
|
|
func DropAllTables(db *sql.DB) error {
|
|
rows, err := db.Query(dropTablesQueryQuery)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
queries := []string{}
|
|
for rows.Next() {
|
|
var s string
|
|
if err := rows.Scan(&s); err != nil {
|
|
return err
|
|
}
|
|
queries = append(queries, s)
|
|
}
|
|
|
|
if len(queries) > 0 {
|
|
log.Printf("DROPPING ALL (%d) TABLES", len(queries))
|
|
}
|
|
|
|
for _, query := range queries {
|
|
if _, err := db.Exec(query); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|