Commit d3e00b88 authored by Paulo V C Medeiros's avatar Paulo V C Medeiros
Browse files

Fix ignored table if not in first input file

parent 2fe3ea0f
......@@ -4,6 +4,7 @@ import shutil
import sqlite3
from pathlib import Path
import logging
import re
def merge_sqlite_dbs(input_files, output_file, overwrite):
......@@ -54,9 +55,34 @@ def merge_sqlite_dbs(input_files, output_file, overwrite):
cur.execute("SELECT name FROM current_db.sqlite_master WHERE type='table';")
all_tables = cur.fetchall()
for table in all_tables:
table_name = table[0]
logger.debug(" > '%s' table", table_name)
try:
# Create destination table with same signature as origin if it doesn't exist
# (i) Get sql employed to create the table in the origin DB
cur.execute(
"SELECT sql FROM current_db.sqlite_master WHERE type='table' AND name='%s'"
% (table_name)
)
# (ii) Create the table
create_table_sql = re.sub(
"create table",
"CREATE TABLE IF NOT EXISTS",
cur.fetchall()[0][0],
flags=re.IGNORECASE,
)
cur.execute(create_table_sql)
except IndexError:
raise RuntimeError(
"Could not get sql for creation of table '%s' from input database."
% (table_name)
)
# Append data assuming same columns in the same position
logger.debug(" > '%s' table", table)
sql = "INSERT INTO {0} SELECT * FROM current_db.[{0}];".format(table[0])
sql = "INSERT INTO {0} SELECT * FROM current_db.[{0}];".format(
table_name
)
cur.execute(sql)
conn.commit()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment