mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-11-19 16:48:42 +01:00
sqlite-utils tables ... --schema option, closes #104
This commit is contained in:
parent
2b40710e9d
commit
147b52622d
3 changed files with 34 additions and 2 deletions
14
docs/cli.rst
14
docs/cli.rst
|
|
@ -162,7 +162,7 @@ You can list the names of tables in a database using the ``tables`` subcommand::
|
||||||
{"table": "cats"},
|
{"table": "cats"},
|
||||||
{"table": "chickens"}]
|
{"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
|
$ sqlite-utils tables mydb.db --csv --no-headers
|
||||||
dogs
|
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": "Gosh2", "count": 0, "columns": ["c1", "c2", "c3"]},
|
||||||
{"table": "dogs", "count": 2, "columns": ["id", "age", "name"]}]
|
{"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.
|
The ``--nl``, ``--csv`` and ``--table`` options are all available.
|
||||||
|
|
||||||
.. _cli_inserting_data:
|
.. _cli_inserting_data:
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,9 @@ def cli():
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
default=False,
|
default=False,
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--schema", help="Include schema for each table", is_flag=True, default=False,
|
||||||
|
)
|
||||||
def tables(
|
def tables(
|
||||||
path,
|
path,
|
||||||
fts4,
|
fts4,
|
||||||
|
|
@ -88,8 +91,9 @@ def tables(
|
||||||
no_headers,
|
no_headers,
|
||||||
table,
|
table,
|
||||||
fmt,
|
fmt,
|
||||||
columns,
|
|
||||||
json_cols,
|
json_cols,
|
||||||
|
columns,
|
||||||
|
schema,
|
||||||
):
|
):
|
||||||
"""List the tables in the database"""
|
"""List the tables in the database"""
|
||||||
db = sqlite_utils.Database(path)
|
db = sqlite_utils.Database(path)
|
||||||
|
|
@ -98,6 +102,8 @@ def tables(
|
||||||
headers.append("count")
|
headers.append("count")
|
||||||
if columns:
|
if columns:
|
||||||
headers.append("columns")
|
headers.append("columns")
|
||||||
|
if schema:
|
||||||
|
headers.append("schema")
|
||||||
|
|
||||||
def _iter():
|
def _iter():
|
||||||
for name in db.table_names(fts4=fts4, fts5=fts5):
|
for name in db.table_names(fts4=fts4, fts5=fts5):
|
||||||
|
|
@ -110,6 +116,8 @@ def tables(
|
||||||
row.append("\n".join(cols))
|
row.append("\n".join(cols))
|
||||||
else:
|
else:
|
||||||
row.append(cols)
|
row.append(cols)
|
||||||
|
if schema:
|
||||||
|
row.append(db[name].schema)
|
||||||
yield row
|
yield row
|
||||||
|
|
||||||
if table:
|
if table:
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,18 @@ def test_tables_counts_and_columns_csv(db_path):
|
||||||
) == result.output.strip()
|
) == 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(
|
@pytest.mark.parametrize(
|
||||||
"fmt,expected",
|
"fmt,expected",
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue