db: allow to edit the postgresql.conf file at cluster creation time


Resolves #4.
2 files changed, 36 insertions(+), 5 deletions(-)

M db.py
M tests/test_fixture.py
M db.py +18 -3
@@ 12,11 12,26 @@ def pcall(cmd, args, env=None):
         raise OSError('command %r failed, is it in your PATH ?' % cmd)
 
 
-def setup_local_pg_cluster(request, datadir, pgport):
+def edit_config(confpath, confoptions):
+    with confpath.open('ab') as cfg:
+        for optname, optvalue in confoptions.items():
+            cfg.write('{} = {}\n'.format(optname, optvalue).encode('ascii'))
+
+
+def ensure_cluster(clusterpath, postgresql_conf_options=None):
+    if not clusterpath.exists():
+        pcall('initdb', ['-D', clusterpath.as_posix(), '-E', 'utf-8', '--locale=C'])
+        if postgresql_conf_options is not None:
+            print('Adjusting `{}/postgresql.conf`'.format(clusterpath))
+            edit_config(clusterpath / 'postgresql.conf', postgresql_conf_options)
+
+
+
+def setup_local_pg_cluster(request, datadir, pgport,
+                           postgresql_conf_options=None):
     " create (if missing) a local cluster to use for the tests "
     dbpath = Path(datadir, 'pgdb')
-    if not dbpath.exists():
-        pcall('initdb', ['-D', dbpath.as_posix(), '-E', 'utf-8', '--locale=C'])
+    ensure_cluster(dbpath, postgresql_conf_options)
     env = os.environ.copy()
     sockdir = tempfile.mkdtemp(prefix='pgsocket')
     options = '-k %s -p %s' % (sockdir, pgport)

          
M tests/test_fixture.py +18 -2
@@ 1,11 1,13 @@ 
+import shutil
 from pathlib import Path
 
 from sqlalchemy import MetaData, Table, Column, Integer, String
 
 from pytest_sa_pg.fixture import engine_fixture
+from pytest_sa_pg.db import ensure_cluster
 
 
-TESTDIR = Path(__file__).parent
+DATADIR = Path(__file__).parent / 'data'
 META = MetaData()
 
 T = Table('Person', META,

          
@@ 13,8 15,22 @@ T = Table('Person', META,
           Column('name', String)
 )
 
-engine = engine_fixture(META, TESTDIR, 5433)
+engine = engine_fixture(META, DATADIR, 5433)
 
 
 def test_engine(engine):
     assert engine.execute(T.insert().values(name='Babar'))
+
+
+def test_adjusted_cluster():
+    clusterpath = DATADIR / 'othercluster' / 'pgdb'
+    shutil.rmtree(clusterpath)
+    ensure_cluster(clusterpath,
+                   postgresql_conf_options={
+                       'max_locks_per_transaction': 42
+                   }
+    )
+    cfgpath = clusterpath / 'postgresql.conf'
+    assert cfgpath.read_text().strip().endswith(
+        'max_locks_per_transaction = 42'
+    )