How to use _with_conn method in dbt-osmosis

Best Python code snippet using dbt-osmosis_python

osmosis.py

Source:osmosis.py Github

copy

Full Screen

...302 """Used for jobs which are intended to be submitted to a thread pool,303 the 'master' thread should always have an available connection for the duration of304 typical program runtime by virtue of the `_verify_connection` method.305 Threads however require singleton seeding"""306 def _with_conn() -> T:307 self.adapter.connections.set_connection_name()308 return fn(*args, **kwargs)309 return _with_conn310 @property311 def project_name(self) -> str:312 """dbt project name"""313 return self.config.project_name314 @property315 def project_root(self) -> str:316 """dbt project root"""317 return self.config.project_root318 @property319 def manifest(self) -> ManifestProxy:320 """dbt manifest dict"""...

Full Screen

Full Screen

catalogs.py

Source:catalogs.py Github

copy

Full Screen

...78 self.row_catalog['subcatalogs'].pop(pathname)79 self.subcatalogs.pop(pathname)80 def _row_catalog(self):81 return self._row_catalog82def _with_conn(cursor_factory=RealDictCursor):83 def decarator(func):84 def wraper(self, *args):85 conn = connect(**self.db_conf)86 try:87 with conn.cursor(cursor_factory=cursor_factory) as curs:88 result = func(self, curs, *args)89 except Exception as exp:90 conn.rollback()91 raise exp92 else:93 conn.commit()94 return result95 finally:96 conn.close()97 return wraper98 return decarator99class MainCatalog(Catalog):100 def __init__(self, config, db_conf, main_config):101 row_catalog = config.get_cfg()102 if row_catalog == {}:103 row_catalog['main_config'] = main_config104 row_catalog['name'] = 'Главная страница'105 row_catalog['subcatalogs'] = {}106 config.save_cfg()107 self.main_config = row_catalog['main_config']108 super().__init__(row_catalog, config, db_conf)109 self.__initializate()110 @_with_conn()111 def __initializate(self, curs):112 if self.main_config != '':113 ct_vars = ',' + self.main_config114 else:115 ct_vars = ''116 curs.execute(f'''CREATE TABLE 117 IF NOT EXISTS 118 main(id BIGSERIAL PRIMARY KEY, 119 table_name VARCHAR(255) NOT NULL120 {ct_vars});''')121 @_with_conn()122 def get_by_id(self, curs, id):123 curs.execute(f'''SELECT table_name FROM main 124 WHERE id={id};''')125 table_name = curs.fetchone()126 curs.execute(f'''SELECT * FROM main 127 WHERE id={id}128 INNER JOIN "{table_name}" ch 129 ON ch.id = main.id;''')130 product = curs.fetchone()131 if product is None:132 raise ProductNotFound(id)133 return product134 @_with_conn()135 def remove_by_id(self, curs, id):136 curs = self.conn.cursor()137 curs.execute(f'''SELECT table_name FROM main 138 WHERE id={id};''')139 table_name = curs.fetchone()140 curs.execute(f'''DELETE FROM main 141 WHERE main.id={id}';''')142 curs.execute(f'''DELETE FROM {table_name} as ch143 WHERE ch.id = {id};''')144class ProductCatalog:145 def __init__(self, row_catalog, db_conf, check=False):146 self.db_conf = db_conf147 self.name = row_catalog['name']148 self.table_name = row_catalog['table_name']149 self.config = row_catalog['config']150 self.__initializate(check)151 @_with_conn()152 def __initializate(self, curs, check):153 if self.config != '':154 ct_vars = ',' + self.config155 else:156 ct_vars = ''157 if check:158 curs.execute(f'''CREATE TABLE159 "{self.table_name}"160 (id INT NOT NULL UNIQUE161 {ct_vars});''')162 else:163 curs.execute(f'''CREATE TABLE 164 IF NOT EXISTS "{self.table_name}"165 (id INT NOT NULL UNIQUE166 {ct_vars});''')167 @_with_conn()168 def add_products(self, curs, products): 169 main_columns = ProductCatalog._get_columns(curs, 'main')170 catalog_columns = ProductCatalog._get_columns(curs, self.table_name)171 for product in products:172 product.pop('id', None)173 product['table_name'] = self.table_name174 ProductCatalog._insert_(curs, 'main', format_dict(product, main_columns))175 product['id'] = curs.fetchone()['id']176 ProductCatalog._insert_(curs, 177 self.table_name, 178 format_dict(product, catalog_columns))179 @_with_conn()180 def get_product(self, curs, id):181 curs.execute(f'''SELECT * FROM main 182 INNER JOIN "{self.table_name}" ch 183 ON ch.id = main.id184 WHERE ch.id={id}''')185 product = curs.fetchone()186 if product is None:187 raise ProductNotFound(id)188 return remove_null_attrs(product)189 @_with_conn()190 def get_product_main_specs(self, curs, id):191 curs.execute(f'''SELECT * FROM main ch192 WHERE ch.id={id}''')193 product = curs.fetchone()194 if product is None:195 raise ProductNotFound(id)196 return remove_null_attrs(product)197 @_with_conn()198 def get_product_specs(self, curs, id):199 curs.execute(f'''SELECT * FROM {self.table_name} ch200 WHERE ch.id={id}''')201 product = curs.fetchone()202 if product is None:203 raise ProductNotFound(id)204 return remove_null_attrs(product)205 @_with_conn()206 def get_products(self, curs):207 curs.execute(f'''SELECT * FROM main208 INNER JOIN "{self.table_name}" ch 209 ON ch.id = main.id;''')210 return curs.fetchall()211 @_with_conn()212 def get_products_main_specs(self, curs):213 curs.execute(f'''SELECT * FROM main 214 WHERE main.table_name='{self.table_name}';''')215 return curs.fetchall()216 @_with_conn()217 def get_columns(self, curs):218 return ProductCatalog._get_columns(curs, 'main') + ProductCatalog._get_columns(curs, self.table_name)219 @_with_conn()220 def remove_product(self, curs, id):221 curs.execute(f'''DELETE FROM {self.table_name} as ch222 WHERE ch.id = {id};''')223 curs.execute(f'''DELETE FROM main224 WHERE main.table_name = '{self.table_name}';''')225 @_with_conn()226 def remove_self(self, curs):227 curs.execute(f'''DROP TABLE "{self.table_name}";''')228 curs.execute(f'''DELETE FROM main229 WHERE main.table_name = '{self.table_name}';''')230 @staticmethod231 def _get_columns(curs, table_name):232 curs.execute(f'''SELECT column_name233 FROM INFORMATION_SCHEMA.COLUMNS 234 WHERE table_name = '{table_name}';''')235 columns = [column['column_name'] for column in curs.fetchall()]236 if columns == []:237 raise CatalogNotFound(table_name)238 print(columns)239 return columns...

