activemodel 0.5.0__tar.gz → 0.7.0__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.7.0/.envrc +12 -0
- activemodel-0.7.0/.github/dependabot.yml +12 -0
- activemodel-0.7.0/.github/workflows/build_and_publish.yml +58 -0
- activemodel-0.7.0/.github/workflows/repo-sync.yml +16 -0
- activemodel-0.7.0/.gitignore +131 -0
- activemodel-0.7.0/.tool-versions +3 -0
- activemodel-0.7.0/.vscode/settings.json +36 -0
- activemodel-0.7.0/CHANGELOG.md +115 -0
- activemodel-0.7.0/Justfile +14 -0
- activemodel-0.7.0/Makefile +7 -0
- activemodel-0.7.0/PKG-INFO +235 -0
- activemodel-0.7.0/README.md +220 -0
- activemodel-0.7.0/TODO +51 -0
- {activemodel-0.5.0 → activemodel-0.7.0}/activemodel/__init__.py +2 -0
- {activemodel-0.5.0 → activemodel-0.7.0}/activemodel/base_model.py +105 -29
- activemodel-0.7.0/activemodel/celery.py +28 -0
- activemodel-0.7.0/activemodel/errors.py +6 -0
- activemodel-0.7.0/activemodel/get_column_from_field_patch.py +137 -0
- activemodel-0.7.0/activemodel/mixins/__init__.py +4 -0
- activemodel-0.7.0/activemodel/mixins/pydantic_json.py +69 -0
- activemodel-0.7.0/activemodel/mixins/soft_delete.py +17 -0
- activemodel-0.7.0/activemodel/mixins/typeid.py +46 -0
- {activemodel-0.5.0 → activemodel-0.7.0}/activemodel/pytest/truncate.py +1 -1
- {activemodel-0.5.0 → activemodel-0.7.0}/activemodel/query_wrapper.py +24 -10
- activemodel-0.7.0/activemodel/session_manager.py +132 -0
- activemodel-0.7.0/activemodel/types/__init__.py +1 -0
- activemodel-0.7.0/activemodel/types/typeid.py +191 -0
- activemodel-0.7.0/activemodel/utils.py +65 -0
- activemodel-0.7.0/docker-compose.yml +24 -0
- activemodel-0.7.0/playground/comments.py +74 -0
- activemodel-0.7.0/playground/env-with-model.patch +12 -0
- activemodel-0.7.0/playground/field.py +13 -0
- activemodel-0.7.0/playground/middleware.py +9 -0
- activemodel-0.7.0/playground/pydantic_validation.py +13 -0
- activemodel-0.7.0/playground.py +21 -0
- {activemodel-0.5.0 → activemodel-0.7.0}/pyproject.toml +22 -2
- activemodel-0.7.0/test/__init__.py +0 -0
- activemodel-0.7.0/test/comments_test.py +111 -0
- activemodel-0.7.0/test/conftest.py +17 -0
- activemodel-0.7.0/test/delete_test.py +17 -0
- activemodel-0.7.0/test/fastapi_test.py +79 -0
- activemodel-0.7.0/test/migrations/README +1 -0
- activemodel-0.7.0/test/migrations/alembic.ini +120 -0
- activemodel-0.7.0/test/migrations/env.py +87 -0
- activemodel-0.7.0/test/migrations/script.py.mako +28 -0
- activemodel-0.7.0/test/migrations_test.py +45 -0
- activemodel-0.7.0/test/models.py +44 -0
- activemodel-0.7.0/test/orm_test.py +51 -0
- activemodel-0.7.0/test/serialization_test.py +85 -0
- activemodel-0.7.0/test/table_name_test.py +14 -0
- activemodel-0.7.0/test/test_wrapper.py +44 -0
- activemodel-0.7.0/test/typeid_test.py +90 -0
- activemodel-0.7.0/test/utils.py +45 -0
- activemodel-0.7.0/uv.lock +1300 -0
- activemodel-0.5.0/PKG-INFO +0 -66
- activemodel-0.5.0/README.md +0 -51
- activemodel-0.5.0/activemodel/mixins/__init__.py +0 -2
- activemodel-0.5.0/activemodel/mixins/typeid.py +0 -36
- activemodel-0.5.0/activemodel/session_manager.py +0 -62
- activemodel-0.5.0/activemodel/types/typeid.py +0 -56
- activemodel-0.5.0/activemodel/utils.py +0 -15
- activemodel-0.5.0/activemodel.egg-info/PKG-INFO +0 -66
- activemodel-0.5.0/activemodel.egg-info/SOURCES.txt +0 -23
- activemodel-0.5.0/activemodel.egg-info/dependency_links.txt +0 -1
- activemodel-0.5.0/activemodel.egg-info/entry_points.txt +0 -2
- activemodel-0.5.0/activemodel.egg-info/requires.txt +0 -4
- activemodel-0.5.0/activemodel.egg-info/top_level.txt +0 -1
- activemodel-0.5.0/setup.cfg +0 -4
- {activemodel-0.5.0 → activemodel-0.7.0}/LICENSE +0 -0
- {activemodel-0.5.0 → activemodel-0.7.0}/activemodel/logger.py +0 -0
- {activemodel-0.5.0 → activemodel-0.7.0}/activemodel/mixins/timestamps.py +0 -0
- {activemodel-0.5.0 → activemodel-0.7.0}/activemodel/pytest/__init__.py +0 -0
- {activemodel-0.5.0 → activemodel-0.7.0}/activemodel/pytest/transaction.py +0 -0
- /activemodel-0.5.0/activemodel/_session_manager.py → /activemodel-0.7.0/playground/old_session_manager.py +0 -0
activemodel-0.7.0/.envrc
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
layout uv
|
|
2
|
+
|
|
3
|
+
# if you are using orb for local development, this will work just fine
|
|
4
|
+
export DATABASE_HOST=${DATABASE_HOST:-postgres.$(basename $PWD).orb.local}
|
|
5
|
+
|
|
6
|
+
export POSTGRES_USER=root
|
|
7
|
+
export POSTGRES_PASSWORD=password
|
|
8
|
+
export POSTGRES_DB=development
|
|
9
|
+
|
|
10
|
+
export DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}:5432/development
|
|
11
|
+
|
|
12
|
+
export PYTHONBREAKPOINT=ipdb.set_trace
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
name: Build and Publish to PyPI
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
- master
|
|
7
|
+
|
|
8
|
+
# write permissions for release-please
|
|
9
|
+
# permissions:
|
|
10
|
+
# contents: write
|
|
11
|
+
# pull-requests: write
|
|
12
|
+
|
|
13
|
+
env:
|
|
14
|
+
# avoid build failures due to flaky pypi
|
|
15
|
+
PIP_DEFAULT_TIMEOUT: 60
|
|
16
|
+
PIP_RETRIES: 5
|
|
17
|
+
|
|
18
|
+
DATABASE_HOST: localhost
|
|
19
|
+
|
|
20
|
+
jobs:
|
|
21
|
+
release-please:
|
|
22
|
+
runs-on: ubuntu-latest
|
|
23
|
+
needs: [build]
|
|
24
|
+
outputs:
|
|
25
|
+
release_created: ${{ steps.release.outputs.release_created }}
|
|
26
|
+
steps:
|
|
27
|
+
- uses: googleapis/release-please-action@v4
|
|
28
|
+
id: release
|
|
29
|
+
with:
|
|
30
|
+
release-type: python
|
|
31
|
+
# bump-minor-pre-major: true
|
|
32
|
+
token: ${{ secrets.GH_PERSONAL_TOKEN }}
|
|
33
|
+
|
|
34
|
+
publish:
|
|
35
|
+
runs-on: ubuntu-latest
|
|
36
|
+
needs: [release-please]
|
|
37
|
+
if: needs.release-please.outputs.release_created
|
|
38
|
+
steps:
|
|
39
|
+
- uses: actions/checkout@v4
|
|
40
|
+
- uses: jdx/mise-action@v2
|
|
41
|
+
- run: direnv allow . && direnv export gha >> "$GITHUB_ENV"
|
|
42
|
+
- run: uv build
|
|
43
|
+
- run: uv publish --token ${{ secrets.PYPI_API_TOKEN }}
|
|
44
|
+
|
|
45
|
+
build:
|
|
46
|
+
runs-on: ubuntu-latest
|
|
47
|
+
steps:
|
|
48
|
+
- uses: actions/checkout@v4
|
|
49
|
+
- uses: jdx/mise-action@v2
|
|
50
|
+
- run: direnv allow . && direnv export gha >> "$GITHUB_ENV"
|
|
51
|
+
- run: docker compose up -d --wait
|
|
52
|
+
- run: uv sync
|
|
53
|
+
|
|
54
|
+
# `uv run` prefix is required since the venv is not activated
|
|
55
|
+
|
|
56
|
+
- name: Make sure we can import the module
|
|
57
|
+
run: uv run python -c 'import ${{ github.event.repository.name }}'
|
|
58
|
+
- run: uv run pytest
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
name: Repository Metadata Sync
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main, master]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
repo_sync:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- name: Fetching Local Repository
|
|
12
|
+
uses: actions/checkout@v4
|
|
13
|
+
- name: Repository Metadata Sync
|
|
14
|
+
uses: iloveitaly/github-actions-metadata-sync@main
|
|
15
|
+
with:
|
|
16
|
+
TOKEN: ${{ secrets.GH_PERSONAL_TOKEN }}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
pip-wheel-metadata/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
# Usually these files are written by a python script from a template
|
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
33
|
+
*.manifest
|
|
34
|
+
*.spec
|
|
35
|
+
|
|
36
|
+
# Installer logs
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
|
|
40
|
+
# Unit test / coverage reports
|
|
41
|
+
htmlcov/
|
|
42
|
+
.tox/
|
|
43
|
+
.nox/
|
|
44
|
+
.coverage
|
|
45
|
+
.coverage.*
|
|
46
|
+
.cache
|
|
47
|
+
nosetests.xml
|
|
48
|
+
coverage.xml
|
|
49
|
+
*.cover
|
|
50
|
+
*.py,cover
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
target/
|
|
76
|
+
|
|
77
|
+
# Jupyter Notebook
|
|
78
|
+
.ipynb_checkpoints
|
|
79
|
+
|
|
80
|
+
# IPython
|
|
81
|
+
profile_default/
|
|
82
|
+
ipython_config.py
|
|
83
|
+
|
|
84
|
+
# pyenv
|
|
85
|
+
.python-version
|
|
86
|
+
|
|
87
|
+
# pipenv
|
|
88
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
89
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
90
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
91
|
+
# install all needed dependencies.
|
|
92
|
+
#Pipfile.lock
|
|
93
|
+
|
|
94
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
95
|
+
__pypackages__/
|
|
96
|
+
|
|
97
|
+
# Celery stuff
|
|
98
|
+
celerybeat-schedule
|
|
99
|
+
celerybeat.pid
|
|
100
|
+
|
|
101
|
+
# SageMath parsed files
|
|
102
|
+
*.sage.py
|
|
103
|
+
|
|
104
|
+
# Environments
|
|
105
|
+
.env
|
|
106
|
+
.venv
|
|
107
|
+
env/
|
|
108
|
+
venv/
|
|
109
|
+
ENV/
|
|
110
|
+
env.bak/
|
|
111
|
+
venv.bak/
|
|
112
|
+
|
|
113
|
+
# Spyder project settings
|
|
114
|
+
.spyderproject
|
|
115
|
+
.spyproject
|
|
116
|
+
|
|
117
|
+
# Rope project settings
|
|
118
|
+
.ropeproject
|
|
119
|
+
|
|
120
|
+
# mkdocs documentation
|
|
121
|
+
/site
|
|
122
|
+
|
|
123
|
+
# mypy
|
|
124
|
+
.mypy_cache/
|
|
125
|
+
.dmypy.json
|
|
126
|
+
dmypy.json
|
|
127
|
+
|
|
128
|
+
# Pyre type checker
|
|
129
|
+
.pyre/
|
|
130
|
+
|
|
131
|
+
/test/migrations/versions
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"[python]": {
|
|
3
|
+
"editor.formatOnSave": true,
|
|
4
|
+
"editor.defaultFormatter": "charliermarsh.ruff",
|
|
5
|
+
"editor.codeActionsOnSave": {
|
|
6
|
+
"source.fixAll": "explicit",
|
|
7
|
+
"source.organizeImports": "explicit"
|
|
8
|
+
},
|
|
9
|
+
"editor.tabSize": 4
|
|
10
|
+
},
|
|
11
|
+
"[toml]": {
|
|
12
|
+
"editor.formatOnSave": true,
|
|
13
|
+
"editor.tabSize": 4
|
|
14
|
+
},
|
|
15
|
+
"python.analysis.autoFormatStrings": true,
|
|
16
|
+
|
|
17
|
+
// for import autosuggest
|
|
18
|
+
"python.analysis.indexing": true,
|
|
19
|
+
"python.analysis.autoImportCompletions": true,
|
|
20
|
+
|
|
21
|
+
"python.analysis.packageIndexDepths": [
|
|
22
|
+
{
|
|
23
|
+
"name": "",
|
|
24
|
+
"depth": 3,
|
|
25
|
+
"includeAllSymbols": true
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
|
|
29
|
+
"cSpell.words": ["openai", "httpx"],
|
|
30
|
+
|
|
31
|
+
"files.exclude": {
|
|
32
|
+
".ruff_cache": true,
|
|
33
|
+
".pytest_cache": true,
|
|
34
|
+
".venv": true
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.7.0](https://github.com/iloveitaly/activemodel/compare/v0.6.0...v0.7.0) (2025-02-08)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **migrations:** add test to validate autogenerated migrations ([03e0511](https://github.com/iloveitaly/activemodel/commit/03e0511b90307091448f165cca1976ca37e41d4a))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* filter migration files to include only Python files in test ([76f6723](https://github.com/iloveitaly/activemodel/commit/76f672349f24c34284c190368e71a5d4116c3293))
|
|
14
|
+
* make another_with_index optional in ExampleRecord ([2ace729](https://github.com/iloveitaly/activemodel/commit/2ace7292bcc84907818f8719571a2bfcafecfd6b))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Documentation
|
|
18
|
+
|
|
19
|
+
* enhance README with alembic integration instructions ([a7ac734](https://github.com/iloveitaly/activemodel/commit/a7ac734c5f456afb605ce4e7d0e5111835bfb5f6))
|
|
20
|
+
* improve Alembic integration guidance and resources ([1206278](https://github.com/iloveitaly/activemodel/commit/1206278ae292669bff5a1eb4747d81225dd420e9))
|
|
21
|
+
|
|
22
|
+
## [0.6.0](https://github.com/iloveitaly/activemodel/compare/v0.5.0...v0.6.0) (2025-02-08)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* add active model meta functionality with comments ([4180df7](https://github.com/iloveitaly/activemodel/commit/4180df73c8e49fb2da970f934856ac89cd9f4ebc))
|
|
28
|
+
* add Celery encoder for TypeID serialization ([416b04a](https://github.com/iloveitaly/activemodel/commit/416b04ad4bff5f156886fe5519c24505e380f9c7))
|
|
29
|
+
* add data initialization on model load ([00a90ce](https://github.com/iloveitaly/activemodel/commit/00a90cef17d4d5ef1530df21357812d974488012))
|
|
30
|
+
* add field comments to SQLModel metadata ([c32bb9a](https://github.com/iloveitaly/activemodel/commit/c32bb9a45be0e0dd90bc967d28141bf460b524d3))
|
|
31
|
+
* add initial TypeIDType import to __init__.py ([8594dff](https://github.com/iloveitaly/activemodel/commit/8594dffd5ac7fb03ae07bdd8c81e556e1542176e))
|
|
32
|
+
* add JSON serialization for Pydantic models ([1213f2f](https://github.com/iloveitaly/activemodel/commit/1213f2f2bcee0a006eb58658565484ebb60727d5))
|
|
33
|
+
* add playground script for interactive database testing ([fbe419a](https://github.com/iloveitaly/activemodel/commit/fbe419aa6d0edde5abbcdf405f7b037317730df1))
|
|
34
|
+
* add soft deletion mixin for model handling ([2e24e26](https://github.com/iloveitaly/activemodel/commit/2e24e26e0283189d18a7b51b068bd63d58b5e3dd))
|
|
35
|
+
* add TypeID validation and refactor TypeIDType ([5a2a49e](https://github.com/iloveitaly/activemodel/commit/5a2a49e1ee10cb648b4fedc7fd3e262ba5d8e0f1))
|
|
36
|
+
* add utility to import all SQLModel subclasses ([e850016](https://github.com/iloveitaly/activemodel/commit/e85001632524130eac461e6cff23691fcb1c6bbb))
|
|
37
|
+
* enforce unique non-empty prefixes in TypeIDMixin ([a167293](https://github.com/iloveitaly/activemodel/commit/a167293996b319759c8b01c49a05ab0f8dd1d153))
|
|
38
|
+
* enhance foreign key handling and add validation ([61d5849](https://github.com/iloveitaly/activemodel/commit/61d58495a64c7d87d4c5ad1f1cfe0dd4649fe523))
|
|
39
|
+
* enhance query handling and testing in ORM ([363c0dd](https://github.com/iloveitaly/activemodel/commit/363c0dd67b24ba4e860799555e25d51a3c8d4b0d))
|
|
40
|
+
* enhance schema handling with new json schema generation ([3ba47b6](https://github.com/iloveitaly/activemodel/commit/3ba47b6c150d80e24691c8eba195b800cc3ba61c))
|
|
41
|
+
* enhance session management and JSON serialization ([c74804b](https://github.com/iloveitaly/activemodel/commit/c74804b005e7667b5b18cb4f9b03f52cdcb72313))
|
|
42
|
+
* handle dict annotations in PydanticJSONMixin ([085c6a9](https://github.com/iloveitaly/activemodel/commit/085c6a96d02e0d1521c6c58be38cf5b7d3a667af))
|
|
43
|
+
* integrate field description as SQL comment ([cd98fe4](https://github.com/iloveitaly/activemodel/commit/cd98fe4b3024d55dbd9e7951d65b3b73b2aa3cf8))
|
|
44
|
+
* integrate table comment extraction for models ([b7b9722](https://github.com/iloveitaly/activemodel/commit/b7b97224244b886f2067c0f5a0aea3c77c6bbe82))
|
|
45
|
+
* integrate TypeID with Pydantic schema handling ([ab7ded9](https://github.com/iloveitaly/activemodel/commit/ab7ded9e472abdab8e34205e15fc643db842904d))
|
|
46
|
+
* patch FieldInfo for comment support ([e85d154](https://github.com/iloveitaly/activemodel/commit/e85d1543856be8bd19f185e025a43f20aa9a01cb))
|
|
47
|
+
* **test:** enhance ORM tests and introduce QueryWrapper test ([be98abb](https://github.com/iloveitaly/activemodel/commit/be98abb1bb5abe34cf42ebba7b63b89a942e487e))
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### Bug Fixes
|
|
51
|
+
|
|
52
|
+
* attempt at using a context manager, no joy ([409ed6d](https://github.com/iloveitaly/activemodel/commit/409ed6de7618c0fcce7fa8fde2f77e0c612c39cb))
|
|
53
|
+
* return True upon successful delete operation ([444a5f5](https://github.com/iloveitaly/activemodel/commit/444a5f561a04fb2dad549c4ffc5a05cc31453fca))
|
|
54
|
+
* support lots of UUID inputs ([783a9fe](https://github.com/iloveitaly/activemodel/commit/783a9fe7f733b346cd6bf2401a9ae134ec785cf6))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
### Documentation
|
|
58
|
+
|
|
59
|
+
* add example queries to the README file ([f3e878c](https://github.com/iloveitaly/activemodel/commit/f3e878cb72e62fdec727f7b268806d850ea145f0))
|
|
60
|
+
* add new controller project ([c10bc29](https://github.com/iloveitaly/activemodel/commit/c10bc2946b8819812810271c195e317c19269adc))
|
|
61
|
+
* add usage example for Appointment model in README ([7d9ac70](https://github.com/iloveitaly/activemodel/commit/7d9ac708b6bc5c77d3dde0a904ddb6c24c7f2710))
|
|
62
|
+
* expand README with setup and usage sections ([c471f5f](https://github.com/iloveitaly/activemodel/commit/c471f5ff6359e2ab06e9e9053b416dc4f19dd26a))
|
|
63
|
+
* update README with TypeID integration details ([6112888](https://github.com/iloveitaly/activemodel/commit/6112888a4ac0fc8601bf57daba638db6b5bc6788))
|
|
64
|
+
|
|
65
|
+
## [0.5.0](https://github.com/iloveitaly/activemodel/compare/v0.4.0...v0.5.0) (2024-11-28)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
### Features
|
|
69
|
+
|
|
70
|
+
* add methods for finding or creating records in model ([f1fd2f2](https://github.com/iloveitaly/activemodel/commit/f1fd2f2d65f182631b5df6a1ab20bf2f9a269607))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## [0.4.0](https://github.com/iloveitaly/activemodel/compare/v0.3.0...v0.4.0) (2024-11-26)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
### Bug Fixes
|
|
78
|
+
|
|
79
|
+
* update uv version to 0.5.4 ([1d77c39](https://github.com/iloveitaly/activemodel/commit/1d77c39e2234b98335e7206626dc2fdca0b34b79))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
### Features
|
|
83
|
+
|
|
84
|
+
* add database truncation functionality for tests ([177abf9](https://github.com/iloveitaly/activemodel/commit/177abf91c8b25295cb93878433132302f8caffbc))
|
|
85
|
+
* add FastAPISessionMaker for SQLAlchemy session management ([b80f045](https://github.com/iloveitaly/activemodel/commit/b80f045e52c908c7b1ae8721e42ee2796c8e85bc))
|
|
86
|
+
* add mixins for timestamps and TypeID handling ([51a6128](https://github.com/iloveitaly/activemodel/commit/51a6128e6d0f45e5801821b99e399ae0bcfca624))
|
|
87
|
+
* add python-decouple-typed as a dependency ([d495ad4](https://github.com/iloveitaly/activemodel/commit/d495ad419494c0c387a331a9d0ef4d112df83af7))
|
|
88
|
+
* add typeid-python dependency to project requirements ([998762f](https://github.com/iloveitaly/activemodel/commit/998762fda8f62a078fc3a4afa8c09f5af323dcfb))
|
|
89
|
+
* implement database reset methods for tests ([9cec29d](https://github.com/iloveitaly/activemodel/commit/9cec29d2ad89a871d4cbda2a6b8ad3a1ef50df6e))
|
|
90
|
+
* lots of stuff :) ([0510d20](https://github.com/iloveitaly/activemodel/commit/0510d20a807829db4f6b454ee2915c32ecedb323))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
## [0.3.0](https://github.com/iloveitaly/activemodel/compare/v0.2.0...v0.3.0) (2024-11-20)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
### Features
|
|
98
|
+
|
|
99
|
+
* add inspiration section to README and update TODO ([b11fb09](https://github.com/iloveitaly/activemodel/commit/b11fb09eb95e338de7358ba070fac0e75eda9909))
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
## [0.2.0](https://github.com/iloveitaly/activemodel/compare/193f839c9ace154e7aaa0a9770400031d0e67cd3...v0.2.0) (2024-11-16)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
### Bug Fixes
|
|
107
|
+
|
|
108
|
+
* update uv version and rename test file location ([9389fd2](https://github.com/iloveitaly/activemodel/commit/9389fd2e20e75ea322cd55bb699777978a9d282d))
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
### Features
|
|
112
|
+
|
|
113
|
+
* add Redis and Postgres services to CI workflow ([b9cb0ba](https://github.com/iloveitaly/activemodel/commit/b9cb0baca6bd46092437552ddf1d317a528983ca))
|
|
114
|
+
* automate tablename generation from camelCase ([193f839](https://github.com/iloveitaly/activemodel/commit/193f839c9ace154e7aaa0a9770400031d0e67cd3))
|
|
115
|
+
* enhance project description and keywords in config ([10c5707](https://github.com/iloveitaly/activemodel/commit/10c570786925fa9d7a397cb6f503b658df00aa4f))
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[macos]
|
|
2
|
+
db_play:
|
|
3
|
+
uv tool run pgcli $DATABASE_URL
|
|
4
|
+
|
|
5
|
+
up:
|
|
6
|
+
docker compose up -d
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
gh_configure:
|
|
10
|
+
repo_path=$(gh repo view --json nameWithOwner --jq '.nameWithOwner') && \
|
|
11
|
+
gh api --method PUT "/repos/${repo_path}/actions/permissions/workflow" \
|
|
12
|
+
-f default_workflow_permissions=write \
|
|
13
|
+
-F can_approve_pull_request_reviews=true && \
|
|
14
|
+
gh api "/repos/${repo_path}/actions/permissions/workflow"
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: activemodel
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: Make SQLModel more like an a real ORM
|
|
5
|
+
Project-URL: Repository, https://github.com/iloveitaly/activemodel
|
|
6
|
+
Author-email: Michael Bianco <iloveitaly@gmail.com>
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: activemodel,activerecord,orm,sqlalchemy,sqlmodel
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Requires-Dist: pydash>=8.0.4
|
|
11
|
+
Requires-Dist: python-decouple-typed>=3.11.0
|
|
12
|
+
Requires-Dist: sqlmodel>=0.0.22
|
|
13
|
+
Requires-Dist: typeid-python>=0.3.1
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# ActiveModel: ORM Wrapper for SQLModel
|
|
17
|
+
|
|
18
|
+
No, this isn't *really* [ActiveModel](https://guides.rubyonrails.org/active_model_basics.html). It's just a wrapper around SQLModel that provides a more ActiveRecord-like interface.
|
|
19
|
+
|
|
20
|
+
SQLModel is *not* an ORM. It's a SQL query builder and a schema definition tool.
|
|
21
|
+
|
|
22
|
+
This package provides a thin wrapper around SQLModel that provides a more ActiveRecord-like interface with things like:
|
|
23
|
+
|
|
24
|
+
* Timestamp column mixins
|
|
25
|
+
* Lifecycle hooks
|
|
26
|
+
|
|
27
|
+
## Getting Started
|
|
28
|
+
|
|
29
|
+
First, setup your DB:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Then, setup some models:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from activemodel import BaseModel
|
|
39
|
+
from activemodel.mixins import TimestampsMixin, TypeIDMixin
|
|
40
|
+
|
|
41
|
+
class User(
|
|
42
|
+
BaseModel,
|
|
43
|
+
# optionally, obviously
|
|
44
|
+
TimestampsMixin,
|
|
45
|
+
# you can use a different pk type, but why would you?
|
|
46
|
+
# put this mixin last otherwise `id` will not be the first column in the DB
|
|
47
|
+
TypeIDMixin("user"),
|
|
48
|
+
# wire this model into the DB, without this alembic will not generate a migration
|
|
49
|
+
table=True
|
|
50
|
+
):
|
|
51
|
+
a_field: str
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
### Integrating Alembic
|
|
57
|
+
|
|
58
|
+
`alembic init` will not work out of the box. You need to mutate a handful of files:
|
|
59
|
+
|
|
60
|
+
* To import all of your models you want in your DB. [Here's my recommended way to do this.](https://github.com/iloveitaly/python-starter-template/blob/master/app/models/__init__.py)
|
|
61
|
+
* Use your DB URL from the ENV
|
|
62
|
+
* Target sqlalchemy metadata to the sqlmodel-generated metadata
|
|
63
|
+
|
|
64
|
+
[Take a look at these scripts for an example of how to fully integrate Alembic into your development workflow.](https://github.com/iloveitaly/python-starter-template/blob/0af2c7e95217e34bde7357cc95be048900000e48/Justfile#L618-L712)
|
|
65
|
+
|
|
66
|
+
Here's a diff from the bare `alembic init` from version `1.14.1`.
|
|
67
|
+
|
|
68
|
+
```diff
|
|
69
|
+
diff --git i/test/migrations/alembic.ini w/test/migrations/alembic.ini
|
|
70
|
+
index 0d07420..a63631c 100644
|
|
71
|
+
--- i/test/migrations/alembic.ini
|
|
72
|
+
+++ w/test/migrations/alembic.ini
|
|
73
|
+
@@ -3,13 +3,14 @@
|
|
74
|
+
[alembic]
|
|
75
|
+
# path to migration scripts
|
|
76
|
+
# Use forward slashes (/) also on windows to provide an os agnostic path
|
|
77
|
+
-script_location = .
|
|
78
|
+
+script_location = migrations
|
|
79
|
+
|
|
80
|
+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
|
81
|
+
# Uncomment the line below if you want the files to be prepended with date and time
|
|
82
|
+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
|
|
83
|
+
# for all available tokens
|
|
84
|
+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
|
85
|
+
+file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(rev)s_%%(slug)s
|
|
86
|
+
|
|
87
|
+
# sys.path path, will be prepended to sys.path if present.
|
|
88
|
+
# defaults to the current working directory.
|
|
89
|
+
diff --git i/test/migrations/env.py w/test/migrations/env.py
|
|
90
|
+
index 36112a3..a1e15c2 100644
|
|
91
|
+
--- i/test/migrations/env.py
|
|
92
|
+
+++ w/test/migrations/env.py
|
|
93
|
+
@@ -1,3 +1,6 @@
|
|
94
|
+
+# fmt: off
|
|
95
|
+
+# isort: off
|
|
96
|
+
+
|
|
97
|
+
from logging.config import fileConfig
|
|
98
|
+
|
|
99
|
+
from sqlalchemy import engine_from_config
|
|
100
|
+
@@ -14,11 +17,17 @@ config = context.config
|
|
101
|
+
if config.config_file_name is not None:
|
|
102
|
+
fileConfig(config.config_file_name)
|
|
103
|
+
|
|
104
|
+
+from sqlmodel import SQLModel
|
|
105
|
+
+from test.models import *
|
|
106
|
+
+from test.utils import database_url
|
|
107
|
+
+
|
|
108
|
+
+config.set_main_option("sqlalchemy.url", database_url())
|
|
109
|
+
+
|
|
110
|
+
# add your model's MetaData object here
|
|
111
|
+
# for 'autogenerate' support
|
|
112
|
+
# from myapp import mymodel
|
|
113
|
+
# target_metadata = mymodel.Base.metadata
|
|
114
|
+
-target_metadata = None
|
|
115
|
+
+target_metadata = SQLModel.metadata
|
|
116
|
+
|
|
117
|
+
# other values from the config, defined by the needs of env.py,
|
|
118
|
+
# can be acquired:
|
|
119
|
+
diff --git i/test/migrations/script.py.mako w/test/migrations/script.py.mako
|
|
120
|
+
index fbc4b07..9dc78bb 100644
|
|
121
|
+
--- i/test/migrations/script.py.mako
|
|
122
|
+
+++ w/test/migrations/script.py.mako
|
|
123
|
+
@@ -9,6 +9,8 @@ from typing import Sequence, Union
|
|
124
|
+
|
|
125
|
+
from alembic import op
|
|
126
|
+
import sqlalchemy as sa
|
|
127
|
+
+import sqlmodel
|
|
128
|
+
+import activemodel
|
|
129
|
+
${imports if imports else ""}
|
|
130
|
+
|
|
131
|
+
# revision identifiers, used by Alembic.
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Here are some useful resources around Alembic + SQLModel:
|
|
135
|
+
|
|
136
|
+
* https://github.com/fastapi/sqlmodel/issues/85
|
|
137
|
+
* https://testdriven.io/blog/fastapi-sqlmodel/
|
|
138
|
+
|
|
139
|
+
### Query Wrapper
|
|
140
|
+
|
|
141
|
+
This tool is added to all `BaseModel`s and makes it easy to write SQL queries. Some examples:
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
### Easy Database Sessions
|
|
146
|
+
|
|
147
|
+
I hate the idea f
|
|
148
|
+
|
|
149
|
+
* Behavior should be intuitive and easy to understand. If you run `save()`, it should save, not stick the save in a transaction.
|
|
150
|
+
* Don't worry about dead sessions. This makes it easy to lazy-load computed properties and largely eliminates the need to think about database sessions.
|
|
151
|
+
|
|
152
|
+
There are a couple of thorny problems we need to solve for here:
|
|
153
|
+
|
|
154
|
+
* In-memory fastapi servers are not the same as a uvicorn server, which is threaded *and* uses some sort of threadpool model for handling async requests. I don't claim to understand the entire implementation. For global DB session state (a) we can't use global variables (b) we can't use thread-local variables.
|
|
155
|
+
*
|
|
156
|
+
|
|
157
|
+
https://github.com/tomwojcik/starlette-context
|
|
158
|
+
|
|
159
|
+
### Example Queries
|
|
160
|
+
|
|
161
|
+
* Conditional: `Scrape.select().where(Scrape.id < last_scraped.id).all()`
|
|
162
|
+
* Equality: `MenuItem.select().where(MenuItem.menu_id == menu.id).all()`
|
|
163
|
+
* `IN` example: `CanonicalMenuItem.select().where(col(CanonicalMenuItem.id).in_(canonized_ids)).all()`
|
|
164
|
+
|
|
165
|
+
### TypeID
|
|
166
|
+
|
|
167
|
+
I'm a massive fan of Stripe-style prefixed UUIDs. [There's an excellent project](https://github.com/jetify-com/typeid)
|
|
168
|
+
that defined a clear spec for these IDs. I've used the python implementation of this spec and developed a clean integration
|
|
169
|
+
with SQLModel that plays well with fastapi as well.
|
|
170
|
+
|
|
171
|
+
Here's an example of defining a relationship:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
import uuid
|
|
175
|
+
|
|
176
|
+
from activemodel import BaseModel
|
|
177
|
+
from activemodel.mixins import TimestampsMixin, TypeIDMixin
|
|
178
|
+
from activemodel.types import TypeIDType
|
|
179
|
+
from sqlmodel import Field, Relationship
|
|
180
|
+
|
|
181
|
+
from .patient import Patient
|
|
182
|
+
|
|
183
|
+
class Appointment(
|
|
184
|
+
BaseModel,
|
|
185
|
+
# this adds an `id` field to the model with the correct type
|
|
186
|
+
TypeIDMixin("appointment"),
|
|
187
|
+
table=True
|
|
188
|
+
):
|
|
189
|
+
# `foreign_key` is a activemodel-specific method to generate the right `Field` for the relationship
|
|
190
|
+
# TypeIDType is really important here for fastapi serialization
|
|
191
|
+
doctor_id: TypeIDType = Doctor.foreign_key()
|
|
192
|
+
doctor: Doctor = Relationship()
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Limitations
|
|
196
|
+
|
|
197
|
+
### Validation
|
|
198
|
+
|
|
199
|
+
SQLModel does not currently support pydantic validations (when `table=True`). This is very surprising, but is actually the intended functionality:
|
|
200
|
+
|
|
201
|
+
* https://github.com/fastapi/sqlmodel/discussions/897
|
|
202
|
+
* https://github.com/fastapi/sqlmodel/pull/1041
|
|
203
|
+
* https://github.com/fastapi/sqlmodel/issues/453
|
|
204
|
+
* https://github.com/fastapi/sqlmodel/issues/52#issuecomment-1311987732
|
|
205
|
+
|
|
206
|
+
For validation:
|
|
207
|
+
|
|
208
|
+
* When consuming API data, use a separate shadow model to validate the data with `table=False` and then inherit from that model in a model with `table=True`.
|
|
209
|
+
* When validating ORM data, use SQL Alchemy hooks.
|
|
210
|
+
|
|
211
|
+
<!--
|
|
212
|
+
|
|
213
|
+
This looks neat
|
|
214
|
+
https://github.com/DarylStark/my_data/blob/a17b8b3a8463b9953821b89fee895e272f94d2a4/src/my_model/model.py#L155
|
|
215
|
+
schema_extra={
|
|
216
|
+
'pattern': r'^[a-z0-9_\-\.]+\@[a-z0-9_\-\.]+\.[a-z\.]+$'
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
extra constraints
|
|
220
|
+
|
|
221
|
+
https://github.com/DarylStark/my_data/blob/a17b8b3a8463b9953821b89fee895e272f94d2a4/src/my_model/model.py#L424C1-L426C6
|
|
222
|
+
-->
|
|
223
|
+
## Related Projects
|
|
224
|
+
|
|
225
|
+
* https://github.com/woofz/sqlmodel-basecrud
|
|
226
|
+
* https://github.com/0xthiagomartins/sqlmodel-controller
|
|
227
|
+
|
|
228
|
+
## Inspiration
|
|
229
|
+
|
|
230
|
+
* https://github.com/peterdresslar/fastapi-sqlmodel-alembic-pg
|
|
231
|
+
* [Albemic instructions](https://github.com/fastapi/sqlmodel/pull/899/files)
|
|
232
|
+
* https://github.com/fastapiutils/fastapi-utils/
|
|
233
|
+
* https://github.com/fastapi/full-stack-fastapi-template
|
|
234
|
+
* https://github.com/DarylStark/my_data/
|
|
235
|
+
* https://github.com/petrgazarov/FastAPI-app/tree/main/fastapi_app
|