From 147b52622d68473ba6ab184657258d8576100b05 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 1 May 2020 10:09:36 -0700 Subject: [PATCH] sqlite-utils tables ... --schema option, closes #104 --- docs/cli.rst | 14 +++++++++++++- sqlite_utils/cli.py | 10 +++++++++- tests/test_cli.py | 12 ++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index f09c47e..be9970f 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -162,7 +162,7 @@ You can list the names of tables in a database using the ``tables`` subcommand:: {"table": "cats"}, {"table": "chickens"}] -You can output this list in CSV using the ``-csv`` option:: +You can output this list in CSV using the ``--csv`` option:: $ sqlite-utils tables mydb.db --csv --no-headers dogs @@ -188,6 +188,18 @@ Use ``--columns`` to include a list of columns in each table:: {"table": "Gosh2", "count": 0, "columns": ["c1", "c2", "c3"]}, {"table": "dogs", "count": 2, "columns": ["id", "age", "name"]}] +Use ``--schema`` to include the schema of each table:: + + $ sqlite-utils tables dogs.db --schema --table + table schema + ------- ----------------------------------------------- + Gosh CREATE TABLE Gosh (c1 text, c2 text, c3 text) + Gosh2 CREATE TABLE Gosh2 (c1 text, c2 text, c3 text) + dogs CREATE TABLE [dogs] ( + [id] INTEGER, + [age] INTEGER, + [name] TEXT) + The ``--nl``, ``--csv`` and ``--table`` options are all available. .. _cli_inserting_data: diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 54667e5..f53bcd0 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -77,6 +77,9 @@ def cli(): is_flag=True, default=False, ) +@click.option( + "--schema", help="Include schema for each table", is_flag=True, default=False, +) def tables( path, fts4, @@ -88,8 +91,9 @@ def tables( no_headers, table, fmt, - columns, json_cols, + columns, + schema, ): """List the tables in the database""" db = sqlite_utils.Database(path) @@ -98,6 +102,8 @@ def tables( headers.append("count") if columns: headers.append("columns") + if schema: + headers.append("schema") def _iter(): for name in db.table_names(fts4=fts4, fts5=fts5): @@ -110,6 +116,8 @@ def tables( row.append("\n".join(cols)) else: row.append(cols) + if schema: + row.append(db[name].schema) yield row if table: diff --git a/tests/test_cli.py b/tests/test_cli.py index 52c50da..0bcefff 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -72,6 +72,18 @@ def test_tables_counts_and_columns_csv(db_path): ) == result.output.strip() +def test_tables_schema(db_path): + db = Database(db_path) + with db.conn: + db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)]) + result = CliRunner().invoke(cli.cli, ["tables", "--schema", db_path]) + assert ( + '[{"table": "Gosh", "schema": "CREATE TABLE Gosh (c1 text, c2 text, c3 text)"},\n' + ' {"table": "Gosh2", "schema": "CREATE TABLE Gosh2 (c1 text, c2 text, c3 text)"},\n' + ' {"table": "lots", "schema": "CREATE TABLE [lots] (\\n [id] INTEGER,\\n [age] INTEGER\\n)"}]' + ) == result.output.strip() + + @pytest.mark.parametrize( "fmt,expected", [