Fix restore_sql_file bug: table already exists

pull/98/head
Meng Li 2020-08-18 14:18:30 -04:00
parent b60c2af32f
commit 0b2abc692c
1 changed files with 6 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import pandas as pd
import configparser
import subprocess
import os
# read database credentials
@ -8,14 +9,14 @@ config = configparser.ConfigParser()
config.read(snakemake.input["db_credentials"])
# bash command to create table and restore tables from sql file
checkdb_cmd = "mysql -h " + config[group]["host"] + " -u " + config[group]["user"] + " -p" + config[group]["password"] + " -e \"use " + config[group]["database"] + "\""
checkdb_cmd = "mysql -h " + config[group]["host"] + " -u " + config[group]["user"] + " -p" + config[group]["password"] + " -e use " + config[group]["database"]
create_cmd = "mysql -h " + config[group]["host"] + " -u " + config[group]["user"] + " -p" + config[group]["password"] + " -e \"CREATE DATABASE IF NOT EXISTS " + config[group]["database"] + ";\""
restore_cmd = "mysql -h " + config[group]["host"] + " -u " + config[group]["user"] + " -p" + config[group]["password"] + " " + config[group]["database"] + " < data/external/" + config[group]["database"] + ".sql"
try:
os.system(checkdb_cmd)
except:
print(config[group]["database"] + " DB already exists.")
else:
subprocess.run(checkdb_cmd.split(), check = True)
except subprocess.CalledProcessError:
os.system(create_cmd)
os.system(restore_cmd)
else:
raise ValueError(config[group]["database"] + " DB already exists.")