#!/usr/bin/env python

import os
import subprocess


def psql_sql(sql):
    p = subprocess.Popen(["psql", "-h", "postgres", "-U", "chroma", "-tAc {}".format(sql)], stdout=subprocess.PIPE)
    return p.stdout.read()


cmd = psql_sql(
    "SELECT 'CREATE DATABASE grafana' WHERE NOT EXISTS (SELECT * FROM pg_database WHERE datname = 'grafana')"
)
if cmd:
    psql_sql(cmd)
    print("Grafana database created!")
else:
    print("Grafana database already exists.")
