-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpartner_random_generate_csv.py
46 lines (36 loc) · 1.23 KB
/
partner_random_generate_csv.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
import csv
import psycopg2
from faker import Faker
fake = Faker()
def generate_csv(file_name, total_records):
with open(file_name, mode="w") as file:
writer = csv.writer(file)
writer.writerow(["name", "email", "ref", "create_uid", "create_date", "write_uid", "write_date"])
create_uid = 1
write_uid = 1
for _ in range(total_records):
name = fake.name()
email = fake.email()
ref = fake.random_number(digits=10, fix_len=True)
writer.writerow([name, email, ref, create_uid, "NOW()", write_uid, "NOW()"])
def copy_from_csv(file_name):
# Use PG* environment variables to connect to database
conn = psycopg2.connect()
cursor = conn.cursor()
try:
with open(file_name) as file:
cursor.copy_expert(
"""
COPY res_partner(name, email, ref, create_uid, create_date, write_uid, write_date)
FROM STDIN WITH CSV HEADER
""",
file,
)
conn.commit()
finally:
cursor.close()
conn.close()
if __name__ == "__main__":
fname = "partners.csv"
# generate_csv(fname, total_records=2000000)
copy_from_csv(fname)