kopia lustrzana https://github.com/simonw/datasette
datasette serve --create option, closes #1135
rodzic
4c25b035b2
commit
fe86d85308
|
@ -27,6 +27,7 @@ from .utils import (
|
|||
StaticMount,
|
||||
ValueAsBooleanError,
|
||||
)
|
||||
from .utils.sqlite import sqlite3
|
||||
from .utils.testing import TestClient
|
||||
from .version import __version__
|
||||
|
||||
|
@ -299,7 +300,7 @@ def uninstall(packages, yes):
|
|||
|
||||
|
||||
@cli.command()
|
||||
@click.argument("files", type=click.Path(exists=True), nargs=-1)
|
||||
@click.argument("files", type=click.Path(), nargs=-1)
|
||||
@click.option(
|
||||
"-i",
|
||||
"--immutable",
|
||||
|
@ -401,6 +402,11 @@ def uninstall(packages, yes):
|
|||
is_flag=True,
|
||||
help="Open Datasette in your web browser",
|
||||
)
|
||||
@click.option(
|
||||
"--create",
|
||||
is_flag=True,
|
||||
help="Create database files if they do not exist",
|
||||
)
|
||||
def serve(
|
||||
files,
|
||||
immutable,
|
||||
|
@ -424,6 +430,7 @@ def serve(
|
|||
help_config,
|
||||
pdb,
|
||||
open_browser,
|
||||
create,
|
||||
return_instance=False,
|
||||
):
|
||||
"""Serve up specified SQLite database files with a web UI"""
|
||||
|
@ -486,6 +493,18 @@ def serve(
|
|||
kwargs["config_dir"] = pathlib.Path(files[0])
|
||||
files = []
|
||||
|
||||
# Verify list of files, create if needed (and --create)
|
||||
for file in files:
|
||||
if not pathlib.Path(file).exists():
|
||||
if create:
|
||||
sqlite3.connect(file).execute("vacuum")
|
||||
else:
|
||||
raise click.ClickException(
|
||||
"Invalid value for '[FILES]...': Path '{}' does not exist.".format(
|
||||
file
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
ds = Datasette(files, **kwargs)
|
||||
except SpatialiteNotFound:
|
||||
|
|
|
@ -40,4 +40,5 @@ Options:
|
|||
--help-config Show available config options
|
||||
--pdb Launch debugger on any errors
|
||||
-o, --open Open Datasette in your web browser
|
||||
--create Create database files if they do not exist
|
||||
--help Show this message and exit.
|
||||
|
|
|
@ -146,6 +146,7 @@ def test_metadata_yaml():
|
|||
help_config=False,
|
||||
pdb=False,
|
||||
open_browser=False,
|
||||
create=False,
|
||||
return_instance=True,
|
||||
)
|
||||
client = _TestClient(ds)
|
||||
|
@ -221,3 +222,21 @@ def test_sql_errors_logged_to_stderr(ensure_eventloop):
|
|||
result = runner.invoke(cli, ["--get", "/:memory:.json?sql=select+blah"])
|
||||
assert result.exit_code == 1
|
||||
assert "sql = 'select blah', params = {}: no such column: blah\n" in result.stderr
|
||||
|
||||
|
||||
def test_serve_create(ensure_eventloop, tmpdir):
|
||||
runner = CliRunner()
|
||||
db_path = tmpdir / "does_not_exist_yet.db"
|
||||
assert not db_path.exists()
|
||||
result = runner.invoke(
|
||||
cli, [str(db_path), "--create", "--get", "/-/databases.json"]
|
||||
)
|
||||
assert result.exit_code == 0, result.output
|
||||
databases = json.loads(result.output)
|
||||
assert {
|
||||
"name": "does_not_exist_yet",
|
||||
"is_mutable": True,
|
||||
"is_memory": False,
|
||||
"hash": None,
|
||||
}.items() <= databases[0].items()
|
||||
assert db_path.exists()
|
||||
|
|
Ładowanie…
Reference in New Issue