sqlalchemy-postgres-point 0.1.0__py3-none-any.whl → 0.2.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.
- sqlalchemy_postgres_point/alembic_integration.py +56 -0
- {sqlalchemy_postgres_point-0.1.0.dist-info → sqlalchemy_postgres_point-0.2.0.dist-info}/METADATA +85 -2
- sqlalchemy_postgres_point-0.2.0.dist-info/RECORD +7 -0
- {sqlalchemy_postgres_point-0.1.0.dist-info → sqlalchemy_postgres_point-0.2.0.dist-info}/WHEEL +2 -2
- sqlalchemy_postgres_point-0.1.0.dist-info/RECORD +0 -6
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Alembic integration to automatically add imports for PointType in migration files.
|
|
3
|
+
|
|
4
|
+
This module hooks into Alembic's autogenerate system to automatically include
|
|
5
|
+
the necessary import statement when PointType is detected in models.
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
Add this import to your alembic/env.py file:
|
|
9
|
+
|
|
10
|
+
import sqlalchemy_postgres_point.alembic_integration
|
|
11
|
+
|
|
12
|
+
Once added, `from sqlalchemy_postgres_point import PointType` will be automatically
|
|
13
|
+
included in generated migration files.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import alembic.autogenerate
|
|
17
|
+
import alembic.operations.ops
|
|
18
|
+
from sqlalchemy.types import UserDefinedType
|
|
19
|
+
|
|
20
|
+
from .point import PointType
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def render_point_type(type_, object_, autogen_context):
|
|
24
|
+
"""Render PointType in migration files with proper import."""
|
|
25
|
+
# Add the import to the migration file
|
|
26
|
+
autogen_context.imports.add("from sqlalchemy_postgres_point import PointType")
|
|
27
|
+
return "PointType()"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# Register the renderer for PointType
|
|
31
|
+
alembic.autogenerate.render.renderers.dispatch_for(PointType, replace=True)(
|
|
32
|
+
render_point_type
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# Also register for UserDefinedType as a fallback if the class doesn't match exactly
|
|
37
|
+
@alembic.autogenerate.render.renderers.dispatch_for(UserDefinedType, replace=True)
|
|
38
|
+
def render_user_defined_type(type_, object_, autogen_context):
|
|
39
|
+
if type(object_).__name__ == "PointType":
|
|
40
|
+
return render_point_type(type_, object_, autogen_context)
|
|
41
|
+
return False
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@alembic.autogenerate.comparators.dispatch_for("schema")
|
|
45
|
+
def compare_point_types(
|
|
46
|
+
autogen_context: alembic.autogenerate.api.AutogenContext,
|
|
47
|
+
upgrade_ops: alembic.operations.ops.UpgradeOps,
|
|
48
|
+
schema_names,
|
|
49
|
+
):
|
|
50
|
+
"""
|
|
51
|
+
Hook into Alembic's schema comparison to detect PointType usage.
|
|
52
|
+
|
|
53
|
+
This ensures the import is added even if PointType is used in existing
|
|
54
|
+
columns that might not trigger the render hook directly.
|
|
55
|
+
"""
|
|
56
|
+
pass
|
{sqlalchemy_postgres_point-0.1.0.dist-info → sqlalchemy_postgres_point-0.2.0.dist-info}/METADATA
RENAMED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: sqlalchemy-postgres-point
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Lightweight SQLAlchemy custom type for PostgreSQL POINT columns with tuple binding and an earth-distance comparator.
|
|
5
5
|
Keywords: sqlalchemy,postgresql,point,geometry,geospatial,gis,database,types,earthdistance
|
|
6
6
|
Author: Michael Bianco
|
|
7
7
|
Author-email: Michael Bianco <mike@mikebian.co>
|
|
8
8
|
Requires-Dist: sqlalchemy>=2.0
|
|
9
|
-
Requires-Python: >=3.
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
10
|
Project-URL: Repository, https://github.com/iloveitaly/sqlalchemy-postgres-point
|
|
11
11
|
Description-Content-Type: text/markdown
|
|
12
12
|
|
|
13
|
+
[](https://github.com/iloveitaly/sqlalchemy-postgres-point/releases)
|
|
14
|
+
[](https://pepy.tech/project/sqlalchemy-postgres-point)
|
|
15
|
+

|
|
16
|
+
[](https://opensource.org/licenses/MIT)
|
|
17
|
+
|
|
13
18
|
sqlalchemy-postgres-point
|
|
14
19
|
=========================
|
|
15
20
|
|
|
@@ -72,6 +77,54 @@ stmt = select(Place.id, Place.location.earth_distance(origin).label("dist"))
|
|
|
72
77
|
|
|
73
78
|
The comparator translates `Place.location.earth_distance(origin)` into SQL using the `<@>` operator (requires PostgreSQL with the `cube` / `earthdistance` extension for meaningful results; without extensions the operator may not exist—adapt as needed for your environment). This library only *emits* the operator; it does not manage PostgreSQL extensions.
|
|
74
79
|
|
|
80
|
+
## PostgreSQL Extensions Setup
|
|
81
|
+
|
|
82
|
+
To use the earth distance functionality (`earth_distance()` comparator), you need to enable the `cube` and `earthdistance` PostgreSQL extensions. These extensions provide spatial operations for calculating distances between geographic points on Earth's surface using a spherical model.
|
|
83
|
+
|
|
84
|
+
### Alembic Migration Example
|
|
85
|
+
|
|
86
|
+
If you're using Alembic for database migrations, you can create a migration to enable these extensions:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
"""Add PostgreSQL extensions for earth distance calculations
|
|
90
|
+
|
|
91
|
+
Revision ID: your_revision_id
|
|
92
|
+
Revises: your_previous_revision
|
|
93
|
+
Create Date: 2025-01-XX XX:XX:XX.XXXXXX
|
|
94
|
+
|
|
95
|
+
"""
|
|
96
|
+
from typing import Sequence, Union
|
|
97
|
+
|
|
98
|
+
from alembic import op
|
|
99
|
+
import sqlalchemy as sa
|
|
100
|
+
|
|
101
|
+
# revision identifiers, used by Alembic.
|
|
102
|
+
revision: str = 'your_revision_id'
|
|
103
|
+
down_revision: Union[str, None] = 'your_previous_revision'
|
|
104
|
+
branch_labels: Union[str, Sequence[str], None] = None
|
|
105
|
+
depends_on: Union[str, Sequence[str], None] = None
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def upgrade() -> None:
|
|
109
|
+
op.execute("""
|
|
110
|
+
CREATE EXTENSION IF NOT EXISTS cube;
|
|
111
|
+
CREATE EXTENSION IF NOT EXISTS earthdistance;
|
|
112
|
+
""")
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def downgrade() -> None:
|
|
116
|
+
op.execute("""
|
|
117
|
+
DROP EXTENSION IF EXISTS earthdistance;
|
|
118
|
+
DROP EXTENSION IF EXISTS cube;
|
|
119
|
+
""")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Integration Steps:**
|
|
123
|
+
|
|
124
|
+
1. Generate a new migration: `alembic revision -m "add_postgres_extensions"`
|
|
125
|
+
2. Copy the `upgrade()` and `downgrade()` functions above into your new migration file
|
|
126
|
+
3. Run the migration: `alembic upgrade head`
|
|
127
|
+
|
|
75
128
|
Returned Python Values
|
|
76
129
|
----------------------
|
|
77
130
|
|
|
@@ -86,6 +139,33 @@ Run the test suite with:
|
|
|
86
139
|
uv run pytest -q
|
|
87
140
|
```
|
|
88
141
|
|
|
142
|
+
## Alembic Integration
|
|
143
|
+
|
|
144
|
+
When using `PointType` in your models, you can automatically include the necessary imports in migration files by updating your `alembic/env.py` file.
|
|
145
|
+
|
|
146
|
+
First, import the integration module:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
import sqlalchemy_postgres_point.alembic_integration
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Then, ensure your `context.configure` call includes a `render_item` hook to handle the `PointType`:
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
def render_item(type_, obj, autogen_context):
|
|
156
|
+
if type_ == "type" and type(obj).__name__ == "PointType":
|
|
157
|
+
return sqlalchemy_postgres_point.alembic_integration.render_point_type(obj, obj, autogen_context)
|
|
158
|
+
return False
|
|
159
|
+
|
|
160
|
+
# ... in run_migrations_online and run_migrations_offline ...
|
|
161
|
+
context.configure(
|
|
162
|
+
# ... other options ...
|
|
163
|
+
render_item=render_item,
|
|
164
|
+
)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Once added, `from sqlalchemy_postgres_point import PointType` will be automatically included in generated migration files whenever a `PointType` column is detected.
|
|
168
|
+
|
|
89
169
|
Development
|
|
90
170
|
-----------
|
|
91
171
|
|
|
@@ -114,3 +194,6 @@ License
|
|
|
114
194
|
|
|
115
195
|
MIT (see your project's LICENSE file if added later). Contributions welcome.
|
|
116
196
|
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
*This project was created from [iloveitaly/python-package-template](https://github.com/iloveitaly/python-package-template)*
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
sqlalchemy_postgres_point/__init__.py,sha256=BWlOd4qbAt4fM4cclR7ZrzZ-aib0f13KD3HIKmod2_s,208
|
|
2
|
+
sqlalchemy_postgres_point/alembic_integration.py,sha256=qaMk733hniatgGHwJXl0S5cKIsu_tno4JRvJ_MDLtPs,1840
|
|
3
|
+
sqlalchemy_postgres_point/point.py,sha256=pb_aoNKwMOaKJ_xDvaIhfE-4VZKzyiPgOWmM6_z6xXo,2888
|
|
4
|
+
sqlalchemy_postgres_point/utils.py,sha256=Loc9Q-xkntEKP3WSw48rZr64aqPWdM9huBlJnGuZa_8,1340
|
|
5
|
+
sqlalchemy_postgres_point-0.2.0.dist-info/WHEEL,sha256=QnGI6l9Psotmz6XrseXTYT5jnZRJeTk4SwJP4aLtfdI,80
|
|
6
|
+
sqlalchemy_postgres_point-0.2.0.dist-info/METADATA,sha256=4haIARMrgBsJi5JKaBp01PZ5Cuv8EnHr-ocd6XUL4hA,6367
|
|
7
|
+
sqlalchemy_postgres_point-0.2.0.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
sqlalchemy_postgres_point/__init__.py,sha256=BWlOd4qbAt4fM4cclR7ZrzZ-aib0f13KD3HIKmod2_s,208
|
|
2
|
-
sqlalchemy_postgres_point/point.py,sha256=pb_aoNKwMOaKJ_xDvaIhfE-4VZKzyiPgOWmM6_z6xXo,2888
|
|
3
|
-
sqlalchemy_postgres_point/utils.py,sha256=Loc9Q-xkntEKP3WSw48rZr64aqPWdM9huBlJnGuZa_8,1340
|
|
4
|
-
sqlalchemy_postgres_point-0.1.0.dist-info/WHEEL,sha256=Pi5uDq5Fdo_Rr-HD5h9BiPn9Et29Y9Sh8NhcJNnFU1c,79
|
|
5
|
-
sqlalchemy_postgres_point-0.1.0.dist-info/METADATA,sha256=zPw5cM0f9dCWP_jrYp_WVXEW7TuwTNwXP1EUu10g1u8,3322
|
|
6
|
-
sqlalchemy_postgres_point-0.1.0.dist-info/RECORD,,
|