diff --git a/python_modules/libraries/dagster-postgres/dagster_postgres/event_log/event_log.py b/python_modules/libraries/dagster-postgres/dagster_postgres/event_log/event_log.py --- a/python_modules/libraries/dagster-postgres/dagster_postgres/event_log/event_log.py +++ b/python_modules/libraries/dagster-postgres/dagster_postgres/event_log/event_log.py @@ -68,9 +68,9 @@ ) self._secondary_index_cache = {} - with self.connect() as conn: - table_names = db.inspect(self._engine).get_table_names() - if "event_logs" not in table_names: + table_names = db.inspect(self._engine).get_table_names() + if "event_logs" not in table_names: + with self.connect() as conn: alembic_config = get_alembic_config(__file__) retry_pg_creation_fn(lambda: SqlEventLogStorageMetadata.create_all(conn)) diff --git a/python_modules/libraries/dagster-postgres/dagster_postgres/run_storage/run_storage.py b/python_modules/libraries/dagster-postgres/dagster_postgres/run_storage/run_storage.py --- a/python_modules/libraries/dagster-postgres/dagster_postgres/run_storage/run_storage.py +++ b/python_modules/libraries/dagster-postgres/dagster_postgres/run_storage/run_storage.py @@ -50,10 +50,10 @@ self._index_migration_cache = {} table_names = db.inspect(self._engine).get_table_names() - with self.connect() as conn: - # Stamp and create tables if there's no previously stamped revision and the main table - # doesn't exist (since we used to not stamp postgres storage when it was first created) - if "runs" not in table_names: + # Stamp and create tables if there's no previously stamped revision and the main table + # doesn't exist (since we used to not stamp postgres storage when it was first created) + if "runs" not in table_names: + with self.connect() as conn: alembic_config = get_alembic_config(__file__) retry_pg_creation_fn(lambda: RunStorageSqlMetadata.create_all(conn)) diff --git a/python_modules/libraries/dagster-postgres/dagster_postgres/schedule_storage/schedule_storage.py b/python_modules/libraries/dagster-postgres/dagster_postgres/schedule_storage/schedule_storage.py --- a/python_modules/libraries/dagster-postgres/dagster_postgres/schedule_storage/schedule_storage.py +++ b/python_modules/libraries/dagster-postgres/dagster_postgres/schedule_storage/schedule_storage.py @@ -47,10 +47,9 @@ ) table_names = db.inspect(self._engine).get_table_names() - - with self.connect() as conn: - missing_main_table = "schedules" not in table_names and "jobs" not in table_names - if missing_main_table: + missing_main_table = "schedules" not in table_names and "jobs" not in table_names + if missing_main_table: + with self.connect() as conn: alembic_config = get_alembic_config(__file__) retry_pg_creation_fn(lambda: ScheduleStorageSqlMetadata.create_all(conn))