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
76 changes: 0 additions & 76 deletions pgtoolkit/ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ class Status(enum.IntEnum):

class AbstractPGCtl(abc.ABC):
bindir: Path
version: int

@cached_property
def pg_ctl(self) -> Path:
Expand All @@ -161,9 +160,6 @@ def pg_ctl(self) -> Path:
raise OSError("pg_ctl executable not found")
return value

def version_cmd(self) -> list[str]:
return [str(self.pg_ctl), "--version"]

def init_cmd(self, datadir: Path | str, **opts: str | Literal[True]) -> list[str]:
cmd = [str(self.pg_ctl), "init"] + ["-D", str(datadir)]
options = _args_to_opts(opts)
Expand Down Expand Up @@ -270,10 +266,6 @@ def __init__(
).stdout.strip()
self.bindir = Path(bindir)
self.run_command = run_command
version = run_command(
self.version_cmd(), check=True, capture_output=True
).stdout.strip()
self.version = num_version(version)

def init(
self, datadir: Path | str, **opts: str | Literal[True]
Expand Down Expand Up @@ -434,10 +426,6 @@ async def get(
).stdout.strip()
bindir = Path(bindir)
self = cls(bindir, run_command)
version = (
await run_command(self.version_cmd(), check=True, capture_output=True)
).stdout.strip()
self.version = num_version(version)
return self

async def init(
Expand Down Expand Up @@ -513,67 +501,3 @@ def parse_control_data(lines: Sequence[str]) -> dict[str, str]:
if m:
controldata[m.group(1).strip()] = m.group(2).strip()
return controldata


def num_version(text_version: str) -> int:
"""Return PostgreSQL numeric version as defined by LibPQ PQserverVersion

>>> num_version('pg_ctl (PostgreSQL) 9.6.3')
90603
>>> num_version('pg_ctl (PostgreSQL) 9.2.0')
90200
>>> num_version('pg_ctl (PostgreSQL) 11.10')
110010
>>> num_version('pg_ctl (PostgreSQL) 11.1')
110001
>>> num_version("pg_ctl (PostgreSQL) 14devel")
140000
>>> num_version("pg_ctl (PostgreSQL) 9.6devel")
90600
>>> num_version("pg_ctl (PostgreSQL) 9.6rc1")
90600
"""
res = re.match(
r"pg_ctl \(\w+\) ([0-9]+)\.([0-9]+)(?:\.([0-9]+))?",
text_version,
)
if res is not None:
rmatch = res.group(1)
if int(res.group(1)) < 10:
rmatch += res.group(2).rjust(2, "0")
if res.group(3) is not None:
rmatch += res.group(3).rjust(2, "0")
else:
return num_dev_version(text_version)
else:
rmatch += res.group(2).rjust(4, "0")
pg_num_version = int(rmatch)
return pg_num_version
return num_dev_version(text_version)


def num_dev_version(text_version: str) -> int:
"""Return PostgreSQL numeric version as defined by LibPQ PQserverVersion

>>> num_dev_version("pg_ctl (PostgreSQL) 14devel")
140000
>>> num_dev_version("pg_ctl (PostgreSQL) 13beta1")
130000
>>> num_dev_version("pg_ctl (PostgreSQL) 13rc1")
130000
>>> num_dev_version("pg_ctl (PostgreSQL) 9.6devel")
90600
"""
res = re.match(
r"pg_ctl \(\w+\) ([0-9]+)(?:\.([0-9]+))?(devel|beta[0-9]+|rc[0-9]+)",
text_version,
)
if not res:
raise Exception(f"Undefined PostgreSQL version: {text_version}")
rmatch = res.group(1)
if res.group(2) is not None:
rmatch += res.group(2).rjust(2, "0") + "00"
else:
rmatch += "0000"
pg_num_version = int(rmatch)
return pg_num_version
9 changes: 0 additions & 9 deletions tests/test_ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ def pgctl(bindir: Path) -> ctl.PGCtl:
return c


def test_version(pgctl: ctl.AsyncPGCtl) -> None:
assert pgctl.version == 110010


def test_init_cmd(pgctl: ctl.PGCtl) -> None:
assert pgctl.init_cmd(
"data",
Expand Down Expand Up @@ -170,11 +166,6 @@ async def run_command(args: Sequence[str], **kwargs: Any) -> ctl.CompletedProces
return c


@pytest.mark.asyncio
async def test_version_async(apgctl: ctl.AsyncPGCtl) -> None:
assert apgctl.version == 110010


@pytest.mark.parametrize(
"rc, status",
[
Expand Down
10 changes: 0 additions & 10 deletions tests/test_ctl_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ def tmp_port() -> int:
def test_start_stop_status_restart_reload(
initdb: tuple[Path, Path, Path], pgctl: ctl.PGCtl, tmp_port: int
) -> None:
from psycopg2 import connect

datadir, __, pidpath = initdb
assert pgctl.status("invalid") == ctl.Status.unspecified_datadir
assert pgctl.status(str(datadir)) == ctl.Status.not_running
Expand All @@ -113,9 +111,6 @@ def test_start_stop_status_restart_reload(
assert pidpath.exists()
pid1 = pidpath.read_text()

connection = connect(dbname="postgres", host="0.0.0.0", port=tmp_port)
assert connection.info.server_version == pgctl.version

assert pgctl.status(str(datadir)) == ctl.Status.running
pgctl.restart(str(datadir), mode="immediate", wait=2)
pid2 = pidpath.read_text()
Expand All @@ -134,8 +129,6 @@ def test_start_stop_status_restart_reload(
async def test_start_stop_status_restart_reload_async(
ainitdb: tuple[Path, Path, Path], apgctl: ctl.AsyncPGCtl, tmp_port: int
) -> None:
from psycopg2 import connect

datadir, __, pidpath = ainitdb
assert (await apgctl.status("invalid")) == ctl.Status.unspecified_datadir
assert (await apgctl.status(str(datadir))) == ctl.Status.not_running
Expand All @@ -144,9 +137,6 @@ async def test_start_stop_status_restart_reload_async(
assert pidpath.exists()
pid1 = pidpath.read_text()

connection = connect(dbname="postgres", host="0.0.0.0", port=tmp_port)
assert connection.info.server_version == apgctl.version

assert (await apgctl.status(str(datadir))) == ctl.Status.running
await apgctl.restart(str(datadir), mode="immediate", wait=2)
pid2 = pidpath.read_text()
Expand Down