Skip to content

Commit

Permalink
using DROP DATABASE to cleare it
Browse files Browse the repository at this point in the history
  • Loading branch information
bstarynk committed Apr 5, 2020
1 parent d75ebeb commit 2843184
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions hcv_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,10 @@ hcv_initialize_database(const std::string&uri, bool cleardata)
pqxx::work firsttransact(*hcv_dbconn);
if (cleardata)
{
// https://dba.stackexchange.com/a/154075/204015
firsttransact.exec0(R"cleardatabase(
DO
$$
DECLARE
l_stmt text;
BEGIN
SELECT 'truncate ' || string_agg(format('%I.%I', schemaname, tablename), ',')
INTO l_stmt
FROM pg_tables
WHERE schemaname IN ('public');
EXECUTE l_stmt;
END;
$$
)cleardatabase");
// bad idea https://dba.stackexchange.com/a/154075/204015
// better idea https://www.postgresql.org/docs/current/sql-dropdatabase.html
firsttransact.exec0(std::string("DROP DATABASE IF EXISTS ")
+ hcv_dbconn->dbname());
HCV_SYSLOGOUT(LOG_NOTICE, "hcv_initialize_database cleared database");
}
///================ add something into PostGreSQL log
Expand Down

0 comments on commit 2843184

Please sign in to comment.