netherframe.backends.postgresql 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. netherframe_backends_postgresql-0.2.1/LICENSE +21 -0
  2. netherframe_backends_postgresql-0.2.1/PKG-INFO +138 -0
  3. netherframe_backends_postgresql-0.2.1/README.md +114 -0
  4. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/__init__.py +59 -0
  5. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/connection_settings.py +40 -0
  6. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph.py +2511 -0
  7. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_authorization.py +102 -0
  8. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_checkpoints.py +239 -0
  9. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_completion.py +66 -0
  10. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_convergence.py +103 -0
  11. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_invalidation.py +115 -0
  12. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_lease_queue.py +236 -0
  13. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_planning_reads.py +97 -0
  14. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_publication.py +373 -0
  15. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_retention.py +141 -0
  16. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_reverse_edges.py +143 -0
  17. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_schema.py +2038 -0
  18. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/graph_transfers.py +128 -0
  19. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/postgresql.py +1207 -0
  20. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/postgresql_backend_bootstrap.py +139 -0
  21. netherframe_backends_postgresql-0.2.1/netherframe/backends/postgresql/py.typed +0 -0
  22. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/__init__.py +4 -0
  23. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/connection_settings.py +4 -0
  24. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph.py +4 -0
  25. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_authorization.py +4 -0
  26. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_checkpoints.py +4 -0
  27. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_completion.py +4 -0
  28. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_convergence.py +4 -0
  29. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_invalidation.py +4 -0
  30. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_lease_queue.py +4 -0
  31. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_planning_reads.py +4 -0
  32. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_publication.py +4 -0
  33. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_retention.py +4 -0
  34. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_reverse_edges.py +4 -0
  35. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_schema.py +4 -0
  36. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/graph_transfers.py +4 -0
  37. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/postgresql.py +4 -0
  38. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/postgresql_backend_bootstrap.py +4 -0
  39. netherframe_backends_postgresql-0.2.1/nf/backends/postgresql/py.typed +0 -0
  40. netherframe_backends_postgresql-0.2.1/pyproject.toml +54 -0
  41. netherframe_backends_postgresql-0.2.1/pyproject.toml.orig +56 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Netherframe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,138 @@
