#!/usr/bin/env python3

import hra.db as db
from sqlalchemy import exc, update
import sys
import hra.config  as config

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--drop", help="Drop all existing tables", action="store_true")

args = parser.parse_args()

print(config.SQLALCHEMY_DATABASE_URI)

if args.drop:
    if config.WEB_FLAVOR != "devel":
        if input('Write "DROP ALL": ') != "DROP ALL":
            print("Wrong, nothing to do.")
            sys.exit(1)
    rs = db.get_session().execute("""
    SELECT
      'DROP TABLE IF EXISTS "' || tablename || '" CASCADE;'
    from
      pg_tables WHERE schemaname = 'public';""")

    for row in rs:
        x = db.get_session().execute(row[0])
        print(row, x)
    db.get_session().commit()

db.metadata.bind = db.get_engine()
db.metadata.create_all()