dataforge-core 0.1.1__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.
@@ -0,0 +1,204 @@
1
+ Copyright (c) 2017 HashiCorp, Inc.
2
+
3
+
4
+ Apache License
5
+ Version 2.0, January 2004
6
+ http://www.apache.org/licenses/
7
+
8
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
9
+
10
+ 1. Definitions.
11
+
12
+ "License" shall mean the terms and conditions for use, reproduction,
13
+ and distribution as defined by Sections 1 through 9 of this document.
14
+
15
+ "Licensor" shall mean the copyright owner or entity authorized by
16
+ the copyright owner that is granting the License.
17
+
18
+ "Legal Entity" shall mean the union of the acting entity and all
19
+ other entities that control, are controlled by, or are under common
20
+ control with that entity. For the purposes of this definition,
21
+ "control" means (i) the power, direct or indirect, to cause the
22
+ direction or management of such entity, whether by contract or
23
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
24
+ outstanding shares, or (iii) beneficial ownership of such entity.
25
+
26
+ "You" (or "Your") shall mean an individual or Legal Entity
27
+ exercising permissions granted by this License.
28
+
29
+ "Source" form shall mean the preferred form for making modifications,
30
+ including but not limited to software source code, documentation
31
+ source, and configuration files.
32
+
33
+ "Object" form shall mean any form resulting from mechanical
34
+ transformation or translation of a Source form, including but
35
+ not limited to compiled object code, generated documentation,
36
+ and conversions to other media types.
37
+
38
+ "Work" shall mean the work of authorship, whether in Source or
39
+ Object form, made available under the License, as indicated by a
40
+ copyright notice that is included in or attached to the work
41
+ (an example is provided in the Appendix below).
42
+
43
+ "Derivative Works" shall mean any work, whether in Source or Object
44
+ form, that is based on (or derived from) the Work and for which the
45
+ editorial revisions, annotations, elaborations, or other modifications
46
+ represent, as a whole, an original work of authorship. For the purposes
47
+ of this License, Derivative Works shall not include works that remain
48
+ separable from, or merely link (or bind by name) to the interfaces of,
49
+ the Work and Derivative Works thereof.
50
+
51
+ "Contribution" shall mean any work of authorship, including
52
+ the original version of the Work and any modifications or additions
53
+ to that Work or Derivative Works thereof, that is intentionally
54
+ submitted to Licensor for inclusion in the Work by the copyright owner
55
+ or by an individual or Legal Entity authorized to submit on behalf of
56
+ the copyright owner. For the purposes of this definition, "submitted"
57
+ means any form of electronic, verbal, or written communication sent
58
+ to the Licensor or its representatives, including but not limited to
59
+ communication on electronic mailing lists, source code control systems,
60
+ and issue tracking systems that are managed by, or on behalf of, the
61
+ Licensor for the purpose of discussing and improving the Work, but
62
+ excluding communication that is conspicuously marked or otherwise
63
+ designated in writing by the copyright owner as "Not a Contribution."
64
+
65
+ "Contributor" shall mean Licensor and any individual or Legal Entity
66
+ on behalf of whom a Contribution has been received by Licensor and
67
+ subsequently incorporated within the Work.
68
+
69
+ 2. Grant of Copyright License. Subject to the terms and conditions of
70
+ this License, each Contributor hereby grants to You a perpetual,
71
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
72
+ copyright license to reproduce, prepare Derivative Works of,
73
+ publicly display, publicly perform, sublicense, and distribute the
74
+ Work and such Derivative Works in Source or Object form.
75
+
76
+ 3. Grant of Patent License. Subject to the terms and conditions of
77
+ this License, each Contributor hereby grants to You a perpetual,
78
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
79
+ (except as stated in this section) patent license to make, have made,
80
+ use, offer to sell, sell, import, and otherwise transfer the Work,
81
+ where such license applies only to those patent claims licensable
82
+ by such Contributor that are necessarily infringed by their
83
+ Contribution(s) alone or by combination of their Contribution(s)
84
+ with the Work to which such Contribution(s) was submitted. If You
85
+ institute patent litigation against any entity (including a
86
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
87
+ or a Contribution incorporated within the Work constitutes direct
88
+ or contributory patent infringement, then any patent licenses
89
+ granted to You under this License for that Work shall terminate
90
+ as of the date such litigation is filed.
91
+
92
+ 4. Redistribution. You may reproduce and distribute copies of the
93
+ Work or Derivative Works thereof in any medium, with or without
94
+ modifications, and in Source or Object form, provided that You
95
+ meet the following conditions:
96
+
97
+ (a) You must give any other recipients of the Work or
98
+ Derivative Works a copy of this License; and
99
+
100
+ (b) You must cause any modified files to carry prominent notices
101
+ stating that You changed the files; and
102
+
103
+ (c) You must retain, in the Source form of any Derivative Works
104
+ that You distribute, all copyright, patent, trademark, and
105
+ attribution notices from the Source form of the Work,
106
+ excluding those notices that do not pertain to any part of
107
+ the Derivative Works; and
108
+
109
+ (d) If the Work includes a "NOTICE" text file as part of its
110
+ distribution, then any Derivative Works that You distribute must
111
+ include a readable copy of the attribution notices contained
112
+ within such NOTICE file, excluding those notices that do not
113
+ pertain to any part of the Derivative Works, in at least one
114
+ of the following places: within a NOTICE text file distributed
115
+ as part of the Derivative Works; within the Source form or
116
+ documentation, if provided along with the Derivative Works; or,
117
+ within a display generated by the Derivative Works, if and
118
+ wherever such third-party notices normally appear. The contents
119
+ of the NOTICE file are for informational purposes only and
120
+ do not modify the License. You may add Your own attribution
121
+ notices within Derivative Works that You distribute, alongside
122
+ or as an addendum to the NOTICE text from the Work, provided
123
+ that such additional attribution notices cannot be construed
124
+ as modifying the License.
125
+
126
+ You may add Your own copyright statement to Your modifications and
127
+ may provide additional or different license terms and conditions
128
+ for use, reproduction, or distribution of Your modifications, or
129
+ for any such Derivative Works as a whole, provided Your use,
130
+ reproduction, and distribution of the Work otherwise complies with
131
+ the conditions stated in this License.
132
+
133
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
134
+ any Contribution intentionally submitted for inclusion in the Work
135
+ by You to the Licensor shall be under the terms and conditions of
136
+ this License, without any additional terms or conditions.
137
+ Notwithstanding the above, nothing herein shall supersede or modify
138
+ the terms of any separate license agreement you may have executed
139
+ with Licensor regarding such Contributions.
140
+
141
+ 6. Trademarks. This License does not grant permission to use the trade
142
+ names, trademarks, service marks, or product names of the Licensor,
143
+ except as required for reasonable and customary use in describing the
144
+ origin of the Work and reproducing the content of the NOTICE file.
145
+
146
+ 7. Disclaimer of Warranty. Unless required by applicable law or
147
+ agreed to in writing, Licensor provides the Work (and each
148
+ Contributor provides its Contributions) on an "AS IS" BASIS,
149
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
150
+ implied, including, without limitation, any warranties or conditions
151
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
152
+ PARTICULAR PURPOSE. You are solely responsible for determining the
153
+ appropriateness of using or redistributing the Work and assume any
154
+ risks associated with Your exercise of permissions under this License.
155
+
156
+ 8. Limitation of Liability. In no event and under no legal theory,
157
+ whether in tort (including negligence), contract, or otherwise,
158
+ unless required by applicable law (such as deliberate and grossly
159
+ negligent acts) or agreed to in writing, shall any Contributor be
160
+ liable to You for damages, including any direct, indirect, special,
161
+ incidental, or consequential damages of any character arising as a
162
+ result of this License or out of the use or inability to use the
163
+ Work (including but not limited to damages for loss of goodwill,
164
+ work stoppage, computer failure or malfunction, or any and all
165
+ other commercial damages or losses), even if such Contributor
166
+ has been advised of the possibility of such damages.
167
+
168
+ 9. Accepting Warranty or Additional Liability. While redistributing
169
+ the Work or Derivative Works thereof, You may choose to offer,
170
+ and charge a fee for, acceptance of support, warranty, indemnity,
171
+ or other liability obligations and/or rights consistent with this
172
+ License. However, in accepting such obligations, You may act only
173
+ on Your own behalf and on Your sole responsibility, not on behalf
174
+ of any other Contributor, and only if You agree to indemnify,
175
+ defend, and hold each Contributor harmless for any liability
176
+ incurred by, or claims asserted against, such Contributor by reason
177
+ of your accepting any such warranty or additional liability.
178
+
179
+ END OF TERMS AND CONDITIONS
180
+
181
+ APPENDIX: How to apply the Apache License to your work.
182
+
183
+ To apply the Apache License to your work, attach the following
184
+ boilerplate notice, with the fields enclosed by brackets "[]"
185
+ replaced with your own identifying information. (Don't include
186
+ the brackets!) The text should be enclosed in the appropriate
187
+ comment syntax for the file format. We also recommend that a
188
+ file or class name and description of purpose be included on the
189
+ same "printed page" as the copyright notice for easier
190
+ identification within third-party archives.
191
+
192
+ Copyright [yyyy] [name of copyright owner]
193
+
194
+ Licensed under the Apache License, Version 2.0 (the "License");
195
+ you may not use this file except in compliance with the License.
196
+ You may obtain a copy of the License at
197
+
198
+ http://www.apache.org/licenses/LICENSE-2.0
199
+
200
+ Unless required by applicable law or agreed to in writing, software
201
+ distributed under the License is distributed on an "AS IS" BASIS,
202
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
203
+ See the License for the specific language governing permissions and
204
+ limitations under the License.
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,20 @@
1
+ Metadata-Version: 2.1
2
+ Name: dataforge-core
3
+ Version: 0.1.1
4
+ Summary: Command line compiler for dataforge core projects
5
+ Author-email: Vadim Orlov <vorlov@dataforgelabs.com>
6
+ Project-URL: Homepage, https://github.com/dataforgelabs/dataforge-core
7
+ Project-URL: Issues, https://github.com/dataforgelabs/dataforge-core/issues
8
+ Requires-Python: >=3.12
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Requires-Dist: pyyaml
12
+ Requires-Dist: psycopg2-binary
13
+ Requires-Dist: pyspark
14
+ Requires-Dist: importlib_resources
15
+
16
+ # Dataforge Core
17
+
18
+ ## Installation
19
+
20
+ - download and install Python version 3.12+ from https://www.python.org/downloads/
@@ -0,0 +1,5 @@
1
+ # Dataforge Core
2
+
3
+ ## Installation
4
+
5
+ - download and install Python version 3.12+ from https://www.python.org/downloads/
File without changes
@@ -0,0 +1,128 @@
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import yaml
6
+ from .mainConfig import MainConfig
7
+ from .miniSparky import MiniSparky
8
+
9
+
10
+ class ImportProject:
11
+ def __init__(self, config: MainConfig):
12
+ self.ms = None
13
+ self._config = config
14
+ self.import_id = 0
15
+
16
+ def start(self):
17
+ _import_id = self._config.pg.sql("select meta.svc_import_start()")
18
+ self.import_id = int(_import_id)
19
+ print('Started import with id ', self.import_id)
20
+
21
+ def validate(self):
22
+ print(f"Validating project path {self._config.source_path}")
23
+ meta_flag = False
24
+ source_flag = False
25
+ with os.scandir(self._config.source_path) as entries:
26
+ for file in entries:
27
+ if file.is_dir() and file.name == 'sources':
28
+ source_flag = True
29
+ elif file.name == "meta.yaml":
30
+ meta_flag = True
31
+ if not meta_flag:
32
+ print(f"Missing meta.yaml in project path {self._config.source_path}")
33
+ sys.exit(1)
34
+ if not source_flag:
35
+ print(f"Missing sources folder in project path {self._config.source_path}")
36
+ sys.exit(1)
37
+
38
+ def load(self):
39
+ self.validate()
40
+ self.start()
41
+ with os.scandir(self._config.source_path) as entries:
42
+ for file in entries:
43
+ if file.is_dir() and file.name in ('sources', 'outputs'):
44
+ self.list_files(file.path, file.name)
45
+ elif file.name.endswith(".yaml"):
46
+ self.load_file(file.path, file.name)
47
+ self._config.pg.sql("SELECT meta.svc_import_complete(%s, 'I')", [self.import_id])
48
+ self._config.pg.sql("SELECT meta.imp_parse_objects(%s)", [self.import_id])
49
+ print("Files parsed")
50
+ self._config.pg.sql("SELECT meta.svc_import_execute(%s)", [self.import_id])
51
+ print("Objects loaded")
52
+ self.test_expressions()
53
+ print("Expressions validated")
54
+ print("Import completed successfully")
55
+ self.write_log()
56
+ self.write_queries("source")
57
+ self.write_queries("output")
58
+
59
+ def list_files(self, path: str, folder_name: str):
60
+ print("Importing files..")
61
+ with os.scandir(path) as entries:
62
+ for file in entries:
63
+ if file.is_file() & file.name.endswith(".yaml"):
64
+ self.load_file(file.path, folder_name + '/' + file.name)
65
+
66
+ def load_file(self, full_path: str, path: str):
67
+ print(path)
68
+ with open(full_path, 'r') as file:
69
+ file_js = yaml.safe_load(file)
70
+ self._config.pg.sql("SELECT meta.svc_import_load_object(%s, %s, %s)",
71
+ (self.import_id, path, json.dumps(file_js)))
72
+
73
+ def test_expressions(self):
74
+ exps = self._config.pg.sql("SELECT meta.impc_test_expressions(%s)", [self.import_id])
75
+ self.ms = MiniSparky(self._config)
76
+ self.test_expressions_recursive(exps)
77
+ self.ms.stop()
78
+ del self.ms
79
+
80
+ def test_expressions_recursive(self, test_expressions, recursion_level=0):
81
+ try:
82
+ test_results = []
83
+ for exp in test_expressions:
84
+ query_result = self.ms.execute_query(exp['expression'])
85
+ del exp['expression']
86
+ exp['result'] = query_result
87
+ test_results.append(exp)
88
+ # update test results
89
+ test_results_str = json.dumps(test_results)
90
+ res = self._config.pg.sql("SELECT meta.impc_update_test_results(%s, %s)",
91
+ (self.import_id, test_results_str))
92
+ if res.get('error'):
93
+ self.fail_import('Invalid expression detected. See log file for details')
94
+ if recursion_level > 20:
95
+ self.fail_import('Maximum recursion exceeded while testing expressions. Check error logs and '
96
+ 'expression test tables for more details')
97
+ if len(res['next']) > 0:
98
+ self.test_expressions_recursive(res['next'], recursion_level + 1)
99
+ except Exception as e:
100
+ print(e)
101
+ self.fail_import(str(e))
102
+
103
+ def fail_import(self, message):
104
+ print(f"Import failed: {message}")
105
+ self._config.pg.sql("SELECT meta.svc_import_complete(%s, 'F', %s)", (self.import_id, message))
106
+ self.write_log()
107
+ sys.exit(1)
108
+
109
+ def write_log(self):
110
+ log_file = self._config.pg.sql("SELECT meta.svc_import_get_log(%s)", [self.import_id])
111
+ with open(self._config.log_path, "w") as file:
112
+ # Write the string to the file
113
+ file.write(log_file)
114
+
115
+ def write_queries(self, out_type: str):
116
+ queries = self._config.pg.sql(f"select meta.svcc_get_{out_type}_queries(%s)", [self.import_id])
117
+ if not queries:
118
+ return
119
+ if out_type == "source":
120
+ out_path = self._config.output_source_path
121
+ elif out_type == "output":
122
+ out_path = self._config.output_output_path
123
+ for query in queries:
124
+ file_name = os.path.join(out_path, query['file_name'])
125
+ with open(file_name, "w") as file:
126
+ # Write the string to the file
127
+ file.write(query['query'])
128
+ print(f"Generated {len(queries)} {out_type} queries")
@@ -0,0 +1,12 @@
1
+ from .importProject import ImportProject
2
+ from .mainConfig import MainConfig
3
+
4
+
5
+ def main():
6
+ conf = MainConfig()
7
+ imp = ImportProject(conf)
8
+ imp.load()
9
+
10
+
11
+ if __name__ == '__main__':
12
+ main()
@@ -0,0 +1,72 @@
1
+ import argparse
2
+ import os
3
+ import shutil
4
+ import sys
5
+ import traceback
6
+ from types import ModuleType
7
+
8
+ from importlib_resources.abc import Traversable
9
+ from .pg import Pg
10
+ import importlib_resources
11
+
12
+
13
+ class MainConfig:
14
+ def __init__(self):
15
+ # check if Java is installed
16
+ if os.environ.get('JAVA_HOME') is None:
17
+ print("Java is not installed or JAVA_HOME environment variable is not set")
18
+ sys.exit(1)
19
+ # parse command line args
20
+ _parser = argparse.ArgumentParser(
21
+ prog='dataforge core',
22
+ description='Dataforge Core compiles project and generates source and output SQL queries',
23
+ epilog='Try our cloud product')
24
+ _parser.add_argument('source', type=str, help='Project folder', metavar='<Project Path>', nargs='?')
25
+ _parser.add_argument('--init', '-i', action='store_true', help='Initialize project folder')
26
+ _parser.add_argument('--seed', action='store_true', help='Deploy and seed postgres database')
27
+ _parser.add_argument('--connect', '-c', type=str, help='Connect to postgres database',
28
+ metavar='<Postgres connection string>')
29
+
30
+ args = _parser.parse_args()
31
+ if args.connect:
32
+ self.pg = Pg(args.connect, initialize=True)
33
+ self.pg.seed()
34
+ sys.exit(0)
35
+ else:
36
+ self.pg = Pg()
37
+ if args.seed:
38
+ self.pg.seed()
39
+ self.source_path = os.getcwd() if args.source is None else args.source
40
+ if args.init:
41
+ try:
42
+ if self.pg.confirm_action(f"All files and subfolders in {self.source_path} will be deleted. Continue (y/n)? "):
43
+ shutil.rmtree(self.source_path, ignore_errors=True)
44
+ os.makedirs(self.source_path)
45
+ self.traverse_resource_dir(importlib_resources.files().joinpath('resources', 'project'))
46
+ print(f"Initialized project in {self.source_path}")
47
+ except Exception as e:
48
+ traceback.print_stack()
49
+ print(f"Error initializing project in {self.source_path} : {e}")
50
+ sys.exit(0)
51
+ self.output_path = os.path.join(self.source_path, 'target')
52
+ self.log_path = os.path.join(self.output_path, 'log.txt')
53
+ shutil.rmtree(self.output_path, ignore_errors=True)
54
+ os.makedirs(self.output_path)
55
+ self.output_source_path = os.path.join(self.output_path, 'sources')
56
+ os.makedirs(self.output_source_path)
57
+ self.output_output_path = os.path.join(self.output_path, 'outputs')
58
+ os.makedirs(self.output_output_path)
59
+
60
+ def traverse_resource_dir(self, resource: Traversable, folder=''):
61
+ for file in resource.iterdir():
62
+ if file.is_dir():
63
+ os.makedirs(os.path.join(self.source_path, folder, file.name))
64
+ self.traverse_resource_dir(resource.joinpath(file.name), file.name)
65
+ if file.is_file():
66
+ self.copy_resource_file(folder, file)
67
+
68
+ def copy_resource_file(self, folder: str, resource: Traversable):
69
+ file_name = os.path.join(self.source_path, folder, resource.name)
70
+ with open(file_name, "w") as file:
71
+ # Write the string to the file
72
+ file.write(resource.read_text())
@@ -0,0 +1,83 @@
1
+ from pyspark.sql import SparkSession
2
+ from pyspark.sql.types import DataType, StructType, ArrayType, DecimalType
3
+ from .mainConfig import MainConfig
4
+
5
+
6
+ class MiniSparky:
7
+ def __init__(self, config: MainConfig):
8
+ self._config = config
9
+ self.spark = SparkSession.builder \
10
+ .appName("dfCore") \
11
+ .master("local[1]") \
12
+ .config("spark.driver.extraJavaOptions", "-Dlog4j.logger.level=FATAL") \
13
+ .config("spark.executor.extraJavaOptions", "-Dlog4j.logger.level=FATAL") \
14
+ .config("spark.log.level", "FATAL") \
15
+ .config("spark.driver.memory", "512m") \
16
+ .config("spark.executor.memory", "512m") \
17
+ .config("spark.executor.cores", "1") \
18
+ .config("spark.ui.enabled", "false") \
19
+ .getOrCreate()
20
+
21
+ self.spark.sql(
22
+ """SELECT CAST(-87.68 as DECIMAL(10,2)) `decimal` , CAST(13518864 as BIGINT) `bigint`, CAST('Western Ave & Walton St' as STRING) `string`, CAST(130 AS INT) `int`, CAST(130 AS INT) `integer`, CAST(41.90331 as FLOAT) `float` , CAST(87.67695 as DOUBLE) `double`, CAST('2017-03-31' as DATE) `date`, CAST('2017-03-31T23:19:17.000+0000' as TIMESTAMP) `timestamp` , true `boolean`, CAST(9999999999 as BIGINT) `long`
23
+ UNION ALL
24
+ SELECT CAST(-8127.68 as DECIMAL(10,2)) `decimal` , CAST(1518864 as BIGINT) `bigint`, CAST('Western Ave & Walton St' as STRING) `string`, CAST(130 AS INT) `int`, CAST(130 AS INT) `integer`, CAST(41.90331 as FLOAT) `float` , CAST(87.67695 as DOUBLE) `double`, CAST('2020-03-31' as DATE) `date`, CAST('2020-03-31T23:19:17' as TIMESTAMP) `timestamp` , true `boolean`, CAST(99999999991 as BIGINT) `long`
25
+ """).createOrReplaceTempView("datatypes")
26
+
27
+ _res = self._config.pg.sql("select meta.svc_select_attribute_types_spark_to_hive()")
28
+ self.type_map = {}
29
+ for x in _res:
30
+ self.type_map[x['spark_type']] = x['hive_type']
31
+
32
+ @staticmethod
33
+ def get_spark_type(spark_type: DataType) -> str:
34
+ match spark_type:
35
+ case StructType(_):
36
+ return "StructType"
37
+ case ArrayType(_, _):
38
+ return "ArrayType"
39
+ case DecimalType():
40
+ return "DecimalType"
41
+ case _:
42
+ return str(spark_type).rstrip("()")
43
+
44
+ def execute_query(self, query: str):
45
+ try:
46
+ df = self.spark.sql(query)
47
+ field = df.schema.fields[0]
48
+ is_null = df.head()[0] is None
49
+ col_name = field.name
50
+ dt = field.dataType
51
+ spark_type = self.get_spark_type(dt)
52
+ att_schema = field.dataType.json()
53
+ data_type = self.type_map.get(spark_type)
54
+
55
+ if is_null:
56
+ return {
57
+ "type": "warning",
58
+ "data_type": data_type,
59
+ "att_schema": att_schema,
60
+ "message": "NULL values detected! This typically indicates improper type casting or that you're doing some very complex logic"
61
+ }
62
+ elif col_name != "col1":
63
+ return {
64
+ "type": "error",
65
+ "message": "Extraneous input detected at end of expression"
66
+ }
67
+ else:
68
+ return {
69
+ "type": "success",
70
+ "data_type": data_type,
71
+ "att_schema": att_schema
72
+ }
73
+ except Exception as e:
74
+ print("spark exception, printing stack trace")
75
+ return {
76
+ "type": "error",
77
+ "message": str(e)
78
+ }
79
+
80
+ def stop(self):
81
+ self.spark.sparkContext.stop()
82
+ del self.spark
83
+
@@ -0,0 +1,70 @@
1
+ import os
2
+ import sys
3
+ import psycopg2
4
+ from importlib_resources import files
5
+
6
+
7
+ class Pg:
8
+ def __init__(self, connection_string: str = None, initialize=False):
9
+ try:
10
+ if initialize:
11
+ self.initialize(connection_string)
12
+ else:
13
+ conn_string = os.environ.get('DATAFORGE_PG_CONNECTION')
14
+ print(f"Connecting to Postgres..")
15
+ if conn_string is None:
16
+ print("Postgres connection is not initialized. Run with --mode init")
17
+ sys.exit(1)
18
+ self.conn = psycopg2.connect(conn_string)
19
+ self.conn.set_session(autocommit=True)
20
+
21
+ except Exception as e:
22
+ print(f"Error connecting to Postgres: {e}")
23
+ sys.exit(1)
24
+
25
+ def sql(self, query: str, params=None, fetch=True):
26
+ # Execute a query
27
+ cur = self.conn.cursor()
28
+ cur.execute(query, params)
29
+ # Retrieve query results
30
+ res = cur.fetchone() if fetch else [None]
31
+ cur.close()
32
+ return res[0]
33
+
34
+ def initialize(self, connection_string: str):
35
+ # Execute a query
36
+ try:
37
+ print("Platform :", sys.platform)
38
+ self.conn = psycopg2.connect(connection_string)
39
+ self.sql("select 1") # execute test query
40
+ match sys.platform:
41
+ case 'win32' | 'cygwin':
42
+ os.system(f"SETX DATAFORGE_PG_CONNECTION \"{connection_string}\"")
43
+ case _:
44
+ os.system(f"export DATAFORGE_PG_CONNECTION=\"{connection_string}\"")
45
+ # Change connection
46
+ print("Please restart your console")
47
+ except Exception as e:
48
+ print(f"Error initializing Postgres database or insufficient permissions. Details: {e}")
49
+ sys.exit(1)
50
+
51
+ def seed(self):
52
+ schemas = self.sql(
53
+ "select string_agg(schema_name,',') from information_schema.schemata where schema_name IN ('meta','log')")
54
+ if schemas:
55
+ if not self.confirm_action(
56
+ f"All objects in schema(s) {schemas} in postgres database will be deleted. Do you want to continue (y/n)?"):
57
+ sys.exit(1)
58
+ # Drop schemas
59
+ self.sql("DROP SCHEMA IF EXISTS meta CASCADE;"
60
+ "DROP SCHEMA IF EXISTS log CASCADE;", fetch=False)
61
+ # Deploy DB code
62
+ print("Initializing database..")
63
+ deploy_sql = files().joinpath('resources', 'pg_deploy.sql').read_text()
64
+ self.sql(deploy_sql, fetch=False)
65
+ print("Database initialized")
66
+
67
+ @staticmethod
68
+ def confirm_action(message: str):
69
+ confirmation = input(message).strip().lower()
70
+ return confirmation in ('yes', 'y')