-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwipeout.py
68 lines (55 loc) · 2.24 KB
/
wipeout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import json
import logging
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def load_config():
try:
with open('server_config.json', 'r') as config_file:
return json.load(config_file)
except FileNotFoundError:
logger.error("Config file not found. Please ensure server_config.json exists.")
return None
except json.JSONDecodeError:
logger.error("Invalid JSON in config file. Please check server_config.json.")
return None
def wipeout_database():
config = load_config()
if not config:
return
db_config = config['database']
try:
# Connect to the default PostgreSQL database
conn = psycopg2.connect(
host=db_config['host'],
user=db_config['username'],
password=db_config['password'],
dbname='postgres' # Connect to the default 'postgres' database
)
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
with conn.cursor() as cursor:
# Check if the database exists
cursor.execute("SELECT 1 FROM pg_database WHERE datname = %s", (db_config['database_name'],))
exists = cursor.fetchone()
if exists:
# Terminate all connections to the database
cursor.execute(f"""
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = %s AND pid <> pg_backend_pid()
""", (db_config['database_name'],))
# Drop the database
cursor.execute(f"DROP DATABASE {db_config['database_name']}")
logger.info(f"Database '{db_config['database_name']}' has been deleted.")
else:
logger.info(f"Database '{db_config['database_name']}' does not exist.")
except psycopg2.Error as e:
logger.error(f"An error occurred: {e}")
finally:
if conn:
conn.close()
logger.info("Database connection closed.")
if __name__ == "__main__":
wipeout_database()