Skip to content

Instantly share code, notes, and snippets.

View vrutik2809's full-sized avatar

Vrutik Prajapati vrutik2809

View GitHub Profile
@vrutik2809
vrutik2809 / backend_workflow.md
Created April 14, 2022 19:56
Backend workflow for Mathesar database exporting functionality

The backend workflow is illustrated in the following chart: image The exporting algorithm for exporting the table will follow as:

  • validate the body
    • check whether database exist or not
    • check whether table <schema_name>.<table_name> exist or not
    • check whether given fields are there in the table or not
  • create engine using create_mathesar_engine function by passing db_name
  • use the created engine to relfect the table with the given schema_name and table_name using reflect_table function
  • then it will follow the logic mentioned in the exporting function prototypes(e.g., refer export_table_to_dict function)
@vrutik2809
vrutik2809 / export_to_sheet.py
Created April 3, 2022 13:43
Exporting SQL table or schema to Google Sheets
import os.path
from sqlalchemy import create_engine, inspect, Table, MetaData
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
@vrutik2809
vrutik2809 / utils.py
Last active April 7, 2022 17:31
Function prototypes for exporting tables from database
from sqlalchemy import create_engine
from sqlalchemy import inspect, Table, MetaData
from xlsxwriter import Workbook
# TODO replace variable with appropiate values
DB_URL = f"postgresql://{user}:{password}@{hostname}:{port}/{database-name}"
engine = create_engine(DB_URL)
def reflect_table(name, schema, engine, metadata=None, connection_to_use=None):