'datasette serve -i immutable.db' option, refs #419

pull/439/head
Simon Willison 2019-03-17 16:25:15 -07:00
rodzic 6f6d0ff2b4
commit 47032636b5
3 zmienionych plików z 22 dodań i 4 usunięć

Wyświetl plik

@ -121,6 +121,7 @@ class Datasette:
def __init__(
self,
files,
immutables=None,
cache_headers=True,
cors=False,
inspect_data=None,
@ -133,7 +134,9 @@ class Datasette:
config=None,
version_note=None,
):
self.files = files
immutables = immutables or []
self.files = files + immutables
self.immutables = set(immutables)
if not self.files:
self.files = [MEMORY]
elif memory:
@ -322,7 +325,7 @@ class Datasette:
raise Exception("Multiple files with same stem %s" % name)
try:
with sqlite3.connect(
"file:{}?immutable=1".format(path), uri=True
"file:{}?mode=ro".format(path), uri=True
) as conn:
self.prepare_connection(conn)
self._inspect[name] = {
@ -426,8 +429,13 @@ class Datasette:
if info["file"] == ":memory:":
conn = sqlite3.connect(":memory:")
else:
# mode=ro or immutable=1?
if info["file"] in self.immutables:
qs = "immutable=1"
else:
qs = "mode=ro"
conn = sqlite3.connect(
"file:{}?immutable=1".format(info["file"]),
"file:{}?{}".format(info["file"], qs),
uri=True,
check_same_thread=False,
)

Wyświetl plik

@ -267,6 +267,13 @@ def package(
@cli.command()
@click.argument("files", type=click.Path(exists=True), nargs=-1)
@click.option(
"-i",
"--immutable",
type=click.Path(exists=True),
help="Database files to open in immutable mode",
multiple=True,
)
@click.option(
"-h", "--host", default="127.0.0.1", help="host for server, defaults to 127.0.0.1"
)
@ -332,6 +339,7 @@ def package(
)
def serve(
files,
immutable,
host,
port,
debug,
@ -376,9 +384,10 @@ def serve(
if metadata:
metadata_data = json.loads(metadata.read())
click.echo("Serve! files={} on port {}".format(files, port))
click.echo("Serve! files={} (immutables={}) on port {}".format(files, immutable, port))
ds = Datasette(
files,
immutables=immutable,
cache_headers=not debug and not reload,
cors=cors,
inspect_data=inspect_data,

Wyświetl plik

@ -5,6 +5,7 @@ Usage: datasette serve [OPTIONS] [FILES]...
Serve up specified SQLite database files with a web UI
Options:
-i, --immutable PATH Database files to open in immutable mode
-h, --host TEXT host for server, defaults to 127.0.0.1
-p, --port INTEGER port for server, defaults to 8001
--debug Enable debug mode - useful for development