--nolock mode to ignore locked files, closes #1744

This commit is contained in:
Simon Willison 2022-05-17 12:40:05 -07:00
commit 3508bf7875
5 changed files with 15 additions and 1 deletions

View file

@ -213,6 +213,7 @@ class Datasette:
config_dir=None, config_dir=None,
pdb=False, pdb=False,
crossdb=False, crossdb=False,
nolock=False,
): ):
assert config_dir is None or isinstance( assert config_dir is None or isinstance(
config_dir, Path config_dir, Path
@ -238,6 +239,7 @@ class Datasette:
self.databases = collections.OrderedDict() self.databases = collections.OrderedDict()
self._refresh_schemas_lock = asyncio.Lock() self._refresh_schemas_lock = asyncio.Lock()
self.crossdb = crossdb self.crossdb = crossdb
self.nolock = nolock
if memory or crossdb or not self.files: if memory or crossdb or not self.files:
self.add_database(Database(self, is_memory=True), name="_memory") self.add_database(Database(self, is_memory=True), name="_memory")
# memory_name is a random string so that each Datasette instance gets its own # memory_name is a random string so that each Datasette instance gets its own

View file

@ -452,6 +452,11 @@ def uninstall(packages, yes):
is_flag=True, is_flag=True,
help="Enable cross-database joins using the /_memory database", help="Enable cross-database joins using the /_memory database",
) )
@click.option(
"--nolock",
is_flag=True,
help="Ignore locking, open locked files in read-only mode",
)
@click.option( @click.option(
"--ssl-keyfile", "--ssl-keyfile",
help="SSL key file", help="SSL key file",
@ -486,6 +491,7 @@ def serve(
open_browser, open_browser,
create, create,
crossdb, crossdb,
nolock,
ssl_keyfile, ssl_keyfile,
ssl_certfile, ssl_certfile,
return_instance=False, return_instance=False,
@ -545,6 +551,7 @@ def serve(
version_note=version_note, version_note=version_note,
pdb=pdb, pdb=pdb,
crossdb=crossdb, crossdb=crossdb,
nolock=nolock,
) )
# if files is a single directory, use that as config_dir= # if files is a single directory, use that as config_dir=

View file

@ -89,6 +89,8 @@ class Database:
# mode=ro or immutable=1? # mode=ro or immutable=1?
if self.is_mutable: if self.is_mutable:
qs = "?mode=ro" qs = "?mode=ro"
if self.ds.nolock:
qs += "&nolock=1"
else: else:
qs = "?immutable=1" qs = "?immutable=1"
assert not (write and not self.is_mutable) assert not (write and not self.is_mutable)

View file

@ -115,6 +115,7 @@ datasette serve --help
--create Create database files if they do not exist --create Create database files if they do not exist
--crossdb Enable cross-database joins using the /_memory --crossdb Enable cross-database joins using the /_memory
database database
--nolock Ignore locking, open locked files in read-only mode
--ssl-keyfile TEXT SSL key file --ssl-keyfile TEXT SSL key file
--ssl-certfile TEXT SSL certificate file --ssl-certfile TEXT SSL certificate file
--help Show this message and exit. --help Show this message and exit.

View file

@ -56,7 +56,9 @@ like so:
:: ::
datasette ~/Library/Application\ Support/Google/Chrome/Default/History datasette ~/Library/Application\ Support/Google/Chrome/Default/History --nolock
The `--nolock` option ignores any file locks. This is safe as Datasette will open the file in read-only mode.
Now visiting http://localhost:8001/History/downloads will show you a web Now visiting http://localhost:8001/History/downloads will show you a web
interface to browse your downloads data: interface to browse your downloads data: