wip
This commit is contained in:
42
pgutil/dropall.go
Normal file
42
pgutil/dropall.go
Normal file
@@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user