1414from packaging .version import Version , parse
1515from psycopg import AsyncCursor , Connection , Cursor
1616
17+ from pytest_postgresql .config import validate_create_strategy
1718from pytest_postgresql .loader import build_loader , sql_async
1819from pytest_postgresql .retry import retry , retry_async
1920
@@ -36,6 +37,7 @@ class BaseDatabaseJanitor:
3637 isolation_level : "psycopg.IsolationLevel | None"
3738 autocommit : bool
3839 version : Version | None
40+ create_strategy : str | None
3941
4042 def __init__ (
4143 self ,
@@ -52,6 +54,7 @@ def __init__(
5254 isolation_level : "psycopg.IsolationLevel | None" = None ,
5355 autocommit : bool = False ,
5456 connection_timeout : int = 60 ,
57+ create_strategy : str | None = None ,
5558 ) -> None :
5659 """Initialize janitor.
5760
@@ -75,6 +78,9 @@ def __init__(
7578 affects the loader connection, not the janitor's admin cursor.
7679 :param connection_timeout: how long to retry connection before
7780 raising a TimeoutError
81+ :param create_strategy: strategy for cloning a test database from its
82+ template: ``wal_log``, ``file_copy``, or ``clone``. None leaves the
83+ choice to PostgreSQL. Does not affect template creation.
7884 """
7985 self .user = user
8086 self .password = password
@@ -87,6 +93,7 @@ def __init__(
8793 self ._connection_timeout = connection_timeout
8894 self .isolation_level = isolation_level
8995 self .autocommit = autocommit
96+ self .create_strategy = validate_create_strategy (create_strategy )
9097 if version is not None :
9198 warnings .warn (
9299 "version argument is deprecated and will be removed in a future release" ,
@@ -109,8 +116,20 @@ def _build_create_database_sql(self) -> sql.Composed:
109116 query = query + sql .SQL (" TEMPLATE {}" ).format (sql .Identifier (self .template_dbname ))
110117 if self .is_template ():
111118 query = query + sql .SQL (" IS_TEMPLATE = true" )
119+ elif self .template_dbname and self .create_strategy is not None :
120+ strategy = "WAL_LOG" if self .create_strategy == "wal_log" else "FILE_COPY"
121+ query = query + sql .SQL (" STRATEGY {}" ).format (sql .SQL (strategy ))
112122 return query
113123
124+ def _build_file_copy_method_sql (self ) -> sql .SQL | None :
125+ """Build a session setting for the explicit file-copy methods."""
126+ if self .template_dbname and not self .is_template ():
127+ if self .create_strategy == "file_copy" :
128+ return sql .SQL ("SET file_copy_method = copy" )
129+ if self .create_strategy == "clone" :
130+ return sql .SQL ("SET file_copy_method = clone" )
131+ return None
132+
114133
115134class DatabaseJanitor (BaseDatabaseJanitor ):
116135 """Manage database state for specific tasks."""
@@ -123,6 +142,9 @@ def init(self) -> None:
123142 # Otherwise, Creating database from template will fail
124143 self ._terminate_connection (cur , self .template_dbname )
125144 query = self ._build_create_database_sql ()
145+ file_copy_method_sql = self ._build_file_copy_method_sql ()
146+ if file_copy_method_sql is not None :
147+ cur .execute (file_copy_method_sql )
126148 cur .execute (query )
127149
128150 @staticmethod
@@ -231,6 +253,9 @@ async def init(self) -> None:
231253 # Otherwise, Creating database from template will fail
232254 await self ._terminate_connection (cur , self .template_dbname )
233255 query = self ._build_create_database_sql ()
256+ file_copy_method_sql = self ._build_file_copy_method_sql ()
257+ if file_copy_method_sql is not None :
258+ await cur .execute (file_copy_method_sql )
234259 await cur .execute (query )
235260
236261 @staticmethod
0 commit comments