def create_views(): db = LocalSession() queries = db.table_queries queries = [ queries.create_view_notebooks.sql, queries.create_view_desktops.sql, ] for query in queries: db.exec_query(query)
def drop_duplicates(): db = LocalSession() queries = db.drop_duplicates_queries queries = ( queries.drop_product_duplicates, queries.drop_review_duplicates, queries.drop_category_duplicates, queries.drop_cpu_rating_duplicates, queries.drop_gpu_rating_duplicates, ) for query in queries: db.exec_query(query.sql)
def drop_indexes(): db = LocalSession() queries = db.drop_index_queries queries = ( queries.drop_category_indexes, queries.drop_product_indexes, queries.drop_review_indexes, queries.drop_specs_indexes, queries.drop_cpu_rating_indexes, queries.drop_gpu_rating_indexes, ) for query in queries: db.exec_query(query.sql)
def dump_into_product_details(): db = LocalSession() queries = db.insert_queries query = queries.bulk_insert_product_details _insert_from_source("product_details.json", query.sql)
def init_db(): print("* Recreate tables in meta schema") recreate_meta() db_session = LocalSession() print("* Populate users") init_users(db_session)
def dump_into_tables(): db = LocalSession() queries = db.insert_queries dumpfiles_with_queries = ( ("categories.json", queries.bulk_insert_categories), ("products.json", queries.bulk_insert_products), ("reviews.json", queries.bulk_insert_reviews), ("specs.json", queries.bulk_insert_specs), ) for source_file, query in dumpfiles_with_queries: _insert_from_source(source_file, query.sql)
def _insert_from_source(source_file, query): db = LocalSession() date = utils.parse_latest_date(DB_DUMPS_DIR) data: List[dict] = load_json(f"{DB_DUMPS_DIR}/{date}/{source_file}") db.bulk_insert_dicts(query, data)
def _copy_rating_from_files(files, table, columns): db = LocalSession() for file in files: with open(f"{RATINGS_DIR}/{file}") as f: db.copy_from(f, table, columns)
def get_dumped_product_details(): db_session = LocalSession() fetch_all = db_session.select_all("SELECT product_id FROM product_details") return {x[0] for x in fetch_all}
def get_db() -> Generator: try: db = LocalSession() yield db finally: db.close()
def get_db(): try: db_session = LocalSession() yield db_session finally: db_session.close()