polypheny-polynom 0.1.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.
- polypheny_polynom-0.1.0/LICENSE +201 -0
- polypheny_polynom-0.1.0/MANIFEST.in +2 -0
- polypheny_polynom-0.1.0/PKG-INFO +56 -0
- polypheny_polynom-0.1.0/README.md +25 -0
- polypheny_polynom-0.1.0/polyNOM-version.txt +1 -0
- polypheny_polynom-0.1.0/polynom/__init__.py +0 -0
- polypheny_polynom-0.1.0/polynom/application.py +285 -0
- polypheny_polynom-0.1.0/polynom/config.py +89 -0
- polypheny_polynom-0.1.0/polynom/joined_load.py +15 -0
- polypheny_polynom-0.1.0/polynom/model.py +88 -0
- polypheny_polynom-0.1.0/polynom/query.py +247 -0
- polypheny_polynom-0.1.0/polynom/reflection.py +45 -0
- polypheny_polynom-0.1.0/polynom/schema/__init__.py +0 -0
- polypheny_polynom-0.1.0/polynom/schema/cascade.py +19 -0
- polypheny_polynom-0.1.0/polynom/schema/field.py +88 -0
- polypheny_polynom-0.1.0/polynom/schema/migration.py +119 -0
- polypheny_polynom-0.1.0/polynom/schema/polytypes.py +264 -0
- polypheny_polynom-0.1.0/polynom/schema/relationship.py +67 -0
- polypheny_polynom-0.1.0/polynom/schema/schema.py +35 -0
- polypheny_polynom-0.1.0/polynom/schema/schema_registry.py +52 -0
- polypheny_polynom-0.1.0/polynom/session.py +260 -0
- polypheny_polynom-0.1.0/polypheny_polynom.egg-info/PKG-INFO +56 -0
- polypheny_polynom-0.1.0/polypheny_polynom.egg-info/SOURCES.txt +36 -0
- polypheny_polynom-0.1.0/polypheny_polynom.egg-info/dependency_links.txt +1 -0
- polypheny_polynom-0.1.0/polypheny_polynom.egg-info/requires.txt +6 -0
- polypheny_polynom-0.1.0/polypheny_polynom.egg-info/top_level.txt +2 -0
- polypheny_polynom-0.1.0/requirements.txt +4 -0
- polypheny_polynom-0.1.0/setup.cfg +4 -0
- polypheny_polynom-0.1.0/setup.py +71 -0
- polypheny_polynom-0.1.0/tests/__init__.py +0 -0
- polypheny_polynom-0.1.0/tests/config_test.py +93 -0
- polypheny_polynom-0.1.0/tests/model.py +34 -0
- polypheny_polynom-0.1.0/tests/schema.py +34 -0
- polypheny_polynom-0.1.0/tests/test_model.py +99 -0
- polypheny_polynom-0.1.0/tests/test_polytypes.py +209 -0
- polypheny_polynom-0.1.0/tests/test_query.py +311 -0
- polypheny_polynom-0.1.0/tests/test_relationship.py +57 -0
- polypheny_polynom-0.1.0/tests/test_session.py +423 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: polypheny-polynom
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Object Mapper for Polypheny
|
|
5
|
+
Home-page: https://polypheny.com/
|
|
6
|
+
Author: The Polypheny Project
|
|
7
|
+
Author-email: mail@polypheny.org
|
|
8
|
+
License: Apache License, Version 2.0
|
|
9
|
+
Project-URL: Documentation, https://github.com/polypheny/PolyNOM/docs
|
|
10
|
+
Project-URL: Code, https://github.com/polypheny/PolyNOM
|
|
11
|
+
Requires-Python: >=3.11
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: polypheny
|
|
15
|
+
Requires-Dist: docker
|
|
16
|
+
Requires-Dist: shapely
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest; extra == "dev"
|
|
19
|
+
Dynamic: author
|
|
20
|
+
Dynamic: author-email
|
|
21
|
+
Dynamic: description
|
|
22
|
+
Dynamic: description-content-type
|
|
23
|
+
Dynamic: home-page
|
|
24
|
+
Dynamic: license
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
Dynamic: project-url
|
|
27
|
+
Dynamic: provides-extra
|
|
28
|
+
Dynamic: requires-dist
|
|
29
|
+
Dynamic: requires-python
|
|
30
|
+
Dynamic: summary
|
|
31
|
+
|
|
32
|
+
PolyNOM — Polypheny Native Object Mapper
|
|
33
|
+
|
|
34
|
+
**PolyNOM** is a powerful Python-based object mapping layer designed to work seamlessly with [Polypheny](https://polypheny.com), the flexible and distributed data management platform. It leverages the [`Polypheny Connector Python`](https://github.com/polypheny/Polypheny-Connector-Python) to simplify the development process by providing automatic object mapping, schema migration, and changelog tracking.
|
|
35
|
+
|
|
36
|
+
PolyNOM allows developers to work with Python classes and objects, while automatically handling the communication and translation with a Polypheny instance — either via Docker or direct host/port configuration.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 🚀 Features
|
|
41
|
+
|
|
42
|
+
- ✅ **Object Mapping**
|
|
43
|
+
Map native Python classes to Polypheny collections with minimal boilerplate.
|
|
44
|
+
|
|
45
|
+
- 🔄 **Automatic Schema Migration**
|
|
46
|
+
Define your data model in Python and let PolyNOM handle schema creation and updates on the fly.
|
|
47
|
+
|
|
48
|
+
- 📜 **Changelog Support**
|
|
49
|
+
Track and log structural changes applied to your schema over time for better versioning and auditing.
|
|
50
|
+
|
|
51
|
+
- 🐳 **Automatic Docker Deployment**
|
|
52
|
+
Easily spin up a local Polypheny instance using Docker in one command.
|
|
53
|
+
|
|
54
|
+
- ⚡ **Fast and Lightweight**
|
|
55
|
+
Built with simplicity and performance in mind, ideal for rapid development and prototyping.
|
|
56
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
PolyNOM — Polypheny Native Object Mapper
|
|
2
|
+
|
|
3
|
+
**PolyNOM** is a powerful Python-based object mapping layer designed to work seamlessly with [Polypheny](https://polypheny.com), the flexible and distributed data management platform. It leverages the [`Polypheny Connector Python`](https://github.com/polypheny/Polypheny-Connector-Python) to simplify the development process by providing automatic object mapping, schema migration, and changelog tracking.
|
|
4
|
+
|
|
5
|
+
PolyNOM allows developers to work with Python classes and objects, while automatically handling the communication and translation with a Polypheny instance — either via Docker or direct host/port configuration.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 🚀 Features
|
|
10
|
+
|
|
11
|
+
- ✅ **Object Mapping**
|
|
12
|
+
Map native Python classes to Polypheny collections with minimal boilerplate.
|
|
13
|
+
|
|
14
|
+
- 🔄 **Automatic Schema Migration**
|
|
15
|
+
Define your data model in Python and let PolyNOM handle schema creation and updates on the fly.
|
|
16
|
+
|
|
17
|
+
- 📜 **Changelog Support**
|
|
18
|
+
Track and log structural changes applied to your schema over time for better versioning and auditing.
|
|
19
|
+
|
|
20
|
+
- 🐳 **Automatic Docker Deployment**
|
|
21
|
+
Easily spin up a local Polypheny instance using Docker in one command.
|
|
22
|
+
|
|
23
|
+
- ⚡ **Fast and Lightweight**
|
|
24
|
+
Built with simplicity and performance in mind, ideal for rapid development and prototyping.
|
|
25
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v0.1.0
|
|
File without changes
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import polypheny
|
|
2
|
+
import docker
|
|
3
|
+
import logging
|
|
4
|
+
import socket
|
|
5
|
+
import time
|
|
6
|
+
import polynom.config as cfg
|
|
7
|
+
from polynom.schema.migration import Migrator
|
|
8
|
+
from polynom.session import Session
|
|
9
|
+
from docker.errors import DockerException, NotFound, ImageNotFound
|
|
10
|
+
from polynom.schema.schema_registry import _get_ordered_schemas, _to_json
|
|
11
|
+
from polynom.schema.field import PrimaryKeyField, ForeignKeyField
|
|
12
|
+
from polynom.reflection import SchemaSnapshot, SchemaSnapshotSchema
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
class Application:
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
app_uuid: str,
|
|
20
|
+
address,
|
|
21
|
+
user: str = cfg.get(cfg.DEFAULT_USER),
|
|
22
|
+
password: str = cfg.get(cfg.DEFAULT_PASS),
|
|
23
|
+
transport: str = cfg.get(cfg.DEFAULT_TRANSPORT),
|
|
24
|
+
use_docker: bool = True,
|
|
25
|
+
migrate: bool = False,
|
|
26
|
+
stop_container: bool = False,
|
|
27
|
+
remove_container: bool = False
|
|
28
|
+
):
|
|
29
|
+
cfg.lock()
|
|
30
|
+
|
|
31
|
+
self._app_uuid = app_uuid
|
|
32
|
+
self._address = address
|
|
33
|
+
self._user = user
|
|
34
|
+
self._password = password
|
|
35
|
+
self._transport = transport
|
|
36
|
+
self._use_docker = use_docker
|
|
37
|
+
self._migrate = migrate
|
|
38
|
+
self._stop_container = stop_container
|
|
39
|
+
self._remove_container = remove_container
|
|
40
|
+
|
|
41
|
+
self._conn = None
|
|
42
|
+
self._cursor = None
|
|
43
|
+
self._initialized = False
|
|
44
|
+
|
|
45
|
+
def __enter__(self):
|
|
46
|
+
if self._initialized:
|
|
47
|
+
raise ValueError("Application must only be initialized once.")
|
|
48
|
+
self._initialized = True
|
|
49
|
+
|
|
50
|
+
if self._use_docker:
|
|
51
|
+
self._deploy_polypheny()
|
|
52
|
+
|
|
53
|
+
self._conn = polypheny.connect(
|
|
54
|
+
self._address,
|
|
55
|
+
username=self._user,
|
|
56
|
+
password=self._password,
|
|
57
|
+
transport=self._transport
|
|
58
|
+
)
|
|
59
|
+
self._cursor = self._conn.cursor()
|
|
60
|
+
|
|
61
|
+
self._verify_schema()
|
|
62
|
+
self._create_schema()
|
|
63
|
+
return self
|
|
64
|
+
|
|
65
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
66
|
+
if self._cursor:
|
|
67
|
+
self._cursor.close()
|
|
68
|
+
if self._conn:
|
|
69
|
+
self._conn.close()
|
|
70
|
+
|
|
71
|
+
if not self._use_docker:
|
|
72
|
+
return
|
|
73
|
+
if self._stop_container:
|
|
74
|
+
self._stop_container_by_name(cfg.get(cfg.POLYPHENY_CONTAINER_NAME))
|
|
75
|
+
if self._remove_container:
|
|
76
|
+
self._remove_container_by_name(cfg.get(cfg.POLYPHENY_CONTAINER_NAME))
|
|
77
|
+
cfg.unlock()
|
|
78
|
+
|
|
79
|
+
def _wait_for_prism(self, timeout=60):
|
|
80
|
+
start_time = time.time()
|
|
81
|
+
while time.time() - start_time < timeout:
|
|
82
|
+
try:
|
|
83
|
+
conn = polypheny.connect(
|
|
84
|
+
self._address,
|
|
85
|
+
username=self._user,
|
|
86
|
+
password=self._password,
|
|
87
|
+
transport=self._transport
|
|
88
|
+
)
|
|
89
|
+
conn.close()
|
|
90
|
+
return
|
|
91
|
+
except EOFError:
|
|
92
|
+
time.sleep(1)
|
|
93
|
+
except Exception as e:
|
|
94
|
+
raise RuntimeError(f"Unexpected error while connecting to Polypheny: {e}") from e
|
|
95
|
+
raise TimeoutError("Timed out waiting for Polypheny to become available.")
|
|
96
|
+
|
|
97
|
+
def _deploy_polypheny(self):
|
|
98
|
+
logger.info("Establishing connection to Docker...")
|
|
99
|
+
try:
|
|
100
|
+
client = docker.from_env()
|
|
101
|
+
client.ping()
|
|
102
|
+
except DockerException as e:
|
|
103
|
+
logger.error("Docker is not running or not accessible.")
|
|
104
|
+
raise RuntimeError("Docker is not running or not accessible.") from e
|
|
105
|
+
|
|
106
|
+
container_name = cfg.get(cfg.POLYPHENY_CONTAINER_NAME)
|
|
107
|
+
image_name = cfg.get(cfg.POLYPHENY_IMAGE_NAME)
|
|
108
|
+
ports = cfg.get(cfg.POLYPHENY_PORTS)
|
|
109
|
+
|
|
110
|
+
try:
|
|
111
|
+
logger.info(f"Checking for presence of Polypheny container '{container_name}'...")
|
|
112
|
+
container = client.containers.get(container_name)
|
|
113
|
+
container.start()
|
|
114
|
+
logger.info(f"Container '{container_name}' found and started.")
|
|
115
|
+
except NotFound:
|
|
116
|
+
logger.info("Polypheny container not found. Deploying a new container. This may take a moment...")
|
|
117
|
+
try:
|
|
118
|
+
client.images.pull(image_name)
|
|
119
|
+
container = client.containers.run(
|
|
120
|
+
image_name,
|
|
121
|
+
name=container_name,
|
|
122
|
+
ports=ports,
|
|
123
|
+
detach=True
|
|
124
|
+
)
|
|
125
|
+
logger.info(f"New Polypheny container '{container_name}' deployed and started.")
|
|
126
|
+
except DockerException as e:
|
|
127
|
+
logger.error(f"Failed to create or run the Polypheny container: {e}")
|
|
128
|
+
raise RuntimeError("Failed to create or run the Polypheny container.") from e
|
|
129
|
+
|
|
130
|
+
logger.info(f"Waiting for Polypheny Prism to become available at {self._address,}...")
|
|
131
|
+
try:
|
|
132
|
+
self._wait_for_prism()
|
|
133
|
+
logger.info("Polypheny Prism is now responsive.")
|
|
134
|
+
except TimeoutError as e:
|
|
135
|
+
logger.error(str(e))
|
|
136
|
+
raise RuntimeError("Polypheny container did not become ready in time.") from e
|
|
137
|
+
|
|
138
|
+
def _verify_schema(self):
|
|
139
|
+
self._process_schema(SchemaSnapshotSchema)
|
|
140
|
+
|
|
141
|
+
session = Session(self)
|
|
142
|
+
with session:
|
|
143
|
+
logger.debug(f"Reading schema snapshot from database for application {self._app_uuid}.")
|
|
144
|
+
previous = SchemaSnapshot.query(session).get(self._app_uuid)
|
|
145
|
+
current_snapshot = _to_json()
|
|
146
|
+
|
|
147
|
+
if not previous:
|
|
148
|
+
logger.debug(f"No schema snapshot found for application {self._app_uuid}. Creating a first one.")
|
|
149
|
+
previous = SchemaSnapshot(current_snapshot, _entry_id=self._app_uuid)
|
|
150
|
+
session.add(previous)
|
|
151
|
+
session.commit()
|
|
152
|
+
return
|
|
153
|
+
|
|
154
|
+
logger.debug(f"Checking for schema changes for application {self._app_uuid}.")
|
|
155
|
+
diff = self._compare_snapshots(previous.snapshot, current_snapshot)
|
|
156
|
+
|
|
157
|
+
if diff and self._migrate:
|
|
158
|
+
logger.debug(f"Schema changes for application {self._app_uuid} found.")
|
|
159
|
+
migrator = Migrator()
|
|
160
|
+
migrator.run(session, diff)
|
|
161
|
+
|
|
162
|
+
previous.snapshot = current_snapshot
|
|
163
|
+
session.commit()
|
|
164
|
+
|
|
165
|
+
def _create_schema(self):
|
|
166
|
+
for schema in _get_ordered_schemas():
|
|
167
|
+
self._process_schema(schema)
|
|
168
|
+
|
|
169
|
+
def _compare_snapshots(self, previous, current):
|
|
170
|
+
diff = {}
|
|
171
|
+
|
|
172
|
+
current_schemas = {s['entity_name']: s for s in current['schemas']}
|
|
173
|
+
previous_schemas = {s['entity_name']: s for s in previous['schemas']}
|
|
174
|
+
|
|
175
|
+
for entity_name, prev_entity in previous_schemas.items():
|
|
176
|
+
curr_entity = current_schemas.get(entity_name)
|
|
177
|
+
|
|
178
|
+
entity_diff = {
|
|
179
|
+
'namespace_name': prev_entity.get('namespace_name'),
|
|
180
|
+
'changes': {}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if not curr_entity:
|
|
184
|
+
diff[entity_name] = entity_diff
|
|
185
|
+
continue
|
|
186
|
+
|
|
187
|
+
prev_fields = {f['name']: f for f in prev_entity.get('fields', [])}
|
|
188
|
+
curr_fields = {f['name']: f for f in curr_entity.get('fields', [])}
|
|
189
|
+
handled_prev_fields = set()
|
|
190
|
+
|
|
191
|
+
for curr_name, curr_field in curr_fields.items():
|
|
192
|
+
prev_name = curr_field.get('previous_name')
|
|
193
|
+
if prev_name and prev_name in prev_fields:
|
|
194
|
+
entity_diff['changes'][curr_name] = [prev_fields[prev_name], curr_field]
|
|
195
|
+
handled_prev_fields.add(prev_name)
|
|
196
|
+
continue
|
|
197
|
+
if curr_name not in prev_fields:
|
|
198
|
+
entity_diff['changes'][curr_name] = [None, curr_field]
|
|
199
|
+
|
|
200
|
+
for prev_name, prev_field in prev_fields.items():
|
|
201
|
+
if prev_name in handled_prev_fields:
|
|
202
|
+
continue
|
|
203
|
+
if prev_name not in curr_fields:
|
|
204
|
+
entity_diff['changes'][prev_name] = [prev_field, None]
|
|
205
|
+
elif prev_fields[prev_name] != curr_fields[prev_name]:
|
|
206
|
+
entity_diff['changes'][prev_name] = [prev_field, curr_fields[prev_name]]
|
|
207
|
+
|
|
208
|
+
if entity_diff['changes']:
|
|
209
|
+
diff[entity_name] = entity_diff
|
|
210
|
+
|
|
211
|
+
return diff
|
|
212
|
+
|
|
213
|
+
def _process_schema(self, schema_class):
|
|
214
|
+
entity = schema_class.entity_name
|
|
215
|
+
namespace = schema_class.namespace_name
|
|
216
|
+
fields = schema_class._get_fields()
|
|
217
|
+
|
|
218
|
+
logger.info(f"Initializing entity {entity} in namespace {namespace}.")
|
|
219
|
+
|
|
220
|
+
self._cursor.execute(f'CREATE RELATIONAL NAMESPACE IF NOT EXISTS "{namespace}"')
|
|
221
|
+
logger.debug(f"Created namespace {namespace} if absent.")
|
|
222
|
+
|
|
223
|
+
column_defs = []
|
|
224
|
+
foreign_keys = []
|
|
225
|
+
unique_columns = []
|
|
226
|
+
primary_key_columns = []
|
|
227
|
+
|
|
228
|
+
for field in fields:
|
|
229
|
+
col_def = f'"{field._db_field_name}" {field._polytype._type_string}'
|
|
230
|
+
if not getattr(field, 'nullable', False):
|
|
231
|
+
col_def += " NOT NULL"
|
|
232
|
+
default_value = getattr(field, 'default', None)
|
|
233
|
+
if default_value is not None:
|
|
234
|
+
col_def += f" DEFAULT {field._polytype._to_sql_expression(default_value)}"
|
|
235
|
+
if getattr(field, 'unique', False):
|
|
236
|
+
unique_columns.append(field._db_field_name)
|
|
237
|
+
if isinstance(field, PrimaryKeyField):
|
|
238
|
+
primary_key_columns.append(field._db_field_name)
|
|
239
|
+
column_defs.append(col_def)
|
|
240
|
+
|
|
241
|
+
if isinstance(field, ForeignKeyField):
|
|
242
|
+
fk = (
|
|
243
|
+
f'FOREIGN KEY ("{field._db_field_name}") '
|
|
244
|
+
f'REFERENCES "{field.referenced_entity_name}"("{field.referenced_db_field_name}")'
|
|
245
|
+
)
|
|
246
|
+
foreign_keys.append(fk)
|
|
247
|
+
|
|
248
|
+
constraints = foreign_keys[:]
|
|
249
|
+
if primary_key_columns:
|
|
250
|
+
constraints.append(f"PRIMARY KEY ({', '.join(primary_key_columns)})")
|
|
251
|
+
constraints += [f'UNIQUE ("{col}")' for col in unique_columns]
|
|
252
|
+
|
|
253
|
+
create_stmt = f'CREATE TABLE IF NOT EXISTS "{namespace}"."{entity}" ({", ".join(column_defs + constraints)});'
|
|
254
|
+
self._cursor.executeany('sql', create_stmt, namespace=namespace)
|
|
255
|
+
self._conn.commit()
|
|
256
|
+
|
|
257
|
+
logger.debug(f"Created entity {entity} if absent.")
|
|
258
|
+
|
|
259
|
+
def _stop_container_by_name(self, container_name):
|
|
260
|
+
try:
|
|
261
|
+
client = docker.from_env()
|
|
262
|
+
client.ping()
|
|
263
|
+
container = client.containers.get(container_name)
|
|
264
|
+
if container.status == 'running':
|
|
265
|
+
container.stop()
|
|
266
|
+
logger.info(f"Container '{container_name}' stopped.")
|
|
267
|
+
except NotFound:
|
|
268
|
+
logger.warning(f"No container named '{container_name}' found.")
|
|
269
|
+
except DockerException as e:
|
|
270
|
+
logger.error(f"Failed to stop the container '{container_name}': {e}")
|
|
271
|
+
raise RuntimeError(f"Failed to stop the container '{container_name}'") from e
|
|
272
|
+
|
|
273
|
+
def _remove_container_by_name(self, container_name):
|
|
274
|
+
try:
|
|
275
|
+
client = docker.from_env()
|
|
276
|
+
client.ping()
|
|
277
|
+
container = client.containers.get(container_name)
|
|
278
|
+
container.remove()
|
|
279
|
+
logger.info(f"Container '{container_name}' removed.")
|
|
280
|
+
except NotFound:
|
|
281
|
+
logger.warning(f"No container named '{container_name}' found.")
|
|
282
|
+
except DockerException as e:
|
|
283
|
+
logger.error(f"Failed to remove the container '{container_name}': {e}")
|
|
284
|
+
raise RuntimeError(f"Failed to remove the container '{container_name}'") from e
|
|
285
|
+
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# keys
|
|
2
|
+
INTERNAL_NAMESPACE = 'INTERNAL_NAMESPACE'
|
|
3
|
+
CHANGE_LOG_TABLE = 'CHANGE_LOG_TABLE'
|
|
4
|
+
SYSTEM_USER_NAME = 'SYSTEM_USER_NAME'
|
|
5
|
+
SNAPSHOT_TABLE = 'SNAPSHOT_TABLE'
|
|
6
|
+
|
|
7
|
+
DEFAULT_NAMESPACE = 'DEFAULT_NAMESPACE'
|
|
8
|
+
POLYPHENY_CONTAINER_NAME = 'POLYPHENY_CONTAINER_NAME'
|
|
9
|
+
POLYPHENY_IMAGE_NAME = 'POLYPHENY_IMAGE_NAME'
|
|
10
|
+
POLYPHENY_PORTS = 'POLYPHENY_PORTS'
|
|
11
|
+
DEFAULT_TRANSPORT = 'DEFAULT_TRANSPORT'
|
|
12
|
+
DEFAULT_USER = 'DEFAULT_USER'
|
|
13
|
+
DEFAULT_PASS = 'DEFAULT_PASS'
|
|
14
|
+
|
|
15
|
+
CHANGE_LOG_IDENTIFIER = 'CHANGE_LOG_IDENTIFIER'
|
|
16
|
+
|
|
17
|
+
# constants
|
|
18
|
+
_internals = {
|
|
19
|
+
INTERNAL_NAMESPACE: 'internal',
|
|
20
|
+
CHANGE_LOG_TABLE: 'change_log',
|
|
21
|
+
SYSTEM_USER_NAME: 'SYSTEM',
|
|
22
|
+
SNAPSHOT_TABLE: 'snapshot',
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
# user configurables
|
|
26
|
+
_config = {
|
|
27
|
+
DEFAULT_NAMESPACE: 'public',
|
|
28
|
+
POLYPHENY_CONTAINER_NAME: 'polypheny',
|
|
29
|
+
POLYPHENY_IMAGE_NAME: 'vogti/polypheny',
|
|
30
|
+
POLYPHENY_PORTS: {
|
|
31
|
+
"20590/tcp": 20590,
|
|
32
|
+
"7659/tcp": 7659,
|
|
33
|
+
"80/tcp": 80,
|
|
34
|
+
"8081/tcp": 8081,
|
|
35
|
+
"8082/tcp": 8082,
|
|
36
|
+
},
|
|
37
|
+
DEFAULT_TRANSPORT: 'plain',
|
|
38
|
+
DEFAULT_USER: 'pa',
|
|
39
|
+
DEFAULT_PASS: '',
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
# derived options
|
|
43
|
+
_derived = {}
|
|
44
|
+
|
|
45
|
+
def _refresh_derived():
|
|
46
|
+
_derived[CHANGE_LOG_IDENTIFIER] = f"{_internals[INTERNAL_NAMESPACE]}.{_internals[CHANGE_LOG_TABLE]}"
|
|
47
|
+
|
|
48
|
+
_refresh_derived()
|
|
49
|
+
|
|
50
|
+
USER_CONFIGURABLE_KEYS = set(_config.keys())
|
|
51
|
+
|
|
52
|
+
_lock_count = 0
|
|
53
|
+
|
|
54
|
+
def lock():
|
|
55
|
+
global _lock_count
|
|
56
|
+
_lock_count += 1
|
|
57
|
+
|
|
58
|
+
def unlock():
|
|
59
|
+
global _lock_count
|
|
60
|
+
if _lock_count == 0:
|
|
61
|
+
raise RuntimeError("Unlock called more times than lock")
|
|
62
|
+
_lock_count -= 1
|
|
63
|
+
|
|
64
|
+
def get(key: str):
|
|
65
|
+
if key in _config:
|
|
66
|
+
return _config[key]
|
|
67
|
+
elif key in _internals:
|
|
68
|
+
return _internals[key]
|
|
69
|
+
elif key in _derived:
|
|
70
|
+
return _derived[key]
|
|
71
|
+
else:
|
|
72
|
+
raise KeyError(f"Unknown config key: {key}")
|
|
73
|
+
|
|
74
|
+
def set(key: str, value):
|
|
75
|
+
if key not in USER_CONFIGURABLE_KEYS:
|
|
76
|
+
raise KeyError(f"Cannot set config key: {key}")
|
|
77
|
+
if _lock_count > 0:
|
|
78
|
+
raise RuntimeError("Configuration is locked. No updates are allowed.")
|
|
79
|
+
_config[key] = value
|
|
80
|
+
_refresh_derived()
|
|
81
|
+
|
|
82
|
+
def set_config(overrides: dict):
|
|
83
|
+
if _lock_count > 0:
|
|
84
|
+
raise RuntimeError("Configuration is locked. No updates are allowed.")
|
|
85
|
+
for key, value in overrides.items():
|
|
86
|
+
set(key, value)
|
|
87
|
+
|
|
88
|
+
def all_config():
|
|
89
|
+
return {**_internals, **_config, **_derived}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from typing import Any, Tuple
|
|
2
|
+
from polynom.query import Query
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Encapsulates content to eagerly load a relationship
|
|
6
|
+
"""
|
|
7
|
+
class JoinedLoad:
|
|
8
|
+
def __init__(self, relationship_attr: Any):
|
|
9
|
+
self.attr = relationship_attr # e.g. Bike.user
|
|
10
|
+
|
|
11
|
+
def apply(self, query: Query):
|
|
12
|
+
related_model = self.attr.get_target_model()
|
|
13
|
+
query.join(related_model)
|
|
14
|
+
alias = f"{related_model.schema.entity_name}_{len(query._joins)}"
|
|
15
|
+
query._eager_loads.append((self.attr._key, related_model, alias))
|