Skip to content

Commit 3ba0454

Browse files
committed
use transaction for read-only operations
1 parent 8406391 commit 3ba0454

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

pum/schema_migrations.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ def exists(self, connection: psycopg.Connection) -> bool:
7474
"schema": psycopg.sql.Literal(self.config.config.pum.migration_table_schema),
7575
}
7676

77-
cursor = SqlContent(query).execute(connection, parameters=parameters)
78-
result = cursor._pum_results[0] if cursor._pum_results else None
79-
return result[0] if result else False
77+
with connection.transaction():
78+
cursor = SqlContent(query).execute(connection, parameters=parameters)
79+
result = cursor._pum_results[0] if cursor._pum_results else None
80+
return result[0] if result else False
8081

8182
def exists_in_other_schemas(self, connection: psycopg.Connection) -> list[str]:
8283
"""Check if the schema_migrations information table exists in other schemas.
@@ -99,8 +100,9 @@ def exists_in_other_schemas(self, connection: psycopg.Connection) -> list[str]:
99100
parameters = {
100101
"schema": psycopg.sql.Literal(self.config.config.pum.migration_table_schema),
101102
}
102-
cursor = SqlContent(query).execute(connection, parameters=parameters)
103-
return [row[0] for row in (cursor._pum_results or [])]
103+
with connection.transaction():
104+
cursor = SqlContent(query).execute(connection, parameters=parameters)
105+
return [row[0] for row in (cursor._pum_results or [])]
104106

105107
def create(
106108
self,
@@ -344,13 +346,14 @@ def baseline(self, connection: psycopg.Connection) -> packaging.version.Version:
344346
"table": self.migration_table_identifier,
345347
}
346348

347-
cursor = SqlContent(query).execute(connection, parameters=parameters)
348-
row = cursor._pum_results[0] if cursor._pum_results else None
349-
if row is None:
350-
raise PumSchemaMigrationNoBaselineError(
351-
f"Baseline version not found in the {self.migration_table_identifier_str} table."
352-
)
353-
return packaging.version.parse(row[0])
349+
with connection.transaction():
350+
cursor = SqlContent(query).execute(connection, parameters=parameters)
351+
row = cursor._pum_results[0] if cursor._pum_results else None
352+
if row is None:
353+
raise PumSchemaMigrationNoBaselineError(
354+
f"Baseline version not found in the {self.migration_table_identifier_str} table."
355+
)
356+
return packaging.version.parse(row[0])
354357

355358
def migration_details(self, connection: psycopg.Connection, version: str | None = None) -> dict:
356359
"""Return the migration details from the migration table.
@@ -401,13 +404,14 @@ def migration_details(self, connection: psycopg.Connection, version: str | None
401404
"version": psycopg.sql.Literal(version),
402405
}
403406

404-
cursor = SqlContent(query).execute(connection, parameters=parameters)
405-
row = cursor._pum_results[0] if cursor._pum_results else None
406-
if row is None:
407-
raise PumSchemaMigrationError(
408-
f"Migration details not found for version {version} in the {self.migration_table_identifier_str} table."
409-
)
410-
return dict(zip([desc[0] for desc in cursor._pum_description], row, strict=False))
407+
with connection.transaction():
408+
cursor = SqlContent(query).execute(connection, parameters=parameters)
409+
row = cursor._pum_results[0] if cursor._pum_results else None
410+
if row is None:
411+
raise PumSchemaMigrationError(
412+
f"Migration details not found for version {version} in the {self.migration_table_identifier_str} table."
413+
)
414+
return dict(zip([desc[0] for desc in cursor._pum_description], row, strict=False))
411415

412416
def compare(self, connection: psycopg.Connection) -> int:
413417
"""Compare the migrations details in the database to the changelogs in the source.

0 commit comments

Comments
 (0)