-
Notifications
You must be signed in to change notification settings - Fork 0
/
Organizations.py
61 lines (50 loc) · 1.42 KB
/
Organizations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import datetime
import os
from dotenv import load_dotenv
from db_connect import *
from dataclasses import dataclass
@dataclass
class Organization:
FilerId: int
OrgName: str
OrgType: str
load_dotenv()
connection = db_connect(os.getenv('CALACCESSDB'))
cursor = connection.cursor()
# get all organizations
organizations = []
cursor.execute("""
SELECT DISTINCT FILER_ID, NAML, FILER_TYPE
FROM FILERNAME_CD
WHERE FILER_TYPE IN ("EMPLOYER", "SLATE MAILER ORGANIZATIONS", "RECIPIENT COMMITTEE")
""")
db_organizations = cursor.fetchall()
print("Unique Organizations: ", cursor.rowcount)
for org in db_organizations:
organizations.append(
Organization(
FilerId=org[0],
OrgName=org[1],
OrgType=org[2]
)
)
cursor.close()
connection.close()
# insert Organizations into db
connection = db_connect(os.getenv('DEVDB'))
cursor = connection.cursor()
insert_query = """
INSERT IGNORE INTO Organizations (FilerId, OrgName, OrgType)
VALUES (%s, %s, %s)
"""
data_to_insert = [
(org.FilerId, org.OrgName, org.OrgType)
for org in organizations
]
batch_size = 1000
for i in range(0, len(data_to_insert), batch_size):
batch_data = data_to_insert[i:i + batch_size]
cursor.executemany(insert_query, batch_data)
connection.commit()
cursor.close()
connection.close()