mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
'datasette serve -i immutable.db' option, refs #419
This commit is contained in:
parent
6f6d0ff2b4
commit
47032636b5
3 changed files with 22 additions and 4 deletions
|
|
@ -121,6 +121,7 @@ class Datasette:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
files,
|
files,
|
||||||
|
immutables=None,
|
||||||
cache_headers=True,
|
cache_headers=True,
|
||||||
cors=False,
|
cors=False,
|
||||||
inspect_data=None,
|
inspect_data=None,
|
||||||
|
|
@ -133,7 +134,9 @@ class Datasette:
|
||||||
config=None,
|
config=None,
|
||||||
version_note=None,
|
version_note=None,
|
||||||
):
|
):
|
||||||
self.files = files
|
immutables = immutables or []
|
||||||
|
self.files = files + immutables
|
||||||
|
self.immutables = set(immutables)
|
||||||
if not self.files:
|
if not self.files:
|
||||||
self.files = [MEMORY]
|
self.files = [MEMORY]
|
||||||
elif memory:
|
elif memory:
|
||||||
|
|
@ -322,7 +325,7 @@ class Datasette:
|
||||||
raise Exception("Multiple files with same stem %s" % name)
|
raise Exception("Multiple files with same stem %s" % name)
|
||||||
try:
|
try:
|
||||||
with sqlite3.connect(
|
with sqlite3.connect(
|
||||||
"file:{}?immutable=1".format(path), uri=True
|
"file:{}?mode=ro".format(path), uri=True
|
||||||
) as conn:
|
) as conn:
|
||||||
self.prepare_connection(conn)
|
self.prepare_connection(conn)
|
||||||
self._inspect[name] = {
|
self._inspect[name] = {
|
||||||
|
|
@ -426,8 +429,13 @@ class Datasette:
|
||||||
if info["file"] == ":memory:":
|
if info["file"] == ":memory:":
|
||||||
conn = sqlite3.connect(":memory:")
|
conn = sqlite3.connect(":memory:")
|
||||||
else:
|
else:
|
||||||
|
# mode=ro or immutable=1?
|
||||||
|
if info["file"] in self.immutables:
|
||||||
|
qs = "immutable=1"
|
||||||
|
else:
|
||||||
|
qs = "mode=ro"
|
||||||
conn = sqlite3.connect(
|
conn = sqlite3.connect(
|
||||||
"file:{}?immutable=1".format(info["file"]),
|
"file:{}?{}".format(info["file"], qs),
|
||||||
uri=True,
|
uri=True,
|
||||||
check_same_thread=False,
|
check_same_thread=False,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -267,6 +267,13 @@ def package(
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.argument("files", type=click.Path(exists=True), nargs=-1)
|
@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(
|
@click.option(
|
||||||
"-h", "--host", default="127.0.0.1", help="host for server, defaults to 127.0.0.1"
|
"-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(
|
def serve(
|
||||||
files,
|
files,
|
||||||
|
immutable,
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
debug,
|
debug,
|
||||||
|
|
@ -376,9 +384,10 @@ def serve(
|
||||||
if metadata:
|
if metadata:
|
||||||
metadata_data = json.loads(metadata.read())
|
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(
|
ds = Datasette(
|
||||||
files,
|
files,
|
||||||
|
immutables=immutable,
|
||||||
cache_headers=not debug and not reload,
|
cache_headers=not debug and not reload,
|
||||||
cors=cors,
|
cors=cors,
|
||||||
inspect_data=inspect_data,
|
inspect_data=inspect_data,
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ Usage: datasette serve [OPTIONS] [FILES]...
|
||||||
Serve up specified SQLite database files with a web UI
|
Serve up specified SQLite database files with a web UI
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
-i, --immutable PATH Database files to open in immutable mode
|
||||||
-h, --host TEXT host for server, defaults to 127.0.0.1
|
-h, --host TEXT host for server, defaults to 127.0.0.1
|
||||||
-p, --port INTEGER port for server, defaults to 8001
|
-p, --port INTEGER port for server, defaults to 8001
|
||||||
--debug Enable debug mode - useful for development
|
--debug Enable debug mode - useful for development
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue