diff --git a/datasette/cli.py b/datasette/cli.py index 919be065..c59fb6e0 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -388,7 +388,7 @@ async def check_databases(ds): # to confirm they are all usable for database in list(ds.databases.values()): try: - await database.execute_against_connection_in_thread(check_connection) + await database.execute_fn(check_connection) except SpatialiteConnectionProblem: raise click.UsageError( "It looks like you're trying to load a SpatiaLite" diff --git a/datasette/database.py b/datasette/database.py index 48c367ef..0f540e01 100644 --- a/datasette/database.py +++ b/datasette/database.py @@ -101,7 +101,7 @@ class Database: result = e task.reply_queue.sync_q.put(result) - async def execute_against_connection_in_thread(self, fn): + async def execute_fn(self, fn): def in_thread(): conn = getattr(connections, self.name, None) if not conn: @@ -163,9 +163,7 @@ class Database: return Results(rows, False, cursor.description) with trace("sql", database=self.name, sql=sql.strip(), params=params): - results = await self.execute_against_connection_in_thread( - sql_operation_in_thread - ) + results = await self.execute_fn(sql_operation_in_thread) return results @property @@ -223,19 +221,13 @@ class Database: return [r[0] for r in results.rows] async def table_columns(self, table): - return await self.execute_against_connection_in_thread( - lambda conn: table_columns(conn, table) - ) + return await self.execute_fn(lambda conn: table_columns(conn, table)) async def primary_keys(self, table): - return await self.execute_against_connection_in_thread( - lambda conn: detect_primary_keys(conn, table) - ) + return await self.execute_fn(lambda conn: detect_primary_keys(conn, table)) async def fts_table(self, table): - return await self.execute_against_connection_in_thread( - lambda conn: detect_fts(conn, table) - ) + return await self.execute_fn(lambda conn: detect_fts(conn, table)) async def label_column_for_table(self, table): explicit_label_column = self.ds.table_metadata(self.name, table).get( @@ -244,9 +236,7 @@ class Database: if explicit_label_column: return explicit_label_column # If a table has two columns, one of which is ID, then label_column is the other one - column_names = await self.execute_against_connection_in_thread( - lambda conn: table_columns(conn, table) - ) + column_names = await self.execute_fn(lambda conn: table_columns(conn, table)) # Is there a name or title column? name_or_title = [c for c in column_names if c in ("name", "title")] if name_or_title: @@ -261,7 +251,7 @@ class Database: return None async def foreign_keys_for_table(self, table): - return await self.execute_against_connection_in_thread( + return await self.execute_fn( lambda conn: get_outbound_foreign_keys(conn, table) ) @@ -279,9 +269,7 @@ class Database: ) ).rows ] - has_spatialite = await self.execute_against_connection_in_thread( - detect_spatialite - ) + has_spatialite = await self.execute_fn(detect_spatialite) if has_spatialite: # Also hide Spatialite internal tables hidden_tables += [ @@ -329,10 +317,10 @@ class Database: return [r[0] for r in results.rows] async def get_all_foreign_keys(self): - return await self.execute_against_connection_in_thread(get_all_foreign_keys) + return await self.execute_fn(get_all_foreign_keys) async def get_outbound_foreign_keys(self, table): - return await self.execute_against_connection_in_thread( + return await self.execute_fn( lambda conn: get_outbound_foreign_keys(conn, table) )