1
+ Metadata-Version: 2.4
2
+ Name: netherframe.backends.postgresql
3
+ Version: 0.2.1
4
+ Summary: PostgreSQL backend implementation for Netherframe
5
+ Author: Sourabh Cheedella
6
+ Author-email: Sourabh Cheedella <cheedella.sourabh@gmail.com>
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Programming Language :: Python :: 3.14
12
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
13
+ Classifier: Topic :: System :: Systems Administration
14
+ Requires-Dist: anyio>=4.16.0
15
+ Requires-Dist: netherframe-core>=0.10.0
16
+ Requires-Dist: asyncpg>=0.32.0
17
+ Requires-Dist: pytest>=9.2.0 ; extra == 'dev'
18
+ Requires-Dist: testcontainers>=4.15.0 ; extra == 'dev'
19
+ Requires-Python: >=3.14
20
+ Project-URL: Homepage, https://netherframe.arsenal.sh
21
+ Project-URL: Repository, https://github.com/arsenal-sh/netherframe
22
+ Provides-Extra: dev
23
+ Description-Content-Type: text/markdown
24
+
25
+ # Netherframe PostgreSQL Backend
26
+
27
+ PostgreSQL backend implementation for Netherframe: state in PostgreSQL, with a state lock and conditional writes for hybrid execution.
28
+
29
+ ## Features
30
+
31
+ - **PostgreSQL State Storage**: Store infrastructure state in PostgreSQL with JSONB columns for optimal querying
32
+ - **State Locking**: one lock per state (`state_id`), held by a hybrid run from planning through approval to the end of apply (see [Locking](#locking))
33
+ - **ACID Transactions**: Leverage PostgreSQL's ACID guarantees for state consistency
34
+ - **Connection Pooling**: Efficient connection management for high-performance operations
35
+ - **Hybrid Mode Support**: Works with both synthesis mode (Terraform) and hybrid execution mode
36
+ - **Conditional Writes**: a write replaces only the state version its run read (the state row is locked `FOR UPDATE` while compared and written)
37
+ - **Force-Unlock and Checkpoints**: a crashed run's lock is released by its ID (`nf state force-unlock`); pre-apply checkpoints are rows of their own
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ pip install netherframe.backends.postgresql
43
+ ```
44
+
45
+ ## Configuration
46
+
47
+ ```python
48
+ from nf.backends.postgresql import PostgreSQLBackend
49
+
50
+ # Basic configuration
51
+ backend = PostgreSQLBackend(
52
+ nf_scope=stack,
53
+ host="localhost",
54
+ port=5432,
55
+ database="netherframe_state",
56
+ username="nf_user",
57
+ password="your_password",
58
+ )
59
+
60
+ # Advanced configuration with SSL and custom schema
61
+ backend = PostgreSQLBackend(
62
+ nf_scope=stack,
63
+ host="db.example.com",
64
+ port=5432,
65
+ database="infrastructure",
66
+ username="nf_user",
67
+ password="your_password",
68
+ schema="netherframe",
69
+ ssl_mode="require",
70
+ table_prefix="nf_",
71
+ pool_size=10,
72
+ max_overflow=20,
73
+ )
74
+ ```
75
+
76
+ ## Schema
77
+
78
+ The backend creates the following tables:
79
+
80
+ - `nf_states`: Main state storage with JSONB data
81
+ - `nf_locks`: State locks, one per `state_id`
82
+ - `nf_resource_locks`: Locks taken through the resource-lock API (see below); hybrid execution does not use them
83
+
84
+ ## Locking
85
+
86
+ Hybrid execution locks the whole state, as Terraform does; it does not lock
87
+ individual resources:
88
+
89
+ - `apply` takes the lock on its `state_id` before planning and holds it through
90
+ the approval prompt to the end of the apply; `plan` holds it while planning.
91
+ - A second run on the same state does not wait: it fails to take the lock
92
+ (`Failed to acquire state lock`) and logs who holds it. Concurrent applies on
93
+ one state are therefore serialized, never interleaved.
94
+ - Every state write is conditional: it replaces only the state version
95
+ (`serial` and `lineage`) the run read, with the row locked `FOR UPDATE` while
96
+ it is compared and written. A saved plan is rejected if state changed since it
97
+ was created.
98
+ - The lock expires `lock_timeout` seconds (default 300) after its holder last
99
+ renewed it; a running holder renews it in the background. `nf state
100
+ force-unlock` releases the lock of a crashed run at once.
101
+
102
+ Changes that must be applied independently, at the same time, need separate
103
+ states: give each independently deployed app its own `state_id`.
104
+
105
+ ### Resource-Lock API
106
+
107
+ The backend also has `acquire_resource_lock`, `release_resource_lock`,
108
+ `release_all_resource_locks` and `resource_lock_context`: shared and exclusive
109
+ locks on a resource id, with timeouts, stored in `nf_resource_locks` (see
110
+ `example_usage.py`). Netherframe's hybrid execution never calls them, and they
111
+ do not let two runs apply to one state at once. They are only useful to
112
+ coordinate your own tooling.
113
+
114
+ ## Settings
115
+
116
+ Each connection setting is a flag of `nf`, else a key of `[tool.nf.config]` in
117
+ `pyproject.toml`, else the backend's argument, else the default. Nothing is read from the
118
+ environment.
119
+
120
+ - `--postgresql-host` / `postgresql_host`: Database host (default: localhost)
121
+ - `--postgresql-port` / `postgresql_port`: Database port (default: 5432)
122
+ - `--postgresql-database` / `postgresql_database`: Database name
123
+ - `--postgresql-username` / `postgresql_username`: Database username
124
+ - `--postgresql-ssl-mode` / `postgresql_ssl_mode`: SSL mode (disable, allow, prefer, require, verify-ca, verify-full)
125
+ - `--postgresql-password-file` / `postgresql_password_file`: File holding the database password
126
+ (a mounted secret). The password is never a flag. In code, `PostgreSQLBackend` also takes
127
+ `password=`, and `PostgreSQLGraphBackend` takes `password_file=` or `password_source=` (a
128
+ callable).
129
+
130
+ ```toml
131
+ [tool.nf.config]
132
+ postgresql_host = "db.internal"
133
+ postgresql_database = "netherframe"
134
+ postgresql_username = "nf_apply"
135
+ postgresql_password_file = "/run/secrets/postgresql-password"
136
+ ```
137
+
138
+ See the [Settings reference](../../docs/reference/settings.md).
@@ -0,0 +1,114 @@
1
+ # Netherframe PostgreSQL Backend
2
+
3
+ PostgreSQL backend implementation for Netherframe: state in PostgreSQL, with a state lock and conditional writes for hybrid execution.
4
+
5
+ ## Features
6
+
7
+ - **PostgreSQL State Storage**: Store infrastructure state in PostgreSQL with JSONB columns for optimal querying
8
+ - **State Locking**: one lock per state (`state_id`), held by a hybrid run from planning through approval to the end of apply (see [Locking](#locking))
9
+ - **ACID Transactions**: Leverage PostgreSQL's ACID guarantees for state consistency
10
+ - **Connection Pooling**: Efficient connection management for high-performance operations
11
+ - **Hybrid Mode Support**: Works with both synthesis mode (Terraform) and hybrid execution mode
12
+ - **Conditional Writes**: a write replaces only the state version its run read (the state row is locked `FOR UPDATE` while compared and written)
13
+ - **Force-Unlock and Checkpoints**: a crashed run's lock is released by its ID (`nf state force-unlock`); pre-apply checkpoints are rows of their own
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ pip install netherframe.backends.postgresql
19
+ ```
20
+
21
+ ## Configuration
22
+
23
+ ```python
24
+ from nf.backends.postgresql import PostgreSQLBackend
25
+
26
+ # Basic configuration
27
+ backend = PostgreSQLBackend(
28
+ nf_scope=stack,
29
+ host="localhost",
30
+ port=5432,
31
+ database="netherframe_state",
32
+ username="nf_user",
33
+ password="your_password",
34
+ )
35
+
36
+ # Advanced configuration with SSL and custom schema
37
+ backend = PostgreSQLBackend(
38
+ nf_scope=stack,
39
+ host="db.example.com",
40
+ port=5432,
41
+ database="infrastructure",
42
+ username="nf_user",
43
+ password="your_password",
44
+ schema="netherframe",
45
+ ssl_mode="require",
46
+ table_prefix="nf_",
47
+ pool_size=10,
48
+ max_overflow=20,
49
+ )
50
+ ```
51
+
52
+ ## Schema
53
+
54
+ The backend creates the following tables:
55
+
56
+ - `nf_states`: Main state storage with JSONB data
57
+ - `nf_locks`: State locks, one per `state_id`
58
+ - `nf_resource_locks`: Locks taken through the resource-lock API (see below); hybrid execution does not use them
59
+
60
+ ## Locking
61
+
62
+ Hybrid execution locks the whole state, as Terraform does; it does not lock
63
+ individual resources:
64
+
65
+ - `apply` takes the lock on its `state_id` before planning and holds it through
66
+ the approval prompt to the end of the apply; `plan` holds it while planning.
67
+ - A second run on the same state does not wait: it fails to take the lock
68
+ (`Failed to acquire state lock`) and logs who holds it. Concurrent applies on
69
+ one state are therefore serialized, never interleaved.
70
+ - Every state write is conditional: it replaces only the state version
71
+ (`serial` and `lineage`) the run read, with the row locked `FOR UPDATE` while
72
+ it is compared and written. A saved plan is rejected if state changed since it
73
+ was created.
74
+ - The lock expires `lock_timeout` seconds (default 300) after its holder last
75
+ renewed it; a running holder renews it in the background. `nf state
76
+ force-unlock` releases the lock of a crashed run at once.
77
+
78
+ Changes that must be applied independently, at the same time, need separate
79
+ states: give each independently deployed app its own `state_id`.
80
+
81
+ ### Resource-Lock API
82
+
83
+ The backend also has `acquire_resource_lock`, `release_resource_lock`,
84
+ `release_all_resource_locks` and `resource_lock_context`: shared and exclusive
85
+ locks on a resource id, with timeouts, stored in `nf_resource_locks` (see
86
+ `example_usage.py`). Netherframe's hybrid execution never calls them, and they
87
+ do not let two runs apply to one state at once. They are only useful to
88
+ coordinate your own tooling.
89
+
90
+ ## Settings
91
+
92
+ Each connection setting is a flag of `nf`, else a key of `[tool.nf.config]` in
93
+ `pyproject.toml`, else the backend's argument, else the default. Nothing is read from the
94
+ environment.
95
+
96
+ - `--postgresql-host` / `postgresql_host`: Database host (default: localhost)
97
+ - `--postgresql-port` / `postgresql_port`: Database port (default: 5432)
98
+ - `--postgresql-database` / `postgresql_database`: Database name
99
+ - `--postgresql-username` / `postgresql_username`: Database username
100
+ - `--postgresql-ssl-mode` / `postgresql_ssl_mode`: SSL mode (disable, allow, prefer, require, verify-ca, verify-full)
101
+ - `--postgresql-password-file` / `postgresql_password_file`: File holding the database password
102
+ (a mounted secret). The password is never a flag. In code, `PostgreSQLBackend` also takes
103
+ `password=`, and `PostgreSQLGraphBackend` takes `password_file=` or `password_source=` (a
104
+ callable).
105
+
106
+ ```toml
107
+ [tool.nf.config]
108
+ postgresql_host = "db.internal"
109
+ postgresql_database = "netherframe"
110
+ postgresql_username = "nf_apply"
111
+ postgresql_password_file = "/run/secrets/postgresql-password"
112
+ ```
113
+
114
+ See the [Settings reference](../../docs/reference/settings.md).
@@ -0,0 +1,59 @@
1
+ """PostgreSQL backend for Netherframe."""
2
+
3
+ from typing import Any, cast
4
+
5
+ from .graph import PostgreSQLGraphBackend
6
+ from .postgresql import PostgreSQLBackend
7
+ from .postgresql_backend_bootstrap import (
8
+ PostgreSQLBackendBootstrapper,
9
+ PostgreSQLBackendResources,
10
+ )
11
+
12
+ __all__ = [
13
+ "PostgreSQLBackend",
14
+ "PostgreSQLBackendBootstrapper",
15
+ "PostgreSQLBackendResources",
16
+ "PostgreSQLGraphBackend",
17
+ ]
18
+
19
+
20
+ def _register_core_components() -> None:
21
+ """Register PostgreSQL backends with the shared registries."""
22
+ from netherframe.core.backends.backend_bootstrap import BackendBootstrapManager
23
+ from netherframe.core.backends.backend_registry import BackendRegistry
24
+
25
+ BackendRegistry.register_state_backend(
26
+ "postgresql",
27
+ PostgreSQLBackend,
28
+ package_name="netherframe.backends.postgresql",
29
+ )
30
+ BackendBootstrapManager.register_bootstrapper(
31
+ "postgresql",
32
+ PostgreSQLBackendBootstrapper,
33
+ )
34
+
35
+
36
+ def _postgresql_factory(config: dict[str, Any]) -> PostgreSQLBackend:
37
+ """Factory for creating PostgreSQL backends from registry config."""
38
+ return PostgreSQLBackend(**cast("Any", config))
39
+
40
+
41
+ def register_backends() -> None:
42
+ """Register PostgreSQL backends with the registry."""
43
+ try:
44
+ from netherframe.core.backends.backend_registry import BackendRegistry
45
+
46
+ _register_core_components()
47
+ BackendRegistry.register_backend_factory(
48
+ "postgresql",
49
+ _postgresql_factory,
50
+ package_name="netherframe.backends.postgresql",
51
+ )
52
+
53
+ except ImportError:
54
+ # netherframe.core not available, skip registration
55
+ pass
56
+
57
+
58
+ # Auto-registration disabled to avoid circular imports during entry point discovery
59
+ # register_backends()
@@ -0,0 +1,40 @@
1
+ """Connection settings of the PostgreSQL state backends, as every setting: flag > ``[tool.nf.config]`` > argument > default.
2
+
3
+ ``nf --postgresql-host db.internal --postgresql-database nf ...`` or ``postgresql_host`` in
4
+ ``[tool.nf.config]``. The password is never a flag: it comes from a file
5
+ (``--postgresql-password-file``, ``postgresql_password_file``, or the backend's
6
+ ``password_file``, such as a mounted secret or ``<(...)``), or from the backend's own argument
7
+ or callable. Nothing is read from the environment.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from pathlib import Path
13
+ from typing import Any
14
+
15
+ from netherframe.core.config.config_values import get_config_value
16
+
17
+
18
+ def setting(name: str, given: Any, default: Any = None, parser: Any = None) -> Any:
19
+ """``postgresql_<name>``: its flag, else ``[tool.nf.config]``, else ``given`` (the backend's argument), else ``default``."""
20
+ return get_config_value(f"postgresql_{name}", default=default, app_value=given, parser=parser)
21
+
22
+
23
+ def password_file(given: str | Path | None) -> Path | None:
24
+ """The file holding the password: ``--postgresql-password-file``, else ``[tool.nf.config]``, else ``given``."""
25
+ value = setting("password_file", given, parser=str)
26
+ return Path(value).expanduser() if value else None
27
+
28
+
29
+ #: What each pipe (``<(...)``) gave: it can be read once, and the pool may connect again later
30
+ _piped: dict[str, str] = {}
31
+
32
+
33
+ def read_password_file(path: Path) -> str:
34
+ """The password in ``path``: a file is read on each use (a rotated secret applies), a pipe once."""
35
+ if (kept := _piped.get(str(path))) is not None:
36
+ return kept
37
+ password = path.read_text(encoding="utf-8").strip()
38
+ if not path.is_file():
39
+ _piped[str(path)] = password
40
+ return password