Fix restore_sql_file bug: table already exists
parent
b60c2af32f
commit
0b2abc692c
|
@ -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.")
|
||||
|
|
Loading…
Reference in New Issue