Skip to content

Commit

Permalink
Revert "Reuse connections more at butler startup."
Browse files Browse the repository at this point in the history
This reverts commit 5d5cd1b.
  • Loading branch information
TallJimbo committed Dec 13, 2022
1 parent b4dbbea commit 44cf87a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
3 changes: 1 addition & 2 deletions python/lsst/daf/butler/registries/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ def fromConfig(
writeable=writeable,
)
managerTypes = RegistryManagerTypes.fromConfig(config)
with database.session():
managers = managerTypes.loadRepo(database)
managers = managerTypes.loadRepo(database)
if defaults is None:
defaults = RegistryDefaults()
return cls(database, defaults, managers)
Expand Down
61 changes: 29 additions & 32 deletions python/lsst/daf/butler/registry/interfaces/_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ class StaticTablesContext:
which should be the only way it should be constructed.
"""

def __init__(self, db: Database, connection: sqlalchemy.engine.Connection):
def __init__(self, db: Database):
self._db = db
self._foreignKeys: List[Tuple[sqlalchemy.schema.Table, sqlalchemy.schema.ForeignKeyConstraint]] = []
self._inspector = sqlalchemy.inspect(connection)
self._inspector = sqlalchemy.inspect(self._db._engine)
self._tableNames = frozenset(self._inspector.get_table_names(schema=self._db.namespace))
self._initializers: List[Callable[[Database], None]] = []

Expand Down Expand Up @@ -574,33 +574,32 @@ def declareStaticTables(self, *, create: bool) -> Iterator[StaticTablesContext]:
raise ReadOnlyDatabaseError(f"Cannot create tables in read-only database {self}.")
self._metadata = sqlalchemy.MetaData(schema=self.namespace)
try:
with self._transaction_connection() as connection:
context = StaticTablesContext(self, connection)
if create and context._tableNames:
# Looks like database is already initalized, to avoid danger
# of modifying/destroying valid schema we refuse to do
# anything in this case
raise SchemaAlreadyDefinedError(f"Cannot create tables in non-empty database {self}.")
yield context
for table, foreignKey in context._foreignKeys:
table.append_constraint(foreignKey)
if create:
if self.namespace is not None:
if self.namespace not in context._inspector.get_schema_names():
with self._transaction_connection() as connection:
connection.execute(sqlalchemy.schema.CreateSchema(self.namespace))
# In our tables we have columns that make use of sqlalchemy
# Sequence objects. There is currently a bug in sqlalchemy that
# causes a deprecation warning to be thrown on a property of
# the Sequence object when the repr for the sequence is
# created. Here a filter is used to catch these deprecation
# warnings when tables are created.
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=sqlalchemy.exc.SADeprecationWarning)
self._metadata.create_all(self._engine)
# call all initializer methods sequentially
for init in context._initializers:
init(self)
context = StaticTablesContext(self)
if create and context._tableNames:
# Looks like database is already initalized, to avoid danger
# of modifying/destroying valid schema we refuse to do
# anything in this case
raise SchemaAlreadyDefinedError(f"Cannot create tables in non-empty database {self}.")
yield context
for table, foreignKey in context._foreignKeys:
table.append_constraint(foreignKey)
if create:
if self.namespace is not None:
if self.namespace not in context._inspector.get_schema_names():
with self._transaction_connection() as connection:
connection.execute(sqlalchemy.schema.CreateSchema(self.namespace))
# In our tables we have columns that make use of sqlalchemy
# Sequence objects. There is currently a bug in sqlalchemy that
# causes a deprecation warning to be thrown on a property of
# the Sequence object when the repr for the sequence is
# created. Here a filter is used to catch these deprecation
# warnings when tables are created.
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=sqlalchemy.exc.SADeprecationWarning)
self._metadata.create_all(self._engine)
# call all initializer methods sequentially
for init in context._initializers:
init(self)
except BaseException:
self._metadata = None
raise
Expand Down Expand Up @@ -1002,9 +1001,7 @@ def getExistingTable(self, name: str, spec: ddl.TableSpec) -> Optional[sqlalchem
f"the previous definition has {list(table.columns.keys())}."
)
else:
inspector = sqlalchemy.inspect(
self._engine if self._session_connection is None else self._session_connection
)
inspector = sqlalchemy.inspect(self._engine)
if name in inspector.get_table_names(schema=self.namespace):
_checkExistingTableDefinition(name, spec, inspector.get_columns(name, schema=self.namespace))
table = self._convertTableSpec(name, spec, self._metadata)
Expand Down

0 comments on commit 44cf87a

Please sign in to comment.