/
utils.py
334 lines (258 loc) · 9.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""Utility methods for Django Heroku Connect."""
import os
from enum import Enum
from functools import lru_cache
import requests
from django.db import DEFAULT_DB_ALIAS, connections
from django.utils import timezone
from psycopg2.extensions import AsIs
from psycopg2.extras import HstoreAdapter
from heroku_connect.conf import settings
HEROKU_REQUEST_TIMEOUT = 30
class ConnectionStates:
IDLE = "IDLE"
POLLING_DB_CHANGES = "POLLING_DB_CHANGES"
IMPORT_CONFIGURATION = "IMPORT_CONFIGURATION"
BUSY = "BUSY"
NEED_AUTHENTICATION = "NEED_AUTHENTICATION"
INACTIVE_ORG = "INACTIVE_ORG"
PAUSED = "PAUSED"
OK_STATES = (
IDLE,
POLLING_DB_CHANGES,
IMPORT_CONFIGURATION,
BUSY,
)
class WriteAlgorithm(Enum):
MERGE_WRITES = 1
ORDERED_WRITES = 2
def _write_algorithm_from_connection_info(info):
if info.get("features", {}).get("poll_db_no_merge", False):
return WriteAlgorithm.ORDERED_WRITES
else:
return WriteAlgorithm.MERGE_WRITES
def get_mapping(version=1, exported_at=None, app_name=None):
"""
Return Heroku Connect mapping for the entire project.
Args:
version (int): Version of the Heroku Connect mapping, default: ``1``.
exported_at (datetime.datetime): Time the export was created, default is ``now()``.
app_name (str): Name of Heroku application associated with Heroku Connect the add-on.
Returns:
dict: Heroku Connect mapping.
Note:
The version does not need to be incremented. Exports from the Heroku Connect
website will always have the version number ``1``.
"""
if exported_at is None:
exported_at = timezone.now()
app_name = app_name or settings.HEROKU_CONNECT_APP_NAME
return {
"version": version,
"connection": {
"organization_id": settings.HEROKU_CONNECT_ORGANIZATION_ID,
"app_name": app_name,
"exported_at": exported_at.isoformat(),
},
"mappings": [
model.get_heroku_connect_mapping() for model in get_heroku_connect_models()
],
}
def get_heroku_connect_models():
"""
Return all registered Heroku Connect Models.
Returns:
(Iterator):
All registered models that are subclasses of `.HerokuConnectModel`.
Abstract models are excluded, since they are not registered.
"""
from django.apps import apps
apps.check_models_ready()
from heroku_connect.db.models import HerokuConnectModel
return (
model
for models in apps.all_models.values()
for model in models.values()
if issubclass(model, HerokuConnectModel) and not model._meta.managed
)
@lru_cache(maxsize=128)
def get_connected_model_for_table_name(table_name):
"""Return a connected model's table name (which read and written to by Heroku Connect)."""
for connected_model in get_heroku_connect_models():
if connected_model.get_heroku_connect_table_name() == table_name:
return connected_model
raise LookupError("No connected model found for table %r" % (table_name,))
_SCHEMA_EXISTS_QUERY = """
SELECT exists(
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = %s
);
"""
def create_heroku_connect_schema(using=DEFAULT_DB_ALIAS):
"""
Create Heroku Connect schema.
Note:
This function is only meant to be used for local development.
In a production environment the schema will be created by
Heroku Connect.
Args:
using (str): Alias for database connection.
Returns:
bool: ``True`` if the schema was created, ``False`` if the
schema already exists.
"""
connection = connections[using]
with connection.cursor() as cursor:
cursor.execute(_SCHEMA_EXISTS_QUERY, [settings.HEROKU_CONNECT_SCHEMA])
schema_exists = cursor.fetchone()[0]
if schema_exists:
return False
cursor.execute("CREATE SCHEMA %s;", [AsIs(settings.HEROKU_CONNECT_SCHEMA)])
with connection.schema_editor() as editor:
for model in get_heroku_connect_models():
editor.create_model(model)
# Needs PostgreSQL and database superuser privileges (which is the case on Heroku):
editor.execute('CREATE EXTENSION IF NOT EXISTS "hstore";')
from heroku_connect.models import TriggerLog, TriggerLogArchive
for cls in [TriggerLog, TriggerLogArchive]:
editor.create_model(cls)
return True
def _get_authorization_headers():
return {"Authorization": "Bearer %s" % settings.HEROKU_AUTH_TOKEN}
def get_connections(app):
"""
Return all Heroku Connect connections setup with the given application.
For more details check the link -
https://devcenter.heroku.com/articles/heroku-connect-api#step-4-retrieve-the-new-connection-s-id
Sample response from the API call is below::
{
"count": 1,
"results":[{
"id": "<connection_id>",
"name": "<app_name>",
"resource_name": "<resource_name>",
…
}],
…
}
Args:
app (str): Heroku application name.
Returns:
List[dict]: List of all Heroku Connect connections associated with the Heroku application.
Raises:
requests.HTTPError: If an error occurred when accessing the connections API.
ValueError: If response is not a valid JSON.
"""
payload = {"app": app}
url = os.path.join(settings.HEROKU_CONNECT_API_ENDPOINT, "connections")
response = requests.get(
url,
timeout=HEROKU_REQUEST_TIMEOUT,
params=payload,
headers=_get_authorization_headers(),
)
response.raise_for_status()
return response.json()["results"]
def get_all_connections_write_modes(app):
return {
info["id"]: _write_algorithm_from_connection_info(info)
for info in get_connections(app)
}
def get_connection(connection_id, deep=False):
"""
Get Heroku Connection connection information.
For more details check the link -
https://devcenter.heroku.com/articles/heroku-connect-api#step-8-monitor-the-connection-and-mapping-status
Sample response from API call is below::
{
"id": "<connection_id>",
"name": "<app_name>",
"resource_name": "<resource_name>",
"schema_name": "salesforce",
"db_key": "DATABASE_URL",
"state": "IDLE",
"mappings":[
{
"id": "<mapping_id>",
"object_name": "Account",
"state": "SCHEMA_CHANGED",
…
},
{
"id": "<mapping_id>",
"object_name": "Contact",
"state": "SCHEMA_CHANGED",
…
},
…
]
…
}
Args:
connection_id (str): ID for Heroku Connect's connection.
deep (bool): Return information about the connection’s mappings,
in addition to the connection itself. Defaults to ``False``.
Returns:
dict: Heroku Connection connection information.
Raises:
requests.HTTPError: If an error occurred when accessing the connection detail API.
ValueError: If response is not a valid JSON.
"""
url = os.path.join(
settings.HEROKU_CONNECT_API_ENDPOINT, "connections", connection_id
)
payload = {"deep": deep}
response = requests.get(
url,
timeout=HEROKU_REQUEST_TIMEOUT,
params=payload,
headers=_get_authorization_headers(),
)
response.raise_for_status()
return response.json()
def get_connection_write_mode(connection_id):
return _write_algorithm_from_connection_info(get_connection(connection_id))
@lru_cache()
def get_unique_connection_write_mode(app_name=None):
app_name = app_name or settings.HEROKU_CONNECT_APP_NAME
(mode,) = set(get_all_connections_write_modes(app_name).values())
return mode
def import_mapping(connection_id, mapping):
"""
Import Heroku Connection mapping for given connection.
Args:
connection_id (str): Heroku Connection connection ID.
mapping (dict): Heroku Connect mapping.
Raises:
requests.HTTPError: If an error occurs uploading the mapping.
ValueError: If the mapping is not JSON serializable.
"""
url = os.path.join(
settings.HEROKU_CONNECT_API_ENDPOINT,
"connections",
connection_id,
"actions",
"import",
)
response = requests.post(
url=url,
json=mapping,
headers=_get_authorization_headers(),
timeout=HEROKU_REQUEST_TIMEOUT,
)
response.raise_for_status()
def link_connection_to_account(app):
"""
Link the connection to your Heroku user account.
https://devcenter.heroku.com/articles/heroku-connect-api#step-3-link-the-new-add-on-to-your-heroku-user-account
"""
url = os.path.join(
settings.HEROKU_CONNECT_API_ENDPOINT, "users", "me", "apps", app, "auth"
)
response = requests.post(
url=url, timeout=HEROKU_REQUEST_TIMEOUT, headers=_get_authorization_headers()
)
response.raise_for_status()
def hstore_text_to_dict(text):
return HstoreAdapter.parse(text, None)