Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclusive DB access #15029

Merged
merged 11 commits into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion conan/internal/cache/db/table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sqlite3
from collections import namedtuple
import threading
from collections import defaultdict, namedtuple
from contextlib import contextmanager
from typing import Tuple, List, Optional

Expand All @@ -10,20 +11,25 @@ class BaseDbTable:
row_type: namedtuple = None
columns: namedtuple = None
unique_together: tuple = None
_lock: threading.Lock = None
_lock_storage = defaultdict(threading.Lock)

def __init__(self, filename):
self.filename = filename
column_names: List[str] = [it[0] for it in self.columns_description]
self.row_type = namedtuple('_', column_names)
self.columns = self.row_type(*column_names)
self._lock = self._lock_storage[self.filename]

@contextmanager
def db_connection(self):
assert self._lock.acquire(timeout=10), "Conan failed to acquire database lock"
connection = sqlite3.connect(self.filename, isolation_level=None, timeout=10)
try:
yield connection
finally:
connection.close()
self._lock.release()

def create_table(self):
def field(name, typename, nullable=False, check_constraints: Optional[List] = None,
Expand Down