-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed_tables.py
47 lines (36 loc) · 1.1 KB
/
seed_tables.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
import psycopg2
from faker import Faker
import uuid
import random
# Connect to the PostgreSQL database
USERNAME = "postgres"
PASSWORD = "postgres"
ADDRESS = "localhost"
PORT = "5432"
DBNAME = "test_db"
# Create a cursor object to interact with the database
conn = psycopg2.connect(
database=DBNAME, user=USERNAME, password=PASSWORD, host=ADDRESS, port=PORT
)
cursor = conn.cursor()
fake = Faker()
num_records = 100
for _ in range(num_records):
# user table
user_uuid = str(uuid.uuid4())
name = fake.name()
email = fake.email()
# invoice_table
invoice_id = str(uuid.uuid4())
user_id = user_uuid
amount = random.randint(1, 100)
invoice_date = fake.date_time_this_year()
# Construct the SQL query
query1 = f"INSERT INTO loadtesting.user (id, name, email) VALUES ('{user_uuid}', '{name}', '{email}')"
query2 = f"INSERT INTO loadtesting.invoice (id, user_id, amount, invoice_date) VALUES ('{invoice_id}', '{user_id}', {amount}, '{invoice_date}')"
# Execute the queries
cursor.execute(query1)
cursor.execute(query2)
conn.commit()
cursor.close()
conn.close()