Skip to content

Commit

Permalink
Added scripts to manually update Cloud SQL database.
Browse files Browse the repository at this point in the history
  • Loading branch information
marvintensuan committed Dec 30, 2020
1 parent a7b826b commit d24bd90
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scripts_py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
pyvenv.cfg
Include
Lib
Scripts
Binary file added scripts_py/requirements.txt
Binary file not shown.
7 changes: 7 additions & 0 deletions scripts_py/sdl_onlinecourse.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
course_name,facilitator,onlinecourse_date_completed,link_to_certificate
Machine Learning,Andrew Ng,2020-09-01,https://www.coursera.org/account/accomplishments/certificate/D98R6TFBN3JP
Project SPARTA: Data Visualization Fundamentals,Department of Science and Technology,2020-07-23,https://coursebank.ph/certificates/479392b2cd21442a8f79de78bbfc6090
Project SPARTA: Dashboard and Drilldown Analytics,Department of Science and Technology,2020-06-14,https://coursebank.ph/certificates/099dfbb5ae434ca1af7688c22647ddf3
Project SPARTA: Essential Excel Skills for Data Preparation and Analysis,Department of Science and Technology,2020-04-15,https://coursebank.ph/certificates/3d3c3b1f129e484e8cebc670ebc32371
Project SPARTA: Getting Grounded on Analytics,Department of Science and Technology,2020-04-03,https://coursebank.ph/certificates/2dafee277c8c45b29932422f77db1213
Project SPARTA: Data Management Fundamentals,Department of Science and Technology,,
14 changes: 14 additions & 0 deletions scripts_py/sdl_webinars.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
webinar_name,organizer,webinar_date,html_desc
LinkedIn Metadata Day 2020,LinkedIn,15/12/2020,"Zoom webinar bringing together data engineers from large tech companies like Google, LinkedIn, AirBnb, and Paypal to discuss latest developments in the field of data governance; specifically with regards to management of metadata. <a href=""https://metadataday2020.splashthat.com/""> Event page.</a>"
"Drawing Distinctions: An Introduction to Financing Companies, Lending Companies, Foundations and Microfinance NGOs",Securities and Exchange Commission,28/10/2020," <a href=""https://drive.google.com/file/d/1d_fOAd87YpHKUJD6NOsqO2MYuEM9MewI/view""> Link to certificate.</a>"
BASIQS: Business Model Canvas for Beginners,QBO Philippines,22/10/2020,
Wildcard QLITAN: Journey of INQBATION Graduates,QBO Philippines,29/09/2020,
WORQSHOP: Data Analytics with MoEngage,QBO Philippines,09/09/2020,
Distinguished Speaker Series Webinar: Understanding Public Finance and Taxation in a Fiat Currency System,Asian Development Bank,01/09/2020,"A one-hour webinar featuring Khalid Saeed of Worcester Polytechnic Institute who discussed the relevance of modern money theory for contemporary public finance. <a href=""""> Event page.</a>"
Claim it right: Tax deductions explained,PWC Isla Lipana & Co.,28/08/2020,"A two-hour session discussing tax-deductible items along with codal references and court rulings. <a href=""""> </a>"
WORQSHOP: Growth Hacking Your Way to Success,QBO Philippines,14/08/2020,
SEC: Mandate and Functions and The Revised Corporation Code: Salient Provisions,Securities and Exchange Commission,12/08/2020," <a href=""https://drive.google.com/file/d/18BJfZaTfk9r5LJA1KlLYHDHTsLfLyW73/view""> Link to certificate.</a>"
Sealed and Secured: The Low Down on Data Privacy and Cybersecurity,UnionBank of the Philippines,06/08/2020," <a href=""https://apptitude.xyz/dashboard/student/certificate/0x3e770C29E05038087B2B40f219E09b687d7972fA/""> Link to certificate.</a>"
Taking The Robot Out of Human,KPMG R.G Manabat & Co.,14/07/2020,"An overview on Robotic Process Automation (RPA): what it is, how industries use it to achieve strategic goals, and how KPMG can help in operationalizing RPA. (1 hr) <a href=""https://drive.google.com/file/d/1TNbSUTb30sNbBijtokUKsKKNMAuXurKO/view""> Link to certificate.</a>"
Data Science and AI Ethics,UnionBank of the Philippines,13/07/2020," <a href=""https://apptitude.xyz/dashboard/student/certificate/0xb27b8ffa9b8B00975DE2Cd5F8Ef7D91Ac3cd17F6/""> Link to certificate.</a>"
BIR Goes Digital: How to File and Pay Your Taxes Online for Corporate/Non-Individual Taxpayers,Bureau of Internal Revenue,08/06/2020," <a href=""https://drive.google.com/file/d/1PeanQY3qIND6mY32bfp9Lhd5W6G_JBpf/view""> Link to certificate.</a>"
1 change: 1 addition & 0 deletions scripts_py/table_names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["sdl_onlinecourse", "sdl_webinars"]
86 changes: 86 additions & 0 deletions scripts_py/update_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import psycopg2
from sqlalchemy import create_engine
from dotenv import load_dotenv
import os
import pandas as pd
import json

print("\nUpdating database... Please make sure that Cloud SQL Proxy is running.\n\n")

# Load environment variables
load_dotenv()

DB_USER = os.getenv('DB_USER')
TAIL = os.getenv('TAIL')
DB_NAME = os.getenv('DB_NAME')
PROJECT = os.getenv('DJANGO_PROJECT_NAME')
HOST = "127.0.0.1"

# Create sqlalchemy engine.
# To be passed to pandas DataFrame.to_sql() as arg
my_engine = str().join(
[
"postgresql+",
":".join(["psycopg2", f"//" + DB_USER, TAIL]),
"".join(["@", HOST, f"/", DB_NAME])
]
)

engine = create_engine(my_engine)
CLOUD_SQL = engine.connect()

# Connection details for psycopg2
PSQL_CONNECTION_DETAILS = str(" ").join(
[
"dbname=" + DB_NAME,
"user=" + DB_USER,
"password=" + TAIL
]
)

# Table names in the database
file = open("table_names.json", "r")
with file:
TABLE_NAMES = json.loads((file.read()))
file.close()

# Create Postgresql connection and cursor
conn = psycopg2.connect(PSQL_CONNECTION_DETAILS)
cur = conn.cursor()

#Update table

def df_convert_iloc2_as_date(df):
date_series = pd.to_datetime(df.iloc[:,2])
df.iloc[:,2] = date_series
return df

for table in TABLE_NAMES:
model = "_".join([PROJECT, table])
print(f"Updating {model}...\n\n")

add_table = pd.read_csv(table + ".csv")
add_table = df_convert_iloc2_as_date(add_table)

try:
add_table.to_sql(model, CLOUD_SQL,
if_exists='append', index=False)

print(f"Table {model} has been updated!!!\n\n")
except Exception as e:
print(e)

#Check
query = f"SELECT * FROM {model};"

cur.execute(query)
cur.fetchall()
conn.commit()

print("\n\nDone.\n")


print("Closing connections.")
cur.close()
conn.close()
CLOUD_SQL.close()

0 comments on commit d24bd90

Please sign in to comment.