import random
import uuid
import psycopg2
from faker import Faker
fake = Faker()
conn = psycopg2.connect(
host="localhost",
port=15432,
dbname="app",
user="postgres",
password="postgres"
)
cur = conn.cursor()
tenant_ids = []
for tenant_name in [
"Tenant A",
"Tenant B",
"Tenant C"
]:
tenant_id = str(uuid.uuid4())
tenant_ids.append(tenant_id)
cur.execute(
"""
INSERT INTO tenants(id, name)
VALUES (%s, %s)
""",
(tenant_id, tenant_name)
)
for tenant_id in tenant_ids:
users = []
products = []
for _ in range(20):
user_id = str(uuid.uuid4())
users.append(user_id)
cur.execute(
"""
INSERT INTO users(
id,
tenant_id,
email,
first_name,
last_name
)
VALUES (%s,%s,%s,%s,%s)
""",
(
user_id,
tenant_id,
fake.email(),
fake.first_name(),
fake.last_name()
)
)
for _ in range(15):
product_id = str(uuid.uuid4())
products.append(product_id)
cur.execute(
"""
INSERT INTO products(
id,
tenant_id,
name,
price
)
VALUES (%s,%s,%s,%s)
""",
(
product_id,
tenant_id,
fake.word(),
round(random.uniform(10, 500), 2)
)
)
for _ in range(50):
order_id = str(uuid.uuid4())
cur.execute(
"""
INSERT INTO orders(
id,
tenant_id,
user_id,
status
)
VALUES (%s,%s,%s,%s)
""",
(
order_id,
tenant_id,
random.choice(users),
random.choice([
"pending",
"completed",
"cancelled"
])
)
)
for _ in range(random.randint(1, 5)):
cur.execute(
"""
INSERT INTO order_items(
id,
order_id,
product_id,
quantity
)
VALUES (%s,%s,%s,%s)
""",
(
str(uuid.uuid4()),
order_id,
random.choice(products),
random.randint(1, 10)
)
)
cur.execute(
"""
INSERT INTO audit_logs(
id,
tenant_id,
entity_type,
entity_id,
action
)
VALUES (%s,%s,%s,%s,%s)
""",
(
str(uuid.uuid4()),
tenant_id,
"order",
order_id,
"created"
)
)
conn.commit()
conn.close()