Full Screen

Full Screen

proxima_be_repo.py

Source:proxima_be_repo.py Github

copy

Full Screen

1#! /usr/bin/env python2# -*- coding: utf8 -*-3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7# 8# http://www.apache.org/licenses/LICENSE-2.09# 10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15#16# Proxima SE Repository source17#18from common.mysql_utils import MySQL19class ProximaSERepoSource(object):20 def __init__(self, jdbc):21 self._jdbc = jdbc22 def __del__(self):23 pass24 def jdbc_str(self):25 return self._jdbc26 def is_valid(self) -> bool:27 pass28 def tables(self):29 pass30 def schema(self, table):31 pass32 def counts(self, table):33 pass34class MysqlRepoSource(ProximaSERepoSource):35 def __init__(self, jdbc, table):36 ProximaSERepoSource.__init__(self, jdbc)37 self._table = table38 self._with_conn = None39 self._conn = MySQL.connect(self._jdbc)40 def __del__(self):41 pass42 def __enter__(self):43 if self._with_conn:44 self._with_conn = MySQL.connect(self._jdbc)45 return self._with_conn46 def __exit__(self, exc_type, exc_val, exc_tb):47 if self._with_conn:48 self._with_conn.close()49 self._with_conn = None50 def is_valid(self) -> bool:51 return True if self._conn and len(self.schema(self._table)) != 0 else False52 def tables(self):53 return MySQL.tables(self._conn)54 def schema(self, table=None):55 table = self._table if not table else table56 return MySQL.schema(self._conn, table)57 def counts(self, table=None):58 table = self._table if not table else table...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run dbt-osmosis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful