-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdatabase.py
More file actions
344 lines (298 loc) · 11 KB
/
Copy pathdatabase.py
File metadata and controls
344 lines (298 loc) · 11 KB
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
335
336
337
338
339
340
341
342
343
344
from __future__ import annotations
import json
from typing import Any, List, Optional, Union, get_args
import pandas as pd
from morph_lib.error import MorphApiError
from morph_lib.utils.db_connector import DBConnector
from morph_lib.utils.sql import SQLUtils
from pydantic import BaseModel, Field
from morph.api.cloud.client import MorphApiClient, MorphApiKeyClientImpl
from morph.config.project import MorphProject, load_project
from morph.task.utils.connection import (
CONNECTION_TYPE,
Connection,
ConnectionYaml,
DatabaseConnection,
DuckDBConnection,
)
from morph.task.utils.connections.connector import Connector
from morph.task.utils.morph import find_project_root_dir
# ===============================================
#
# Class
#
# ===============================================
class FieldSchema(BaseModel):
name: str
type: str
nullable: Optional[bool] = Field(default=True)
primary: Optional[bool] = Field(default=False)
comment: Optional[str] = Field(default=None)
length: Optional[int] = Field(default=None)
unsigned: Optional[bool] = Field(default=None)
auto_increment: Optional[bool] = Field(default=None)
default: Optional[Any] = Field(default=None)
enums: Optional[List[str]] = Field(default=None)
class TableSchema(BaseModel):
dialect: str
name: str
fields: List[FieldSchema]
schema_name: Optional[str] = Field(default=None)
def to_text(self):
text = f"""# Database
{self.dialect}
"""
if self.schema_name:
text += f"""# Schema
{self.schema_name}
"""
text += f"""# Table
{self.name}
"""
text += "# Fields\n"
for i, field in enumerate(self.fields):
field_text = f"""col{i+1}. {field.name} type={field.type} nullable={field.nullable} """
if field.primary is not None:
field_text += f"primary={field.primary} "
if field.comment:
field_text += f"comment={field.comment}"
if field.length:
field_text += f"length={field.length}"
if field.unsigned is not None:
field_text += f"unsigned={field.unsigned}"
if field.auto_increment is not None:
field_text += f"auto_increment={field.auto_increment}"
if field.default:
field_text += f"default_value={field.default}"
if field.enums:
field_text += f"enums={field.enums}"
text += field_text + "\n"
return text
# ===============================================
#
# Implementation
#
# ===============================================
def __find_connection(connection: str | Connection | None) -> Connection:
if isinstance(connection, get_args(Connection)):
return connection # type: ignore
database_connection: Optional[Union[Connection, DatabaseConnection]] = None
# return connection itself if it's DuckDBConnection
if connection is not None and isinstance(connection, DuckDBConnection):
return connection
# find connection in connections.yml
elif connection is not None and isinstance(connection, str):
connection_yaml = ConnectionYaml.load_yaml()
database_connection = ConnectionYaml.find_connection(
connection_yaml, connection
)
if database_connection is None:
database_connection = ConnectionYaml.find_cloud_connection(connection)
else:
# in case of no connection provided, find default connection
project: Optional[MorphProject] = load_project(find_project_root_dir())
if project is None:
raise MorphApiError("Could not find project.")
elif project.default_connection is None:
raise MorphApiError(
"Default connection is not set in morph_project.yml. Please set default_connection."
)
default_connection = project.default_connection
connection_yaml = ConnectionYaml.load_yaml()
database_connection = ConnectionYaml.find_connection(
connection_yaml, default_connection
)
if database_connection is None:
database_connection = ConnectionYaml.find_cloud_connection(
default_connection
)
return database_connection
def __execute_sql_impl(
sql: str,
connection: str | Connection | None = None,
) -> pd.DataFrame:
database_connection: Connection = __find_connection(connection)
connector = Connector(
connection if isinstance(connection, str) else "", database_connection
)
return connector.execute_sql(sql)
def __process_records(
action: str,
data: pd.DataFrame,
table_name: str,
primary_keys: List[str] = [],
connection: Optional[str] = None,
) -> None:
# Validate action
if action not in {"insert", "update", "delete", "insert_or_update"}:
raise MorphApiError(
"Invalid action provided. Must be 'create', 'insert', or 'update'."
)
database_connection = __find_connection(connection)
if (
database_connection.type != CONNECTION_TYPE.postgres
and database_connection.type != CONNECTION_TYPE.snowflake
and database_connection.type != CONNECTION_TYPE.redshift
and database_connection.type != CONNECTION_TYPE.mysql
and database_connection.type != CONNECTION_TYPE.bigquery
):
raise MorphApiError(
"Only PostgreSQL, Snowflake, Redshift, BigQuery, and MySQL are supported for now."
)
if action == "update" or action == "delete" or action == "insert_or_update":
if len(primary_keys) == 0:
raise MorphApiError(
"Primary keys are required for update, delete, and insert_or_update actions."
)
missing_keys = [key for key in primary_keys if key not in data.columns]
if len(missing_keys) > 0:
raise MorphApiError(
f"Primary keys {missing_keys} are not present in the DataFrame columns."
)
sql_utils = SQLUtils(data, table_name, database_connection)
if action == "insert":
__execute_sql_impl(
sql_utils.generate_insert_sql(), connection=database_connection
)
elif action == "update":
__execute_sql_impl(
sql_utils.generate_update_sql(primary_keys), connection=database_connection
)
elif action == "insert_or_update":
if (
database_connection.type == CONNECTION_TYPE.redshift
or database_connection.type == CONNECTION_TYPE.bigquery
):
raise MorphApiError("Redshift and BigQuery do not support UPSERT.")
__execute_sql_impl(
sql_utils.generate_insert_or_update_sql(primary_keys),
connection=database_connection,
)
elif action == "delete":
__execute_sql_impl(
sql_utils.generate_delete_sql(primary_keys), connection=database_connection
)
# ===============================================
#
# Functions
#
# ===============================================
def execute_sql(sql: str, connection: Optional[str] = None) -> pd.DataFrame:
"""
Execute SQL query
"""
return __execute_sql_impl(sql, connection)
def insert_records(
data: pd.DataFrame,
table_name: str,
connection: Optional[str] = None,
) -> None:
"""
Insert records into the table
"""
__process_records("insert", data, table_name, connection=connection)
def update_records(
data: pd.DataFrame,
primary_keys: List[str],
table_name: str,
connection: Optional[str] = None,
) -> None:
"""
Update records in the table
"""
__process_records(
"update", data, table_name, primary_keys=primary_keys, connection=connection
)
def insert_or_update_records(
data: pd.DataFrame,
primary_keys: List[str],
table_name: str,
connection: Optional[str] = None,
) -> None:
"""
Insert or Update records in the table
"""
__process_records(
"insert_or_update",
data,
table_name,
primary_keys=primary_keys,
connection=connection,
)
def delete_records(
data: pd.DataFrame,
primary_keys: List[str],
table_name: str,
connection: Optional[str] = None,
) -> None:
"""
Delete records in the table
"""
__process_records(
"delete", data, table_name, primary_keys=primary_keys, connection=connection
)
def get_db_connector(connection: str) -> DBConnector:
"""
Obtain a DBConnector object for PostgreSQL, Redshift, or MySQL
"""
connection_yaml = ConnectionYaml.load_yaml()
database_connection = ConnectionYaml.find_connection(connection_yaml, connection)
if database_connection is None:
raise MorphApiError(f"Could not find {connection} in connections.yml.")
if (
database_connection.type != CONNECTION_TYPE.postgres
and database_connection.type != CONNECTION_TYPE.redshift
and database_connection.type != CONNECTION_TYPE.mysql
):
raise ValueError(f"Connection '{connection}' is not supported.")
return DBConnector(database_connection)
def get_tables(
table_names: List[str],
schema_name: Optional[str],
connection: Optional[str],
) -> List[TableSchema]:
"""
Get the table structures
"""
connection_ = __find_connection(connection)
tables: List[TableSchema] = []
client = MorphApiClient(MorphApiKeyClientImpl)
for table in table_names:
response = client.req.list_fields(table, schema_name, connection)
try:
response.is_error(True)
except Exception as e:
error_detail = {
"type": type(e).__name__,
"message": str(e),
}
error_json = json.dumps(error_detail, ensure_ascii=False)
raise MorphApiError(error_json)
fields: List[FieldSchema] = []
for field in response.json()["fields"]:
field_type = field["nativeType"] if "nativeType" in field else field["type"]
fields.append(
FieldSchema(
name=field["name"],
type=field_type,
nullable=field["nullable"] if "nullable" in field else True,
primary=field["primary"] if "primary" in field else None,
comment=field["comment"] if "comment" in field else None,
length=field["length"] if "length" in field else None,
unsigned=field["unsigned"] if "unsigned" in field else None,
auto_increment=(
field["autoIncrement"] if "autoIncrement" in field else None
),
default=(field["default"] if "default" in field else None),
enums=field["members"] if "members" in field else None,
)
)
tables.append(
TableSchema(
dialect=connection_.type,
name=table,
fields=fields,
schema_name=schema_name,
)
)
return tables