sandbox/db_dropper_backend.py
2024-11-02 14:14:15 +03:00

35 lines
1 KiB
Python

import psycopg2
import subprocess
import os
# Clean all untracked migrations
os.system("git clean -xf */migrations/")
# establishing the connection
conn = psycopg2.connect(
database="postgres", user='console_user', password='console_user_password', host='127.0.0.1', port='5432'
)
conn.autocommit = True
# Creating a cursor object using the cursor() method
cursor = conn.cursor()
# Preparing query to create a database
sql = '''DROP DATABASE console_db;'''
# Creating a database
try:
cursor.execute(sql)
print("Database dropped successfully ->")
except psycopg2.ProgrammingError:
print("Database license does not exists")
sql = '''CREATE database console_db;'''
cursor.execute(sql)
print("-> Database created successfully")
sql = '''grant all privileges on database console_db to console_user;'''
cursor.execute(sql)
print("Database user license_user has all rights to database license")
# Closing the connection
conn.close()
subprocess.run(['python3', 'manage.py', 'makemigrations'])
subprocess.run(['python3', 'manage.py', 'migrate'])