diff --git a/docs/cli.rst b/docs/cli.rst index e07c73d..36ff029 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -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 diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 1bc797e..315b162 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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", diff --git a/tests/test_cli.py b/tests/test_cli.py index 5a9ca65..ca5c820 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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"