from cassandra.cluster import Cluster
import os

def db_teardown(config):
    print(" == DB TEARDOWN SCRIPT == ")

    print(f"Attempting Cassandra connection @ {config['cassandra_addresses']}:{config['cassandra_port']}")
    cluster  = Cluster(config['cassandra_addresses'], port=config['cassandra_port'])
    session = cluster.connect(config['cassandra_keyspace'])
    print(f"Connection OK")

    for folder_name in os.listdir(config['setup_tables_dir']):
        print(f"Dropping TABLE '{folder_name}'")
        with open(f"{config['setup_tables_dir']}/{folder_name}/DROP.sql") as table_drop:
            session.execute(table_drop.read())

    with open(f"{config['setup_keyspace_dir']}/DROP.sql") as keyspace_drop:
        print(f"Dropping KEYSPACE '{config['cassandra_keyspace']}'")
        session.execute(keyspace_drop.read())

    print("Done!")
    cluster.shutdown()