muscles-data-sql 2.0.0__py3-none-any.whl
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.
- muscles/data/sql/__init__.py +114 -0
- muscles/data/sql/cli.py +76 -0
- muscles/data/sql/config.py +50 -0
- muscles/data/sql/connections.py +105 -0
- muscles/data/sql/data_adapter.py +71 -0
- muscles/data/sql/drivers.py +52 -0
- muscles/data/sql/engine.py +19 -0
- muscles/data/sql/inspect.py +26 -0
- muscles/data/sql/mapping.py +71 -0
- muscles/data/sql/migration_lock.py +53 -0
- muscles/data/sql/model_mapper.py +30 -0
- muscles/data/sql/native.py +131 -0
- muscles/data/sql/native_migrations.py +200 -0
- muscles/data/sql/providers.py +30 -0
- muscles/data/sql/query.py +33 -0
- muscles/data/sql/repository.py +438 -0
- muscles/data/sql/sqlalchemy_compat.py +18 -0
- muscles/data/sql/uow.py +47 -0
- muscles_data_sql-2.0.0.dist-info/METADATA +223 -0
- muscles_data_sql-2.0.0.dist-info/RECORD +23 -0
- muscles_data_sql-2.0.0.dist-info/WHEEL +5 -0
- muscles_data_sql-2.0.0.dist-info/entry_points.txt +2 -0
- muscles_data_sql-2.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: muscles-data-sql
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Muscles SQL data layer for repositories, migrations and AI-first workflow.
|
|
5
|
+
Author-email: "Denis B." <denis@butko.info>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/butkoden/muscles-data-sql
|
|
8
|
+
Project-URL: Repository, https://github.com/butkoden/muscles-data-sql
|
|
9
|
+
Project-URL: Documentation, https://github.com/butkoden/muscles-data-sql#readme
|
|
10
|
+
Project-URL: Releases, https://github.com/butkoden/muscles-data-sql/releases
|
|
11
|
+
Project-URL: muscles, https://pypi.org/project/muscles/
|
|
12
|
+
Project-URL: muscles-documents, https://pypi.org/project/muscles-documents/
|
|
13
|
+
Project-URL: muscles-ai, https://pypi.org/project/muscles-ai/
|
|
14
|
+
Project-URL: muscles-otel, https://pypi.org/project/muscles-otel/
|
|
15
|
+
Project-URL: muscles-benchmarks, https://pypi.org/project/muscles-benchmarks/
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
Provides-Extra: postgres
|
|
19
|
+
Requires-Dist: psycopg[binary]<4.0,>=3.1; extra == "postgres"
|
|
20
|
+
Provides-Extra: data
|
|
21
|
+
Requires-Dist: muscles-data<1.0.0,>=0.2.0; extra == "data"
|
|
22
|
+
Provides-Extra: legacy
|
|
23
|
+
Requires-Dist: muscles<2.0.0,>=1.0.0rc1; extra == "legacy"
|
|
24
|
+
Requires-Dist: muscles-data<1.0.0,>=0.2.0; extra == "legacy"
|
|
25
|
+
Requires-Dist: SQLAlchemy<3.0,>=2.0; extra == "legacy"
|
|
26
|
+
Requires-Dist: click<9.0,>=8.1; extra == "legacy"
|
|
27
|
+
Requires-Dist: pydantic<3.0,>=2.7; extra == "legacy"
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest<9.0,>=8.0; extra == "dev"
|
|
30
|
+
Requires-Dist: muscles-data-sql[legacy]; extra == "dev"
|
|
31
|
+
|
|
32
|
+
# Muscles Data SQL
|
|
33
|
+
|
|
34
|
+
Muscles SQL is a data layer package for SQL databases:
|
|
35
|
+
- model-to-table mapping
|
|
36
|
+
- engine and session management
|
|
37
|
+
- repository CRUD and advanced query API (filters/operators/joins/aggregates)
|
|
38
|
+
- transactions and Unit of Work (nested/savepoint/retry helpers)
|
|
39
|
+
- native SQL migrations through the shared `muscles-data` migration registry
|
|
40
|
+
- inspect/doctor support with machine-readable diagnostics
|
|
41
|
+
|
|
42
|
+
## Related Repositories
|
|
43
|
+
|
|
44
|
+
- [`muscles`](https://github.com/butkoden/muscles) - core schemas, actions, DI and canonical documentation.
|
|
45
|
+
- [`muscles-documents`](https://github.com/butkoden/muscles-documents) - document metadata/state can use SQL persistence in applications.
|
|
46
|
+
- [`muscles-ai`](https://github.com/butkoden/muscles-ai) - AI/RAG flows can use SQL-backed application state without making SQL generic storage.
|
|
47
|
+
- [`muscles-otel`](https://github.com/butkoden/muscles-otel) - observability hooks around SQL-backed flows.
|
|
48
|
+
- [`muscles-benchmarks`](https://github.com/butkoden/muscles-benchmarks) - SQL transaction and mapping regression checks.
|
|
49
|
+
|
|
50
|
+
## Quickstart
|
|
51
|
+
|
|
52
|
+
This low-level API is useful for standalone scripts and libraries that explicitly
|
|
53
|
+
own their database lifetime. Application packages should use the shared
|
|
54
|
+
`app.resource` integration below instead of constructing `SqlDatabase` objects.
|
|
55
|
+
|
|
56
|
+
The lightweight SQL API has no mandatory third-party dependencies. SQLite uses
|
|
57
|
+
Python's standard library; PostgreSQL requires the `postgres` extra. The
|
|
58
|
+
named-resource integration requires `data`; SQLAlchemy repositories and CLI
|
|
59
|
+
require `legacy`. In this checkout install from the library
|
|
60
|
+
directory using `pip install -e '.[postgres]'`, `.[data]` or `.[legacy]` as
|
|
61
|
+
needed. Publishing the renamed distribution is a separate step.
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from muscles.data.sql import SqlDatabase
|
|
65
|
+
|
|
66
|
+
db = SqlDatabase.sqlite("app.db")
|
|
67
|
+
try:
|
|
68
|
+
with db.transaction() as tx:
|
|
69
|
+
tx.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT)")
|
|
70
|
+
tx.execute("INSERT INTO users VALUES (?, ?)", (1, "Denis"))
|
|
71
|
+
users = tx.fetch_all("SELECT id, name FROM users WHERE id = ?", (1,))
|
|
72
|
+
finally:
|
|
73
|
+
db.close()
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`SqlDatabase.postgres(dsn)` uses psycopg and its native parameter syntax. A
|
|
77
|
+
database owns one connection and is not a connection pool. Transactions from
|
|
78
|
+
different threads are serialized on that connection; nested transactions in the
|
|
79
|
+
same thread are rejected. Direct SQL does not enforce data policies;
|
|
80
|
+
the caller must ensure access restrictions. Commit occurs on successful exit,
|
|
81
|
+
and exceptions roll the transaction back.
|
|
82
|
+
|
|
83
|
+
## `muscles-data` Resource Integration
|
|
84
|
+
|
|
85
|
+
The application composition root owns the catalog and `DataRuntime`. Register
|
|
86
|
+
`NativeSqlFactory` there once, and load `data.resources` from the central
|
|
87
|
+
application configuration:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from muscles.data import DataConfig, DataRuntime
|
|
91
|
+
from muscles.data.catalog import DataAdapterCatalog
|
|
92
|
+
from muscles.data.sql.data_adapter import NativeSqlFactory
|
|
93
|
+
|
|
94
|
+
class App:
|
|
95
|
+
def __init__(self):
|
|
96
|
+
# self.config is the application's shared Muscles configuration.
|
|
97
|
+
catalog = DataAdapterCatalog.with_defaults()
|
|
98
|
+
catalog.register(NativeSqlFactory())
|
|
99
|
+
self.resource = DataRuntime(
|
|
100
|
+
config=DataConfig.from_raw({
|
|
101
|
+
"resources": self.config.get("data.resources").value() or {},
|
|
102
|
+
}),
|
|
103
|
+
catalog=catalog,
|
|
104
|
+
)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The database resource itself is declared once in the application's central
|
|
108
|
+
configuration, for example with `type: native_sql`, `driver: postgres` and
|
|
109
|
+
`url_env: DATABASE_URL`. A package uses the existing resource and does not
|
|
110
|
+
create another catalog, runtime or connection configuration:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from muscles.data.ports import SqlTransactionPort
|
|
114
|
+
|
|
115
|
+
sql = app.resource.connect("database", SqlTransactionPort)
|
|
116
|
+
with sql.transaction() as tx:
|
|
117
|
+
tx.execute("CREATE TABLE IF NOT EXISTS audit (id INTEGER PRIMARY KEY)")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The adapter opens its native connection lazily on the first transaction and
|
|
121
|
+
closes it with the owning `DataRuntime`. Direct `SqlDatabase` examples above
|
|
122
|
+
are for standalone low-level use; application package code should resolve its
|
|
123
|
+
port through `app.resource`.
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
python -m venv .venv
|
|
127
|
+
source .venv/bin/activate
|
|
128
|
+
pip install -e .
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
For local development with the test and legacy dependencies:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
pip install -e ".[dev]"
|
|
135
|
+
pytest -q
|
|
136
|
+
muscles-data-sql doctor --url sqlite:///./app.db
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Named SQL Connections
|
|
140
|
+
|
|
141
|
+
`muscles-data-sql` can manage multiple SQL connections without becoming a generic
|
|
142
|
+
storage registry. The registry is SQL-only: it owns SQL connection configs,
|
|
143
|
+
lazy SQLAlchemy `EngineManager` instances, sessions, inspect and doctor reports.
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
from muscles.data.sql import SqlConnectionConfig, SqlConnectionRegistry
|
|
147
|
+
|
|
148
|
+
registry = SqlConnectionRegistry(
|
|
149
|
+
[
|
|
150
|
+
SqlConnectionConfig(name="default", url="sqlite:///./app.db"),
|
|
151
|
+
SqlConnectionConfig(name="analytics", url="sqlite:///./analytics.db", role="read"),
|
|
152
|
+
]
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
session = registry.session("analytics")
|
|
156
|
+
report = registry.inspect("analytics")
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
CLI diagnostics can read a JSON config:
|
|
160
|
+
|
|
161
|
+
```json
|
|
162
|
+
{
|
|
163
|
+
"connections": {
|
|
164
|
+
"default": "sqlite:///./app.db",
|
|
165
|
+
"analytics": {"url": "sqlite:///./analytics.db", "role": "read"}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
muscles-data-sql inspect --config sql-connections.json --connection analytics
|
|
172
|
+
muscles-data-sql doctor --config sql-connections.json --all
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Diagnostic output uses safe URLs and does not print passwords from DSNs.
|
|
176
|
+
|
|
177
|
+
## Advanced Query Example
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
from sqlalchemy import func
|
|
181
|
+
from muscles.data.sql import FilterClause, JoinClause, QuerySpec, SqlRepository
|
|
182
|
+
|
|
183
|
+
spec = QuerySpec(
|
|
184
|
+
filters=[FilterClause("status", "eq", "active")],
|
|
185
|
+
joins=[JoinClause(table=orders, on=users.c.id == orders.c.user_id)],
|
|
186
|
+
select_columns=[users.c.id, func.count(orders.c.id).label("orders_total")],
|
|
187
|
+
group_by=[users.c.id],
|
|
188
|
+
order_by=[users.c.id.asc()],
|
|
189
|
+
)
|
|
190
|
+
rows = SqlRepository(session, users).aggregate(spec)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Data operation policies
|
|
194
|
+
|
|
195
|
+
`SqlRepository` accepts the policy pipeline and a request-scoped context from
|
|
196
|
+
`muscles-data`. Policy predicates are combined with caller filters using AND;
|
|
197
|
+
they apply to reads, counts, updates, deletes, creates, bulk creates and upserts.
|
|
198
|
+
For update/delete, predicates are part of the SQL statement. Create and changed
|
|
199
|
+
values are checked before execution. Under an active policy pipeline, upsert
|
|
200
|
+
uses the constrained repository path instead of a native conflict statement.
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
from muscles.data import DataOperationContext
|
|
204
|
+
from muscles.data.sql import SqlRepository
|
|
205
|
+
|
|
206
|
+
repo = SqlRepository(
|
|
207
|
+
session,
|
|
208
|
+
documents,
|
|
209
|
+
policies=data_runtime.policies,
|
|
210
|
+
context=DataOperationContext(
|
|
211
|
+
subject_type="account",
|
|
212
|
+
subject_uid=account_uid,
|
|
213
|
+
attributes={"workspace_uid": workspace_uid},
|
|
214
|
+
),
|
|
215
|
+
)
|
|
216
|
+
rows = repo.find({"owner_uid": account_uid}, limit=50)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
The policy contract is backend-neutral; each adapter is responsible for
|
|
220
|
+
translating predicates and enforcing them before pagination or mutation. Direct
|
|
221
|
+
SQLAlchemy session use is outside this enforcement boundary.
|
|
222
|
+
|
|
223
|
+
Repository: https://github.com/butkoden/muscles-data-sql
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
muscles/data/sql/__init__.py,sha256=_rEfWfoB2Ox-QiaeXmszeA3WUl6tjdzDTGra9snKxWA,3355
|
|
2
|
+
muscles/data/sql/cli.py,sha256=QuynWDxzoCXIYyvPaaTtpRo4oXu7eITpQBaKfuFtcVA,2532
|
|
3
|
+
muscles/data/sql/config.py,sha256=MOgFdyD4THFFOLZTG2F_AWum7H21bhjFi3DmHVCFW8Y,1329
|
|
4
|
+
muscles/data/sql/connections.py,sha256=OZSX5PkMtimubGDphwCK7NDM7Urv1WB2blp7nqYw3jI,3937
|
|
5
|
+
muscles/data/sql/data_adapter.py,sha256=7ParyPlg1RtJDSv2J3owhE6xjYpPPHzHT_nzEbHyryo,2391
|
|
6
|
+
muscles/data/sql/drivers.py,sha256=Ty7DebdBKUbgFCNvc_BF7665J28b01arJ-JWWLcVgno,1849
|
|
7
|
+
muscles/data/sql/engine.py,sha256=sA7LJOyYAOkwE9N5KEESmWiZ2pVwPZEjmBwLLgQFqN0,690
|
|
8
|
+
muscles/data/sql/inspect.py,sha256=-o1vDXE4o72WEHhXd0uTQWov7LZ_wEbLLesjz41Sp6I,1005
|
|
9
|
+
muscles/data/sql/mapping.py,sha256=N9UbtpD7LdP38pQ2jHlSx64uPuWWYA2uMWo0Bv6EAAE,2101
|
|
10
|
+
muscles/data/sql/migration_lock.py,sha256=8mi37P4C8eJ1scBtjnrx8HUZOCJIF6A1a7dGblnQqVs,2312
|
|
11
|
+
muscles/data/sql/model_mapper.py,sha256=29SWr9lkAJK8nFAQ9m9n4uN_dGDRlEZgRgLLjNGj_Nk,1264
|
|
12
|
+
muscles/data/sql/native.py,sha256=EzM4AzrhPqTw4P47RXICEjkkS3ggjYq9yBCF4NT11QQ,4726
|
|
13
|
+
muscles/data/sql/native_migrations.py,sha256=cwnwHvfdLCZa4qkpWlkl-Fwuia9T3_-VjLB1gmz75cY,9848
|
|
14
|
+
muscles/data/sql/providers.py,sha256=HvqNeq8v8kfMAVvPz_jVj4BU3CnBpH7mOPmlFYUrzhg,871
|
|
15
|
+
muscles/data/sql/query.py,sha256=yroYWdq0OiwMdeZqfANPZtwK7u0pCvr6OX64QWmK_ts,817
|
|
16
|
+
muscles/data/sql/repository.py,sha256=r_x-smfszUkSr0uAEXR8YLu60PWfJjWuGnn3pDD7-2Y,18184
|
|
17
|
+
muscles/data/sql/sqlalchemy_compat.py,sha256=jVchh_2A-te1ZLRashkE04BJylY1IXphZzz5ppxV71Y,586
|
|
18
|
+
muscles/data/sql/uow.py,sha256=rVTZkwCkbG6HRSQaiEYTDel80tUkRRdhZPh6tlySGRc,1595
|
|
19
|
+
muscles_data_sql-2.0.0.dist-info/METADATA,sha256=cSm6wgCgkrbAKEq9QfTSH0IRiuufUv2GfN0qLpBDfFM,8584
|
|
20
|
+
muscles_data_sql-2.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
21
|
+
muscles_data_sql-2.0.0.dist-info/entry_points.txt,sha256=XsSXbN9ZWt63bhHK4zzekUa8MnwBb49hUGzxKk0QAH0,63
|
|
22
|
+
muscles_data_sql-2.0.0.dist-info/top_level.txt,sha256=7cjPifRPkFXaT0tARERo_UYvYcZKwWYnAjjG6HfZYVE,8
|
|
23
|
+
muscles_data_sql-2.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
muscles
|