Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ Let's walk through the code. First you create new node:
node = testgres.get_new_node('master')
```

`master` is a node's name, not the database's name. The name matters if you're testing something like replication. Function `get_new_node()` only creates directory structure in `/tmp` for cluster. After that, we have to initialize the PostgreSQL cluster:
or:

```python
node = testgres.get_new_node('master', '/path/to/base')
```

`master` is a node's name, not the database's name. The name matters if you're testing something like replication. Function `get_new_node()` only creates directory structure in specified directory (or in '/tmp' if we did not specify base directory) for cluster. After that, we have to initialize the PostgreSQL cluster:

```python
node.init()
Expand Down
25 changes: 18 additions & 7 deletions testgres/testgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,16 @@ def close(self):

class PostgresNode(object):

def __init__(self, name, port):
def __init__(self, name, port, base_dir=None):
self.name = name
self.host = '127.0.0.1'
self.port = port
self.base_dir = tempfile.mkdtemp()
os.makedirs(self.logs_dir)
if base_dir is None:
self.base_dir = tempfile.mkdtemp()
else:
self.base_dir = base_dir
if not os.path.exists(self.logs_dir):
os.makedirs(self.logs_dir)
self.working = False

@property
Expand Down Expand Up @@ -175,6 +179,14 @@ def get_bin_path(self, filename):
def init(self, allows_streaming=False):
""" Performs initdb """

postgres_conf = os.path.join(self.data_dir, "postgresql.conf")

if os.path.isfile(postgres_conf):
# if data directory exists then we don't need reinit it
with open(postgres_conf, "a") as conf:
conf.write("port = %s\n" % self.port)
return self

# initialize cluster
os.makedirs(self.data_dir)
initdb = self.get_bin_path("initdb")
Expand All @@ -189,7 +201,6 @@ def init(self, allows_streaming=False):
raise ClusterException("Cluster initialization failed")

# add parameters to config file
postgres_conf = os.path.join(self.data_dir, "postgresql.conf")
with open(postgres_conf, "a") as conf:
conf.write(
"fsync = off\n"
Expand Down Expand Up @@ -258,7 +269,7 @@ def append_conf(self, filename, string):

A new line is not automatically appended to the string
"""
config_name = "%s/%s" % (self.data_dir, filename)
config_name = os.path.join(self.data_dir, filename)
with open(config_name, "a") as conf:
conf.write(''.join([string, '\n']))

Expand Down Expand Up @@ -496,7 +507,7 @@ def version_to_num(version):
return num


def get_new_node(name):
def get_new_node(name, base_dir=None):
global registered_nodes
global last_assigned_port

Expand All @@ -517,7 +528,7 @@ def get_new_node(name):
# socket.SOCK_STREAM,
# socket.getprotobyname("tcp"))

node = PostgresNode(name, port)
node = PostgresNode(name, port, base_dir)
registered_nodes.append(node)
last_assigned_port = port

Expand Down