simonw / git-history

Tools for analyzing Git history using SQLite
Apache License 2.0
191 stars 18 forks source link

`--sql-log` debug option #35

Open simonw opened 2 years ago

simonw commented 2 years ago

Found this useful while working on #33 - I should turn it into a documented feature:

+@click.option(
+    "--trace-file",
+    type=click.File("w"),
+    help="Store executed SQL in this file",
+)
 @click.option(
     "--silent",
     is_flag=True,
@@ -145,6 +150,7 @@ def file(
     ignore_duplicate_ids,
     wal,
     debug,
+    trace_file,
     silent,
 ):
     "Analyze the history of a specific file and write it to SQLite"
@@ -159,7 +165,15 @@ def file(
             "Cannot use --start-at and --start-after at the same time"
         )

-    db = sqlite_utils.Database(database)
+    if trace_file:
+        db = sqlite_utils.Database(
+            database,
+            tracer=lambda sql, params: trace_file.write(
+                "{} - {}\n".format(sql, params)
+            ),
+        )
+    else:
+        db = sqlite_utils.Database(database)
     if wal:
         db.enable_wal()