From c1b26eed03f60c3e317550053a3832b7ad62e588 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 22 Sep 2021 13:45:37 -0700 Subject: [PATCH] sqlite-utils memory handles files with same filename, closes #325 --- docs/cli.rst | 4 ++++ sqlite_utils/cli.py | 8 +++++++- tests/test_cli_memory.py | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/cli.rst b/docs/cli.rst index 0cc20dd..d2a4d84 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -281,6 +281,10 @@ The in-memory tables will be named after the files without their extensions. The $ sqlite-utils memory example.csv "select * from t" +If two files have the same name they will be assigned a numeric suffix:: + + $ sqlite-utils memory foo/data.csv bar/data.csv "select * from data_2" + To read from standard input, use either ``-`` or ``stdin`` as the filename - then use ``stdin`` or ``t`` or ``t1`` as the table name:: $ cat example.csv | sqlite-utils memory - "select * from stdin" diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 5217965..b29314e 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -1275,6 +1275,7 @@ def memory( if (dump or save or schema or analyze) and not paths: paths = [sql] sql = None + stem_counts = {} for i, path in enumerate(paths): # Path may have a :format suffix if ":" in path and path.rsplit(":", 1)[-1].upper() in Format.__members__: @@ -1287,7 +1288,12 @@ def memory( csv_table = "stdin" else: csv_path = pathlib.Path(path) - csv_table = csv_path.stem + stem = csv_path.stem + if stem_counts.get(stem): + csv_table = "{}_{}".format(stem, stem_counts[stem]) + else: + csv_table = stem + stem_counts[stem] = stem_counts.get(stem, 1) + 1 csv_fp = csv_path.open("rb") rows, format_used = rows_from_file(csv_fp, format=format, encoding=encoding) tracker = None diff --git a/tests/test_cli_memory.py b/tests/test_cli_memory.py index 08566e3..a8b3a18 100644 --- a/tests/test_cli_memory.py +++ b/tests/test_cli_memory.py @@ -241,3 +241,27 @@ def test_memory_analyze(): " Blank rows: 0\n\n" " Distinct values: 2\n\n" ) + + +def test_memory_two_files_with_same_stem(tmpdir): + (tmpdir / "one").mkdir() + (tmpdir / "two").mkdir() + one = tmpdir / "one" / "data.csv" + two = tmpdir / "two" / "data.csv" + one.write_text("id,name\n1,Cleo\n2,Bants", encoding="utf-8") + two.write_text("id,name\n3,Blue\n4,Lila", encoding="utf-8") + result = CliRunner().invoke(cli.cli, ["memory", str(one), str(two), "", "--schema"]) + assert result.exit_code == 0 + assert result.output == ( + 'CREATE TABLE "data" (\n' + " [id] INTEGER,\n" + " [name] TEXT\n" + ");\n" + "CREATE VIEW t1 AS select * from [data];\n" + "CREATE VIEW t AS select * from [data];\n" + 'CREATE TABLE "data_2" (\n' + " [id] INTEGER,\n" + " [name] TEXT\n" + ");\n" + "CREATE VIEW t2 AS select * from [data_2];\n" + )