#!/usr/local/bin/python
import pymysql

from common.loggers import logger
from settings import db


def create_db():
    conn = pymysql.connect(host=db.HOST,
                           user=db.USER,
                           password=db.PASSWORD,
                           port=db.PORT,
                           db=None)
    logger.info(f'Connected to {db.HOST}')

    with conn.cursor() as cur:
        cur.execute(f'CREATE DATABASE IF NOT EXISTS `{db.DATABASE}`')
    conn.close()

    logger.info(f'Created database {db.DATABASE}')

    conn = pymysql.connect(host=db.HOST,
                           user=db.USER,
                           password=db.PASSWORD,
                           port=db.PORT,
                           db=db.DATABASE)
    with conn.cursor() as cur:
        cur.execute(f'GRANT ALL PRIVILEGES ON `{db.DATABASE}` TO `{db.USER}`@`%`')

    logger.info(f'Granted all privileges to {db.USER}')
    conn.close()


if __name__ == '__main__':
    create_db()
