diff --git a/datasette/cli.py b/datasette/cli.py index e83de93a..7b415fc6 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -477,6 +477,11 @@ def uninstall(packages, yes): is_flag=True, help="Create database files if they do not exist", ) +@click.option( + "--create-wal", + is_flag=True, + help="Create database files if they do not exist, in WAL mode", +) @click.option( "--crossdb", is_flag=True, @@ -532,6 +537,7 @@ def serve( pdb, open_browser, create, + create_wal, crossdb, nolock, ssl_keyfile, @@ -618,12 +624,14 @@ def serve( elif len(directories) == 1: kwargs["config_dir"] = pathlib.Path(directories[0]) - # Verify list of files, create if needed (and --create) + # Verify list of files, create if needed (and --create / --create-wal) for file in file_paths: if not pathlib.Path(file).exists(): - if create: + if create or create_wal: conn = sqlite3.connect(file) conn.execute("vacuum") + if create_wal: + conn.execute("pragma journal_mode=wal") conn.close() else: raise click.ClickException( diff --git a/docs/cli-reference.rst b/docs/cli-reference.rst index db903e8e..6912d091 100644 --- a/docs/cli-reference.rst +++ b/docs/cli-reference.rst @@ -132,6 +132,8 @@ Use ``--internal PATH`` or the ``DATASETTE_INTERNAL`` environment variable to pe --pdb Launch debugger on any errors -o, --open Open Datasette in your web browser --create Create database files if they do not exist + --create-wal Create database files if they do not exist, in + WAL mode --crossdb Enable cross-database joins using the /_memory database --nolock Ignore locking, open locked files in read-only diff --git a/tests/test_cli.py b/tests/test_cli.py index fbd4a8a9..bdab6d0c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -171,6 +171,7 @@ def test_metadata_yaml(): nolock=False, open_browser=False, create=False, + create_wal=False, ssl_keyfile=None, ssl_certfile=None, return_instance=True, @@ -462,6 +463,37 @@ def test_serve_create(tmpdir): assert db_path.exists() +def test_serve_create_wal(tmpdir): + runner = CliRunner() + db_path = tmpdir / "does_not_exist_yet.db" + assert not db_path.exists() + result = runner.invoke( + cli, [str(db_path), "--create-wal", "--get", "/-/databases.json"] + ) + assert result.exit_code == 0, result.output + assert db_path.exists() + # The new database should be in WAL mode + conn = sqlite3.connect(str(db_path)) + try: + assert conn.execute("pragma journal_mode").fetchone()[0] == "wal" + finally: + conn.close() + + +def test_serve_create_does_not_set_wal(tmpdir): + runner = CliRunner() + db_path = tmpdir / "does_not_exist_yet.db" + result = runner.invoke( + cli, [str(db_path), "--create", "--get", "/-/databases.json"] + ) + assert result.exit_code == 0, result.output + conn = sqlite3.connect(str(db_path)) + try: + assert conn.execute("pragma journal_mode").fetchone()[0] != "wal" + finally: + conn.close() + + @pytest.mark.parametrize("argument", ("-c", "--config")) @pytest.mark.parametrize("format_", ("json", "yaml")) def test_serve_config(tmpdir, argument, format_):