Lock column updates

This commit is contained in:
Friedrich Lindenberg 2020-03-10 16:28:35 -05:00
parent 894819fe99
commit a513888824

View File

@ -56,17 +56,22 @@ class Table(object):
"""Get a dictionary of all columns and their case mapping.""" """Get a dictionary of all columns and their case mapping."""
if not self.exists: if not self.exists:
return {} return {}
if self._columns is None: with self.db.lock:
# Initialise the table if it doesn't exist if self._columns is None:
table = self.table # Initialise the table if it doesn't exist
self._columns = {} table = self.table
for column in table.columns: self._columns = {}
name = normalize_column_name(column.name) for column in table.columns:
key = normalize_column_key(name) name = normalize_column_name(column.name)
if key in self._columns: key = normalize_column_key(name)
log.warning("Duplicate column: %s", name) if key in self._columns:
self._columns[key] = name log.warning("Duplicate column: %s", name)
return self._columns self._columns[key] = name
return self._columns
def _flush_metadata(self):
with self.db.lock:
self._columns = None
@property @property
def columns(self): def columns(self):
@ -305,7 +310,7 @@ class Table(object):
def _reflect_table(self): def _reflect_table(self):
"""Load the tables definition from the database.""" """Load the tables definition from the database."""
with self.db.lock: with self.db.lock:
self._columns = None self._flush_metadata()
try: try:
self._table = SQLATable(self.name, self._table = SQLATable(self.name,
self.db.metadata, self.db.metadata,
@ -323,7 +328,7 @@ class Table(object):
def _sync_table(self, columns): def _sync_table(self, columns):
"""Lazy load, create or adapt the table structure in the database.""" """Lazy load, create or adapt the table structure in the database."""
self._columns = None self._flush_metadata()
if self._table is None: if self._table is None:
# Load an existing table from the database. # Load an existing table from the database.
self._reflect_table() self._reflect_table()
@ -520,7 +525,7 @@ class Table(object):
self._threading_warn() self._threading_warn()
self.table.drop(self.db.executable, checkfirst=True) self.table.drop(self.db.executable, checkfirst=True)
self._table = None self._table = None
self._columns = None self._flush_metadata()
def has_index(self, columns): def has_index(self, columns):
"""Check if an index exists to cover the given ``columns``.""" """Check if an index exists to cover the given ``columns``."""