activemodel 0.13.0__tar.gz → 0.14.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.
- {activemodel-0.13.0 → activemodel-0.14.1}/.github/dependabot.yml +2 -2
- {activemodel-0.13.0 → activemodel-0.14.1}/.github/workflows/build_and_publish.yml +21 -7
- {activemodel-0.13.0 → activemodel-0.14.1}/.github/workflows/repo-sync.yml +2 -4
- activemodel-0.14.1/.tool-versions +4 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/CHANGELOG.md +32 -0
- activemodel-0.14.1/Justfile +91 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/PKG-INFO +6 -5
- {activemodel-0.13.0 → activemodel-0.14.1}/README.md +2 -1
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/base_model.py +4 -6
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/mixins/pydantic_json.py +27 -7
- activemodel-0.14.1/activemodel/patches/__init__.py +2 -0
- {activemodel-0.13.0/activemodel → activemodel-0.14.1/activemodel/patches}/get_column_from_field_patch.py +13 -26
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/pytest/factories.py +12 -1
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/pytest/plugin.py +11 -7
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/pytest/transaction.py +7 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/pytest/truncate.py +0 -1
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/query_wrapper.py +57 -1
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/session_manager.py +8 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/types/sqlalchemy_protocol.py +1 -3
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/types/sqlalchemy_protocol.pyi +1 -1
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/types/typeid_patch.py +6 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/docker-compose.yml +2 -2
- {activemodel-0.13.0 → activemodel-0.14.1}/pyproject.toml +9 -7
- {activemodel-0.13.0 → activemodel-0.14.1}/test/models.py +1 -1
- {activemodel-0.13.0 → activemodel-0.14.1}/test/nested_pydantic_json_test.py +47 -19
- {activemodel-0.13.0 → activemodel-0.14.1}/test/orm/test_upsert.py +4 -5
- {activemodel-0.13.0 → activemodel-0.14.1}/test/orm_test.py +1 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/pytest/pytest_test.py +1 -0
- activemodel-0.14.1/test/test_query_wrapper.py +163 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/types/typeid_mixin_test.py +0 -5
- {activemodel-0.13.0 → activemodel-0.14.1}/test/types/typeid_sqlmodel_test.py +0 -2
- {activemodel-0.13.0 → activemodel-0.14.1}/test/utils.py +0 -1
- activemodel-0.14.1/uv.lock +1544 -0
- activemodel-0.13.0/.tool-versions +0 -3
- activemodel-0.13.0/Justfile +0 -14
- activemodel-0.13.0/test/test_query_wrapper.py +0 -60
- activemodel-0.13.0/uv.lock +0 -1644
- {activemodel-0.13.0 → activemodel-0.14.1}/.envrc +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/.gitignore +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/.vscode/settings.json +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/LICENSE +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/Makefile +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/TODO +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/__init__.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/celery.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/cli/__init__.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/errors.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/logger.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/mixins/__init__.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/mixins/soft_delete.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/mixins/timestamps.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/mixins/typeid.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/pytest/__init__.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/types/__init__.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/types/typeid.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/activemodel/utils.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground/alternative_typeid_mixin.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground/comments.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground/env-with-model.patch +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground/extract_comments.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground/field.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground/middleware.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground/old_session_manager.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground/pydantic_validation.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/playground.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/__init__.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/comments_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/conftest.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/delete_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/factory_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/fastapi_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/import_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/lifecycle_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/migrations/README +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/migrations/alembic.ini +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/migrations/env.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/migrations/script.py.mako +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/migrations_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/mutation_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/session_manager_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/table_name_test.py +0 -0
- {activemodel-0.13.0 → activemodel-0.14.1}/test/types/typeid_pydantic_test.py +0 -0
|
@@ -32,25 +32,39 @@ jobs:
|
|
|
32
32
|
|
|
33
33
|
publish:
|
|
34
34
|
runs-on: ubuntu-latest
|
|
35
|
-
needs:
|
|
35
|
+
needs: release-please
|
|
36
36
|
if: needs.release-please.outputs.release_created
|
|
37
|
+
permissions:
|
|
38
|
+
id-token: write
|
|
39
|
+
contents: read
|
|
37
40
|
steps:
|
|
38
|
-
- uses: actions/checkout@
|
|
41
|
+
- uses: actions/checkout@v6
|
|
39
42
|
- uses: jdx/mise-action@v3
|
|
40
|
-
-
|
|
43
|
+
- uses: iloveitaly/github-action-direnv-load-and-mask@master
|
|
44
|
+
- run: uv sync
|
|
41
45
|
- run: uv build
|
|
42
|
-
- run: uv publish
|
|
46
|
+
- run: uv publish
|
|
47
|
+
|
|
48
|
+
lint:
|
|
49
|
+
runs-on: ubuntu-latest
|
|
50
|
+
steps:
|
|
51
|
+
- uses: actions/checkout@v6
|
|
52
|
+
- uses: jdx/mise-action@v3
|
|
53
|
+
env:
|
|
54
|
+
MISE_ENV: ci
|
|
55
|
+
- uses: iloveitaly/github-action-direnv-load-and-mask@master
|
|
56
|
+
- run: uv sync
|
|
57
|
+
# - run: just lint
|
|
43
58
|
|
|
44
59
|
matrix-test:
|
|
45
60
|
strategy:
|
|
46
61
|
matrix:
|
|
47
62
|
os: [ubuntu-latest]
|
|
48
|
-
# TODO test on macos-latest, does not have docker by default :/
|
|
49
63
|
# unfortunately, some of the typing stuff we use requires new python versions
|
|
50
|
-
python-version: ["3.13", "3.12"]
|
|
64
|
+
python-version: ["3.14", "3.13", "3.12"]
|
|
51
65
|
runs-on: ${{ matrix.os }}
|
|
52
66
|
steps:
|
|
53
|
-
- uses: actions/checkout@
|
|
67
|
+
- uses: actions/checkout@v6
|
|
54
68
|
- uses: jdx/mise-action@v3
|
|
55
69
|
- run: mise use python@${{ matrix.python-version }}
|
|
56
70
|
- run: docker compose up -d --wait
|
|
@@ -8,9 +8,7 @@ jobs:
|
|
|
8
8
|
repo_sync:
|
|
9
9
|
runs-on: ubuntu-latest
|
|
10
10
|
steps:
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
- name: Repository Metadata Sync
|
|
14
|
-
uses: iloveitaly/github-actions-metadata-sync@main
|
|
11
|
+
- uses: actions/checkout@v6
|
|
12
|
+
- uses: iloveitaly/github-actions-metadata-sync@main
|
|
15
13
|
with:
|
|
16
14
|
TOKEN: ${{ secrets.GH_PERSONAL_TOKEN }}
|
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.14.1](https://github.com/iloveitaly/activemodel/compare/v0.14.0...v0.14.1) (2026-01-14)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* work with latest sqlmodel which drops v1 ([79920c6](https://github.com/iloveitaly/activemodel/commit/79920c63913b0a1d6824efc84719e6ea41b483eb))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Documentation
|
|
12
|
+
|
|
13
|
+
* add pydantic v2 compatibility notes to typeid_patch.py ([7d3f43a](https://github.com/iloveitaly/activemodel/commit/7d3f43a0b1a3c2c51d46ef0530aabee7392f75ec))
|
|
14
|
+
* add warning log for missing session in ActiveModelFactory ([317dd22](https://github.com/iloveitaly/activemodel/commit/317dd22b4abe5d9261d8c7f69a7b32b9c7b2d4de))
|
|
15
|
+
|
|
16
|
+
## [0.14.0](https://github.com/iloveitaly/activemodel/compare/v0.13.0...v0.14.0) (2025-10-16)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* **query_wrapper:** add efficient exists() query method with tests ([257452f](https://github.com/iloveitaly/activemodel/commit/257452fdf976ce263b13997816a3b1b81d2902e9))
|
|
22
|
+
* **query:** add sample() to query wrapper for random row selection ([d35800c](https://github.com/iloveitaly/activemodel/commit/d35800c46244db21fd92e615609b895acdee25dc))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* handle tuples and Optionals in JSON mixin ([1aa1018](https://github.com/iloveitaly/activemodel/commit/1aa1018d9714a43089fa9943daf1b4fcbc7742b9))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
### Documentation
|
|
31
|
+
|
|
32
|
+
* clarify global_session usage in complex test scenarios ([a131b0f](https://github.com/iloveitaly/activemodel/commit/a131b0f64287b4225ac1eb2289e6ce6f006aa4d5))
|
|
33
|
+
* fastapi-sqla ([2183686](https://github.com/iloveitaly/activemodel/commit/2183686421095856c59649c51c03ba1edaea9515))
|
|
34
|
+
|
|
3
35
|
## [0.13.0](https://github.com/iloveitaly/activemodel/compare/v0.12.0...v0.13.0) (2025-09-05)
|
|
4
36
|
|
|
5
37
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
set unstable := true
|
|
2
|
+
|
|
3
|
+
[macos]
|
|
4
|
+
db_play:
|
|
5
|
+
uv tool run pgcli $DATABASE_URL
|
|
6
|
+
|
|
7
|
+
up:
|
|
8
|
+
docker compose up -d
|
|
9
|
+
|
|
10
|
+
# Run linting checks
|
|
11
|
+
[script]
|
|
12
|
+
lint FILES=".":
|
|
13
|
+
set +e
|
|
14
|
+
exit_code=0
|
|
15
|
+
|
|
16
|
+
if [ -n "${CI:-}" ]; then
|
|
17
|
+
# CI mode: GitHub-friendly output
|
|
18
|
+
uv run ruff check --output-format=github {{FILES}} || exit_code=$?
|
|
19
|
+
uv run ruff format --check {{FILES}} || exit_code=$?
|
|
20
|
+
|
|
21
|
+
uv run pyright {{FILES}} --outputjson > pyright_report.json || exit_code=$?
|
|
22
|
+
jq -r \
|
|
23
|
+
--arg root "$GITHUB_WORKSPACE/" \
|
|
24
|
+
'
|
|
25
|
+
.generalDiagnostics[] |
|
|
26
|
+
.file as $file |
|
|
27
|
+
($file | sub("^\\Q\($root)\\E"; "")) as $rel_file |
|
|
28
|
+
"::\(.severity) file=\($rel_file),line=\(.range.start.line),endLine=\(.range.end.line),col=\(.range.start.character),endColumn=\(.range.end.character)::\($rel_file):\(.range.start.line): \(.message)"
|
|
29
|
+
' < pyright_report.json
|
|
30
|
+
rm pyright_report.json
|
|
31
|
+
else
|
|
32
|
+
# Local mode: regular output
|
|
33
|
+
uv run ruff check {{FILES}} || exit_code=$?
|
|
34
|
+
uv run ruff format --check {{FILES}} || exit_code=$?
|
|
35
|
+
uv run pyright {{FILES}} || exit_code=$?
|
|
36
|
+
fi
|
|
37
|
+
|
|
38
|
+
if [ $exit_code -ne 0 ]; then
|
|
39
|
+
echo "One or more linting checks failed"
|
|
40
|
+
exit 1
|
|
41
|
+
fi
|
|
42
|
+
|
|
43
|
+
# Automatically fix linting errors
|
|
44
|
+
lint-fix:
|
|
45
|
+
uv run ruff check . --fix
|
|
46
|
+
uv run ruff format .
|
|
47
|
+
|
|
48
|
+
clean:
|
|
49
|
+
rm -rf *.egg-info .venv
|
|
50
|
+
find . -type d -name "__pycache__" -prune -exec rm -rf {} \; 2>/dev/null || true
|
|
51
|
+
|
|
52
|
+
# TODO what exactly was this used for?
|
|
53
|
+
gh_configure:
|
|
54
|
+
repo_path=$(gh repo view --json nameWithOwner --jq '.nameWithOwner') && \
|
|
55
|
+
gh api --method PUT "/repos/${repo_path}/actions/permissions/workflow" \
|
|
56
|
+
-f default_workflow_permissions=write \
|
|
57
|
+
-F can_approve_pull_request_reviews=true && \
|
|
58
|
+
gh api "/repos/${repo_path}/actions/permissions/workflow"
|
|
59
|
+
|
|
60
|
+
GITHUB_PROTECT_MASTER_RULESET := """
|
|
61
|
+
{
|
|
62
|
+
"name": "Protect master from force pushes",
|
|
63
|
+
"target": "branch",
|
|
64
|
+
"enforcement": "active",
|
|
65
|
+
"conditions": {
|
|
66
|
+
"ref_name": {
|
|
67
|
+
"include": ["refs/heads/master"],
|
|
68
|
+
"exclude": []
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"rules": [
|
|
72
|
+
{
|
|
73
|
+
"type": "non_fast_forward"
|
|
74
|
+
}
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
_github_repo:
|
|
80
|
+
gh repo view --json nameWithOwner -q .nameWithOwner
|
|
81
|
+
|
|
82
|
+
# TODO this only supports deleting the single ruleset specified above
|
|
83
|
+
github_ruleset_protect_master_delete:
|
|
84
|
+
repo=$(just _github_repo) && \
|
|
85
|
+
ruleset_name=$(echo '{{GITHUB_PROTECT_MASTER_RULESET}}' | jq -r .name) && \
|
|
86
|
+
ruleset_id=$(gh api repos/$repo/rulesets --jq ".[] | select(.name == \"$ruleset_name\") | .id") && \
|
|
87
|
+
(([ -n "${ruleset_id}" ] || (echo "No ruleset found" && exit 0)) || gh api --method DELETE repos/$repo/rulesets/$ruleset_id)
|
|
88
|
+
|
|
89
|
+
# adds github ruleset to prevent --force and other destructive actions on the github main branch
|
|
90
|
+
github_ruleset_protect_master_create: github_ruleset_protect_master_delete
|
|
91
|
+
gh api --method POST repos/$(just _github_repo)/rulesets --input - <<< '{{GITHUB_PROTECT_MASTER_RULESET}}'
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: activemodel
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.14.1
|
|
4
4
|
Summary: Make SQLModel more like an a real ORM
|
|
5
5
|
Project-URL: Repository, https://github.com/iloveitaly/activemodel
|
|
6
6
|
Author-email: Michael Bianco <iloveitaly@gmail.com>
|
|
7
7
|
License-File: LICENSE
|
|
8
8
|
Keywords: activemodel,activerecord,orm,sqlalchemy,sqlmodel
|
|
9
|
-
Requires-Python: >=3.
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
10
|
Requires-Dist: python-decouple-typed>=3.11.0
|
|
11
|
-
Requires-Dist: sqlmodel>=0.0.
|
|
11
|
+
Requires-Dist: sqlmodel>=0.0.27
|
|
12
12
|
Requires-Dist: textcase>=0.4.0
|
|
13
|
-
Requires-Dist: typeid-python
|
|
13
|
+
Requires-Dist: typeid-python==0.3.3
|
|
14
14
|
Description-Content-Type: text/markdown
|
|
15
15
|
|
|
16
16
|
# ActiveModel: ORM Wrapper for SQLModel
|
|
@@ -290,7 +290,8 @@ https://github.com/DarylStark/my_data/blob/a17b8b3a8463b9953821b89fee895e272f94d
|
|
|
290
290
|
|
|
291
291
|
* https://github.com/woofz/sqlmodel-basecrud
|
|
292
292
|
* https://github.com/0xthiagomartins/sqlmodel-controller
|
|
293
|
-
* https://github.com/litestar-org/advanced-alchemy
|
|
293
|
+
* https://github.com/litestar-org/advanced-alchemy
|
|
294
|
+
* https://github.com/dialoguemd/fastapi-sqla
|
|
294
295
|
|
|
295
296
|
## Inspiration
|
|
296
297
|
|
|
@@ -275,7 +275,8 @@ https://github.com/DarylStark/my_data/blob/a17b8b3a8463b9953821b89fee895e272f94d
|
|
|
275
275
|
|
|
276
276
|
* https://github.com/woofz/sqlmodel-basecrud
|
|
277
277
|
* https://github.com/0xthiagomartins/sqlmodel-controller
|
|
278
|
-
* https://github.com/litestar-org/advanced-alchemy
|
|
278
|
+
* https://github.com/litestar-org/advanced-alchemy
|
|
279
|
+
* https://github.com/dialoguemd/fastapi-sqla
|
|
279
280
|
|
|
280
281
|
## Inspiration
|
|
281
282
|
|
|
@@ -15,7 +15,7 @@ from sqlalchemy.orm import declared_attr
|
|
|
15
15
|
from activemodel.mixins.pydantic_json import PydanticJSONMixin
|
|
16
16
|
|
|
17
17
|
# NOTE: this patches a core method in sqlmodel to support db comments
|
|
18
|
-
from . import get_column_from_field_patch # noqa: F401
|
|
18
|
+
from .patches import get_column_from_field_patch # noqa: F401
|
|
19
19
|
from .query_wrapper import QueryWrapper
|
|
20
20
|
from .session_manager import get_session
|
|
21
21
|
|
|
@@ -72,11 +72,9 @@ class BaseModel(SQLModel):
|
|
|
72
72
|
def __init_subclass__(cls, **kwargs):
|
|
73
73
|
super().__init_subclass__(**kwargs)
|
|
74
74
|
|
|
75
|
-
from sqlmodel._compat import set_config_value
|
|
76
|
-
|
|
77
75
|
# Enables field-level docstrings on the pydantic `description` field, which we
|
|
78
76
|
# copy into table/column comments by patching SQLModel internals elsewhere.
|
|
79
|
-
|
|
77
|
+
cls.model_config["use_attribute_docstrings"] = True
|
|
80
78
|
|
|
81
79
|
cls._apply_class_doc()
|
|
82
80
|
|
|
@@ -290,7 +288,7 @@ class BaseModel(SQLModel):
|
|
|
290
288
|
# TODO where is this actually used? shoudl prob remove this
|
|
291
289
|
# TODO should we even do this? Can we specify a better json rendering class?
|
|
292
290
|
def json(self, **kwargs):
|
|
293
|
-
return json.dumps(self.
|
|
291
|
+
return json.dumps(self.model_dump(), default=str, **kwargs)
|
|
294
292
|
|
|
295
293
|
# TODO should move this to the wrapper
|
|
296
294
|
@classmethod
|
|
@@ -325,7 +323,7 @@ class BaseModel(SQLModel):
|
|
|
325
323
|
assert len(args) > 0, "Must pass at least one field name"
|
|
326
324
|
|
|
327
325
|
for field_name in args:
|
|
328
|
-
if field_name not in self.
|
|
326
|
+
if field_name not in self.model_fields:
|
|
329
327
|
raise ValueError(f"Field '{field_name}' does not exist in the model.")
|
|
330
328
|
|
|
331
329
|
# check if the field exists
|
|
@@ -6,9 +6,9 @@ SQLModel lacks a direct JSONField equivalent (like Tortoise ORM's JSONField), ma
|
|
|
6
6
|
Extensive discussion on the problem: https://github.com/fastapi/sqlmodel/issues/63
|
|
7
7
|
"""
|
|
8
8
|
|
|
9
|
-
from types import UnionType
|
|
10
9
|
from typing import get_args, get_origin
|
|
11
|
-
|
|
10
|
+
import typing
|
|
11
|
+
import types
|
|
12
12
|
from pydantic import BaseModel as PydanticBaseModel
|
|
13
13
|
from sqlalchemy.orm import reconstructor, attributes
|
|
14
14
|
|
|
@@ -21,6 +21,11 @@ class PydanticJSONMixin:
|
|
|
21
21
|
|
|
22
22
|
>>> class ExampleWithJSON(BaseModel, PydanticJSONMixin, table=True):
|
|
23
23
|
>>> list_field: list[SubObject] = Field(sa_type=JSONB()
|
|
24
|
+
|
|
25
|
+
Notes:
|
|
26
|
+
|
|
27
|
+
- Tuples of pydantic models are not supported, only lists.
|
|
28
|
+
- Nested lists of pydantic models are not supported, e.g. list[list[SubObject]]
|
|
24
29
|
"""
|
|
25
30
|
|
|
26
31
|
@reconstructor
|
|
@@ -37,6 +42,7 @@ class PydanticJSONMixin:
|
|
|
37
42
|
for field_name, field_info in self.model_fields.items():
|
|
38
43
|
raw_value = getattr(self, field_name, None)
|
|
39
44
|
|
|
45
|
+
# if the field is not set on the model, we can avoid doing anything with it
|
|
40
46
|
if raw_value is None:
|
|
41
47
|
continue
|
|
42
48
|
|
|
@@ -44,32 +50,43 @@ class PydanticJSONMixin:
|
|
|
44
50
|
origin = get_origin(annotation)
|
|
45
51
|
|
|
46
52
|
# e.g. `dict` or `dict[str, str]`, we don't want to do anything with these
|
|
47
|
-
if origin
|
|
53
|
+
if origin in (dict, tuple):
|
|
48
54
|
continue
|
|
49
55
|
|
|
50
56
|
annotation_args = get_args(annotation)
|
|
51
57
|
is_top_level_list = origin is list
|
|
58
|
+
model_cls = annotation
|
|
52
59
|
|
|
60
|
+
# TODO not sure what was going on here...
|
|
53
61
|
# if origin is not None:
|
|
54
62
|
# assert annotation.__class__ == origin
|
|
55
63
|
|
|
56
|
-
|
|
64
|
+
# UnionType is only one way of defining an optional. If older typing syntax is used `Tuple[str] | None` the
|
|
65
|
+
# type annotation is different: `typing.Optional[typing.Tuple[float, float]]`. This is why we check both
|
|
66
|
+
# types below.
|
|
57
67
|
|
|
58
68
|
# e.g. SomePydanticModel | None or list[SomePydanticModel] | None
|
|
59
|
-
# annotation_args are (type, NoneType) in this case
|
|
60
|
-
if
|
|
69
|
+
# annotation_args are (type, NoneType) in this case. Remove NoneType.
|
|
70
|
+
if origin in (typing.Union, types.UnionType):
|
|
61
71
|
non_none_types = [t for t in annotation_args if t is not type(None)]
|
|
62
72
|
|
|
63
73
|
if len(non_none_types) == 1:
|
|
64
74
|
model_cls = non_none_types[0]
|
|
75
|
+
else:
|
|
76
|
+
# if there's more than one non-none type, it isn't meant to be serialized to JSON
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
model_cls_origin = get_origin(model_cls)
|
|
65
80
|
|
|
66
81
|
# e.g. list[SomePydanticModel] | None, we have to unpack it
|
|
67
82
|
# model_cls will print as a list, but it contains a subtype if you dig into it
|
|
68
83
|
if (
|
|
69
|
-
|
|
84
|
+
model_cls_origin is list
|
|
70
85
|
and len(list_annotation_args := get_args(model_cls)) == 1
|
|
71
86
|
):
|
|
72
87
|
model_cls = list_annotation_args[0]
|
|
88
|
+
model_cls_origin = get_origin(model_cls)
|
|
89
|
+
|
|
73
90
|
is_top_level_list = True
|
|
74
91
|
|
|
75
92
|
# e.g. list[SomePydanticModel] or list[SomePydanticModel] | None
|
|
@@ -82,6 +99,9 @@ class PydanticJSONMixin:
|
|
|
82
99
|
attributes.set_committed_value(self, field_name, parsed_value)
|
|
83
100
|
continue
|
|
84
101
|
|
|
102
|
+
if model_cls_origin in (list, tuple):
|
|
103
|
+
continue
|
|
104
|
+
|
|
85
105
|
# single class
|
|
86
106
|
if issubclass(model_cls, PydanticBaseModel):
|
|
87
107
|
attributes.set_committed_value(self, field_name, model_cls(**raw_value))
|
|
@@ -13,26 +13,23 @@ Some ideas for this originally sourced from: https://github.com/fastapi/sqlmodel
|
|
|
13
13
|
from typing import (
|
|
14
14
|
TYPE_CHECKING,
|
|
15
15
|
Any,
|
|
16
|
-
Dict,
|
|
17
16
|
Sequence,
|
|
18
17
|
cast,
|
|
19
18
|
)
|
|
20
19
|
|
|
21
20
|
import sqlmodel
|
|
22
|
-
from pydantic.fields import FieldInfo as PydanticFieldInfo
|
|
23
21
|
from sqlalchemy import (
|
|
24
22
|
Column,
|
|
25
23
|
ForeignKey,
|
|
26
24
|
)
|
|
27
25
|
from sqlmodel._compat import ( # type: ignore[attr-defined]
|
|
28
|
-
IS_PYDANTIC_V2,
|
|
29
26
|
ModelMetaclass,
|
|
30
27
|
Representation,
|
|
31
28
|
Undefined,
|
|
32
29
|
UndefinedType,
|
|
33
30
|
is_field_noneable,
|
|
34
31
|
)
|
|
35
|
-
from sqlmodel.main import
|
|
32
|
+
from sqlmodel.main import get_sqlalchemy_type
|
|
36
33
|
|
|
37
34
|
from activemodel.utils import hash_function_code
|
|
38
35
|
|
|
@@ -43,37 +40,31 @@ if TYPE_CHECKING:
|
|
|
43
40
|
from pydantic_core import PydanticUndefinedType as UndefinedType
|
|
44
41
|
|
|
45
42
|
|
|
43
|
+
# https://github.com/fastapi/sqlmodel/blob/5c2dbe419edc2d15200eee5269c9508987944ed8/sqlmodel/main.py#L691
|
|
46
44
|
assert (
|
|
47
45
|
hash_function_code(sqlmodel.main.get_column_from_field)
|
|
48
|
-
== "
|
|
46
|
+
== "ab3cdd5d20079358911b6aef76b3916ba3890b20eb07e970a0f35bd63e1713d9"
|
|
47
|
+
), (
|
|
48
|
+
f"get_column_from_field has changed, please verify the patch is still valid: {hash_function_code(sqlmodel.main.get_column_from_field)}"
|
|
49
49
|
)
|
|
50
50
|
|
|
51
51
|
|
|
52
|
-
def get_column_from_field(field:
|
|
53
|
-
|
|
54
|
-
Takes a field definition, which can either come from the sqlmodel FieldInfo class or the pydantic variant of that class,
|
|
55
|
-
and converts it into a sqlalchemy Column object.
|
|
56
|
-
"""
|
|
57
|
-
if IS_PYDANTIC_V2:
|
|
58
|
-
field_info = field
|
|
59
|
-
else:
|
|
60
|
-
field_info = field.field_info
|
|
61
|
-
|
|
52
|
+
def get_column_from_field(field: Any) -> Column: # type: ignore
|
|
53
|
+
field_info = field
|
|
62
54
|
sa_column = getattr(field_info, "sa_column", Undefined)
|
|
63
55
|
if isinstance(sa_column, Column):
|
|
64
|
-
#
|
|
56
|
+
# <Change>
|
|
65
57
|
if not sa_column.comment and (field_comment := field_info.description):
|
|
66
58
|
sa_column.comment = field_comment
|
|
59
|
+
# </Change>
|
|
67
60
|
return sa_column
|
|
68
|
-
|
|
61
|
+
sa_type = get_sqlalchemy_type(field)
|
|
69
62
|
primary_key = getattr(field_info, "primary_key", Undefined)
|
|
70
63
|
if primary_key is Undefined:
|
|
71
64
|
primary_key = False
|
|
72
|
-
|
|
73
65
|
index = getattr(field_info, "index", Undefined)
|
|
74
66
|
if index is Undefined:
|
|
75
67
|
index = False
|
|
76
|
-
|
|
77
68
|
nullable = not primary_key and is_field_noneable(field)
|
|
78
69
|
# Override derived nullability if the nullable property is set explicitly
|
|
79
70
|
# on the field
|
|
@@ -103,7 +94,6 @@ def get_column_from_field(field: PydanticFieldInfo | FieldInfo) -> Column: # ty
|
|
|
103
94
|
"index": index,
|
|
104
95
|
"unique": unique,
|
|
105
96
|
}
|
|
106
|
-
|
|
107
97
|
sa_default = Undefined
|
|
108
98
|
if field_info.default_factory:
|
|
109
99
|
sa_default = field_info.default_factory
|
|
@@ -111,14 +101,12 @@ def get_column_from_field(field: PydanticFieldInfo | FieldInfo) -> Column: # ty
|
|
|
111
101
|
sa_default = field_info.default
|
|
112
102
|
if sa_default is not Undefined:
|
|
113
103
|
kwargs["default"] = sa_default
|
|
114
|
-
|
|
115
104
|
sa_column_args = getattr(field_info, "sa_column_args", Undefined)
|
|
116
105
|
if sa_column_args is not Undefined:
|
|
117
106
|
args.extend(list(cast(Sequence[Any], sa_column_args)))
|
|
118
|
-
|
|
119
107
|
sa_column_kwargs = getattr(field_info, "sa_column_kwargs", Undefined)
|
|
120
108
|
|
|
121
|
-
#
|
|
109
|
+
# <Change>
|
|
122
110
|
if field_info.description:
|
|
123
111
|
if sa_column_kwargs is Undefined:
|
|
124
112
|
sa_column_kwargs = {}
|
|
@@ -128,11 +116,10 @@ def get_column_from_field(field: PydanticFieldInfo | FieldInfo) -> Column: # ty
|
|
|
128
116
|
# only update comments if not already set
|
|
129
117
|
if "comment" not in sa_column_kwargs:
|
|
130
118
|
sa_column_kwargs["comment"] = field_info.description
|
|
119
|
+
# </Change>
|
|
131
120
|
|
|
132
121
|
if sa_column_kwargs is not Undefined:
|
|
133
|
-
kwargs.update(cast(
|
|
134
|
-
|
|
135
|
-
sa_type = get_sqlalchemy_type(field)
|
|
122
|
+
kwargs.update(cast(dict[Any, Any], sa_column_kwargs))
|
|
136
123
|
return Column(sa_type, *args, **kwargs) # type: ignore
|
|
137
124
|
|
|
138
125
|
|
|
@@ -12,6 +12,7 @@ from polyfactory.field_meta import FieldMeta
|
|
|
12
12
|
from typeid import TypeID
|
|
13
13
|
|
|
14
14
|
from activemodel.session_manager import global_session
|
|
15
|
+
from activemodel.logger import logger
|
|
15
16
|
|
|
16
17
|
# TODO not currently used
|
|
17
18
|
# def type_id_provider(cls, field_meta):
|
|
@@ -56,11 +57,20 @@ class ActiveModelFactory[T](SQLModelFactory[T]):
|
|
|
56
57
|
@classmethod
|
|
57
58
|
def save(cls, *args, **kwargs) -> T:
|
|
58
59
|
"""
|
|
60
|
+
Builds and persists a new model to the database.
|
|
61
|
+
|
|
59
62
|
Where this gets tricky, is this can be called multiple times within the same callstack. This can happen when
|
|
60
|
-
a factory uses other factories to create relationships.
|
|
63
|
+
a factory uses other factories to create relationships. This is fine if `__sqlalchemy_session__` is set, but
|
|
64
|
+
if it's not (in the case of a truncation DB strategy) you'll run into issues.
|
|
61
65
|
|
|
62
66
|
In a truncation strategy, the __sqlalchemy_session__ is set to None.
|
|
63
67
|
"""
|
|
68
|
+
|
|
69
|
+
if cls.__sqlalchemy_session__ is None:
|
|
70
|
+
logger.warning(
|
|
71
|
+
"No __sqlalchemy_session__ set on factory class, nested factory save() will fail. Use `db_session` or `db_truncate_session` to avoid this."
|
|
72
|
+
)
|
|
73
|
+
|
|
64
74
|
with global_session(cls.__sqlalchemy_session__):
|
|
65
75
|
return cls.build(*args, **kwargs).save()
|
|
66
76
|
|
|
@@ -74,6 +84,7 @@ class ActiveModelFactory[T](SQLModelFactory[T]):
|
|
|
74
84
|
# TODO right now assumes the model is typeid, maybe we should assert against this?
|
|
75
85
|
primary_key_name = cls.__model__.primary_key_column().name
|
|
76
86
|
return TypeID(
|
|
87
|
+
# gets the prefix associated with the pk field
|
|
77
88
|
cls.__model__.model_fields[primary_key_name].sa_column.type.prefix
|
|
78
89
|
)
|
|
79
90
|
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Currently provides:
|
|
4
4
|
|
|
5
|
-
* ``db_session`` fixture
|
|
6
|
-
* ``activemodel_preserve_tables`` ini option
|
|
5
|
+
* ``db_session`` fixture - quick access to a database session (see ``test_session``)
|
|
6
|
+
* ``activemodel_preserve_tables`` ini option - configure tables to preserve when using
|
|
7
7
|
``database_reset_truncate`` (comma separated list or multiple lines depending on config style)
|
|
8
8
|
|
|
9
9
|
Configuration examples:
|
|
@@ -28,7 +28,10 @@ The list always implicitly includes ``alembic_version`` even if not specified.
|
|
|
28
28
|
from activemodel.session_manager import global_session
|
|
29
29
|
import pytest
|
|
30
30
|
|
|
31
|
-
from .transaction import
|
|
31
|
+
from .transaction import (
|
|
32
|
+
set_factory_sessions,
|
|
33
|
+
test_session,
|
|
34
|
+
)
|
|
32
35
|
|
|
33
36
|
|
|
34
37
|
def pytest_addoption(
|
|
@@ -53,7 +56,7 @@ def pytest_addoption(
|
|
|
53
56
|
@pytest.fixture(scope="function")
|
|
54
57
|
def db_session():
|
|
55
58
|
"""
|
|
56
|
-
Helpful for tests that are
|
|
59
|
+
Helpful for tests that are similar to unit tests. If you doing a routing or integration test, you
|
|
57
60
|
probably don't need this. If your unit test is simple (you are just creating a couple of models) you
|
|
58
61
|
can most likely skip this.
|
|
59
62
|
|
|
@@ -71,11 +74,12 @@ def db_truncate_session():
|
|
|
71
74
|
"""
|
|
72
75
|
Provides a database session for testing when using a truncation cleaning strategy.
|
|
73
76
|
|
|
74
|
-
When
|
|
77
|
+
When using a truncation cleaning strategy, no global test session is set. This means all models that are created
|
|
78
|
+
are tied to a detached session, which makes it hard to mutate models after creation. This fixture fixes that problem
|
|
79
|
+
by setting the session used by all model factories to a global session.
|
|
75
80
|
"""
|
|
76
81
|
with global_session() as session:
|
|
77
82
|
# set global database sessions for model factories to avoid lazy loading issues
|
|
78
|
-
|
|
79
|
-
set_polyfactory_session(session)
|
|
83
|
+
set_factory_sessions(session)
|
|
80
84
|
|
|
81
85
|
yield session
|
|
@@ -41,6 +41,13 @@ def set_polyfactory_session(session):
|
|
|
41
41
|
ActiveModelFactory.__sqlalchemy_session__ = session
|
|
42
42
|
|
|
43
43
|
|
|
44
|
+
def set_factory_sessions(session):
|
|
45
|
+
"set all supported model factories to use the provided session"
|
|
46
|
+
|
|
47
|
+
set_factory_session(session)
|
|
48
|
+
set_polyfactory_session(session)
|
|
49
|
+
|
|
50
|
+
|
|
44
51
|
@contextlib.contextmanager
|
|
45
52
|
def test_session():
|
|
46
53
|
"""
|