sqlite-utils create-database command, closes #348

This commit is contained in:
Simon Willison 2022-01-09 12:33:16 -08:00
commit 1d64cd2e5b
3 changed files with 48 additions and 0 deletions

View file

@ -699,6 +699,19 @@ The ``most_common`` and ``least_common`` columns will contain nested JSON arrays
["Condarco", 4288]
]
.. _cli_create_database:
Creating an empty database
==========================
You can create a new empty database file using the ``create-database`` command::
$ sqlite-utils create-database empty.db
To enable :ref:`cli_wal` on the newly created database add the ``--enable-wal`` option::
$ sqlite-utils create-database empty.db --enable-wal
.. _cli_inserting_data:
Inserting JSON data

View file

@ -1052,6 +1052,23 @@ def upsert(
raise click.ClickException(UNICODE_ERROR.format(ex))
@cli.command(name="create-database")
@click.argument(
"path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.option(
"--enable-wal", is_flag=True, help="Enable WAL mode on the created database"
)
def create_table(path, enable_wal):
"Create a new empty database file."
db = sqlite_utils.Database(path)
if enable_wal:
db.enable_wal()
db.vacuum()
@cli.command(name="create-table")
@click.argument(
"path",

View file

@ -2039,3 +2039,21 @@ def test_python_dash_m():
)
assert result.returncode == 0
assert b"Commands for interacting with a SQLite database" in result.stdout
@pytest.mark.parametrize("enable_wal", (False, True))
def test_create_database(tmpdir, enable_wal):
db_path = tmpdir / "test.db"
assert not db_path.exists()
args = ["create-database", str(db_path)]
if enable_wal:
args.append("--enable-wal")
result = CliRunner().invoke(cli.cli, args)
assert result.exit_code == 0, result.output
assert db_path.exists()
assert db_path.read_binary()[:16] == b"SQLite format 3\x00"
db = Database(str(db_path))
if enable_wal:
assert db.journal_mode == "wal"
else:
assert db.journal_mode == "delete"