-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bills.py
95 lines (84 loc) · 2.89 KB
/
Bills.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import datetime
import os
from dotenv import load_dotenv
from db_connect import *
from dataclasses import dataclass
@dataclass
class Bill:
BillId: int
BillType: str
BillNumber: str
BillState: str
BillStatus: str
BillHouse: str
BillSession: int
BillUSState: str
SessionYear: datetime
BillTitle: str
BillDigest: str
load_dotenv()
connection = db_connect(os.getenv('DDDB'))
cursor = connection.cursor()
# get all bills
bills = []
cursor.execute("""
SELECT DISTINCT BillId, BillType, BillNumber, BillState, BillStatus, BillHouse, BillSession, BillUSState, SessionYear, BillTitle, BillDigest
FROM (
SELECT
Bill.bid AS BillId,
Bill.type AS BillType,
Bill.number AS BillNumber,
Bill.billState AS BillState,
Bill.status AS BillStatus,
Bill.house AS BillHouse,
Bill.session AS BillSession,
Bill.state AS BillUSState,
Bill.sessionYear AS SessionYear,
BillVersion.title AS BillTitle,
BillVersion.digest AS BillDigest,
ROW_NUMBER() OVER (PARTITION BY Bill.bid ORDER BY Bill.bid) AS rn
FROM
Bill
JOIN
BillVersion ON Bill.bid = BillVersion.bid
) AS ranked_bills
WHERE rn = 1;
""")
db_bills = cursor.fetchall()
print("Bill Count: ", cursor.rowcount)
for bill in db_bills:
bills.append(
Bill(
BillId= bill[0],
BillType= bill[1],
BillNumber= bill[2],
BillState= bill[3],
BillStatus= bill[4],
BillHouse= bill[5],
BillSession= bill[6],
BillUSState= bill[7],
SessionYear= bill[8],
BillTitle= bill[9],
BillDigest= bill[10]
)
)
cursor.close()
connection.close()
# inset bills into db
connection = db_connect(os.getenv('DEVDB'))
cursor = connection.cursor()
insert_query = """
INSERT INTO Bills (BillId, BillType, BillNumber, BillState, BillStatus, BillHouse, BillSession, BillUSState, SessionYear, BillTitle, BillDigest)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
data_to_insert = [
(bill.BillId, bill.BillType, bill.BillNumber, bill.BillState, bill.BillStatus, bill.BillHouse, bill.BillSession, bill.BillUSState, bill.SessionYear, bill.BillTitle, bill.BillDigest)
for bill in bills
]
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()