Add verbose option to import.py
This commit is contained in:
@ -45,12 +45,20 @@ def sqlite_regexp(pattern: str, value: Optional[str]) -> Optional[bool]:
|
|||||||
return re.match(pattern, value) is not None
|
return re.match(pattern, value) is not None
|
||||||
|
|
||||||
|
|
||||||
def import_csv(cur: sqlite3.Cursor, table_name: str) -> None:
|
def import_csv(cur: sqlite3.Cursor, table_name: str, verbose: bool = False) -> None:
|
||||||
rows = utils.csv_parse(f'{DIR}/{table_name}.csv')
|
rows = utils.csv_parse(f'{DIR}/{table_name}.csv')
|
||||||
names = next(rows)
|
names = next(rows)
|
||||||
|
|
||||||
sql = f'INSERT INTO {table_name} ({", ".join(names)}) VALUES ({", ".join(["?"] * len(names))})'
|
sql = f'INSERT INTO {table_name} ({", ".join(names)}) VALUES ({", ".join(["?"] * len(names))})'
|
||||||
print(sql)
|
print(sql)
|
||||||
|
if verbose:
|
||||||
|
inserted = 0
|
||||||
|
for row in rows:
|
||||||
|
print(row)
|
||||||
|
cur.execute(sql, row)
|
||||||
|
inserted += 1
|
||||||
|
print(f'{inserted} inserts')
|
||||||
|
else:
|
||||||
cur.executemany(sql, rows)
|
cur.executemany(sql, rows)
|
||||||
print(f'{cur.rowcount} inserts')
|
print(f'{cur.rowcount} inserts')
|
||||||
|
|
||||||
@ -93,6 +101,8 @@ def main() -> None:
|
|||||||
help='The sqlite database file')
|
help='The sqlite database file')
|
||||||
parser.add_argument('-k', '--keep', action='store_true', default=False,
|
parser.add_argument('-k', '--keep', action='store_true', default=False,
|
||||||
help='Whether the database file should be overwritten or kept')
|
help='Whether the database file should be overwritten or kept')
|
||||||
|
parser.add_argument('-v', '--verbose', action='store_true', default=False,
|
||||||
|
help='Log every inserted row')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
DIR = args.dir
|
DIR = args.dir
|
||||||
@ -121,7 +131,7 @@ def main() -> None:
|
|||||||
cnx.execute("PRAGMA foreign_keys = OFF")
|
cnx.execute("PRAGMA foreign_keys = OFF")
|
||||||
cnx.execute("BEGIN")
|
cnx.execute("BEGIN")
|
||||||
for table in TABLES:
|
for table in TABLES:
|
||||||
import_csv(cnx.cursor(), table)
|
import_csv(cnx.cursor(), table, args.verbose)
|
||||||
if not check_foreign_keys(cnx.cursor()):
|
if not check_foreign_keys(cnx.cursor()):
|
||||||
raise RuntimeError('foreign key constraint failed')
|
raise RuntimeError('foreign key constraint failed')
|
||||||
cnx.execute("COMMIT")
|
cnx.execute("COMMIT")
|
||||||
|
Reference in New Issue
Block a user