Spaces:
Sleeping
Sleeping
File size: 10,890 Bytes
50bf486 | 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | """
Database seeding for the SQL Debug Environment.
Sets up the tables and populates them with enough data to make the tasks
non-trivial. We're using realistic-ish names and values so the agent's
output looks sensible in logs.
Two schemas:
- employees/departments (Task 1)
- users/orders/order_items/products (Tasks 2 & 3)
"""
from __future__ import annotations
import sqlite3
def seed_database(conn: sqlite3.Connection) -> None:
"""Build out the tables and fill them with sample data."""
cur = conn.cursor()
# ββ employees & departments (Task 1) βββββββββββββββββββββββββββββ
cur.executescript(
"""
CREATE TABLE IF NOT EXISTS departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
budget REAL NOT NULL,
manager_id INTEGER
);
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
department TEXT NOT NULL,
salary REAL NOT NULL,
hire_date TEXT NOT NULL
);
"""
)
departments_data = [
(1, "Engineering", 2500000.00, 1),
(2, "Marketing", 800000.00, 5),
(3, "Sales", 1200000.00, 9),
(4, "Human Resources", 600000.00, 13),
(5, "Finance", 900000.00, 17),
(6, "Product", 1100000.00, 21),
]
cur.executemany(
"INSERT OR IGNORE INTO departments (id, name, budget, manager_id) VALUES (?, ?, ?, ?)",
departments_data,
)
# 24 employees across 6 departments β 4 per dept
employees_data = [
# Engineering
(1, "Priya Sharma", "Engineering", 145000.00, "2019-03-15"),
(2, "James Chen", "Engineering", 138000.00, "2020-07-01"),
(3, "Aisha Patel", "Engineering", 152000.00, "2018-11-20"),
(4, "Marcus Johnson", "Engineering", 128000.00, "2021-01-10"),
# Marketing
(5, "Sofia Rodriguez", "Marketing", 95000.00, "2019-06-01"),
(6, "David Kim", "Marketing", 88000.00, "2020-09-15"),
(7, "Emma Watson", "Marketing", 102000.00, "2018-04-22"),
(8, "Lucas Brown", "Marketing", 79000.00, "2022-02-14"),
# Sales
(9, "Olivia Martinez", "Sales", 115000.00, "2017-08-30"),
(10, "Noah Williams", "Sales", 105000.00, "2019-12-01"),
(11, "Ava Thompson", "Sales", 98000.00, "2020-03-17"),
(12, "Liam Davis", "Sales", 110000.00, "2018-10-05"),
# HR
(13, "Isabella Garcia", "Human Resources", 92000.00, "2019-01-20"),
(14, "Ethan Moore", "Human Resources", 85000.00, "2020-06-15"),
(15, "Mia Anderson", "Human Resources", 78000.00, "2021-09-01"),
(16, "Alexander Taylor", "Human Resources", 82000.00, "2022-04-10"),
# Finance
(17, "Charlotte White", "Finance", 125000.00, "2018-02-28"),
(18, "Benjamin Harris", "Finance", 118000.00, "2019-07-14"),
(19, "Amelia Clark", "Finance", 105000.00, "2020-11-30"),
(20, "Daniel Lewis", "Finance", 112000.00, "2021-03-22"),
# Product
(21, "Harper Robinson", "Product", 135000.00, "2018-05-10"),
(22, "Sebastian Walker", "Product", 128000.00, "2019-09-18"),
(23, "Evelyn Hall", "Product", 115000.00, "2020-12-07"),
(24, "Jack Allen", "Product", 122000.00, "2021-06-25"),
]
cur.executemany(
"INSERT OR IGNORE INTO employees (id, name, department, salary, hire_date) VALUES (?, ?, ?, ?, ?)",
employees_data,
)
# ββ e-commerce tables (Tasks 2 & 3) ββββββββββββββββββββββββββββββ
cur.executescript(
"""
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
region TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS orders (
order_id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
created_at TEXT NOT NULL,
status TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
CREATE TABLE IF NOT EXISTS products (
product_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
category TEXT NOT NULL,
unit_price REAL NOT NULL
);
CREATE TABLE IF NOT EXISTS order_items (
item_id INTEGER PRIMARY KEY,
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
price REAL NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
"""
)
# -- Users (went with Friends character names for fun) --
users_data = [
(1, "Rachel Green", "rachel.green@example.com", "North America"),
(2, "Ross Geller", "ross.geller@example.com", "North America"),
(3, "Monica Bing", "monica.bing@example.com", "Europe"),
(4, "Chandler Bing", "chandler.bing@example.com", "Europe"),
(5, "Joey Tribbiani", "joey.tribbiani@example.com", "North America"),
(6, "Phoebe Buffay", "phoebe.buffay@example.com", "Asia Pacific"),
(7, "Gunther Central", "gunther@example.com", "North America"),
(8, "Janice Hosenstein", "janice.h@example.com", "Europe"),
(9, "Mike Hannigan", "mike.hannigan@example.com", "Asia Pacific"),
(10, "Emily Waltham", "emily.w@example.com", "Europe"),
(11, "Richard Burke", "richard.burke@example.com", "North America"),
(12, "Carol Willick", "carol.willick@example.com", "North America"),
(13, "Susan Bunch", "susan.bunch@example.com", "Asia Pacific"),
(14, "David Scientist", "david.sci@example.com", "Europe"),
(15, "Tag Jones", "tag.jones@example.com", "North America"),
]
cur.executemany(
"INSERT OR IGNORE INTO users (user_id, name, email, region) VALUES (?, ?, ?, ?)",
users_data,
)
# -- Products --
products_data = [
(1, "Wireless Bluetooth Headphones", "Electronics", 79.99),
(2, "USB-C Hub Adapter", "Electronics", 34.99),
(3, "Mechanical Keyboard", "Electronics", 129.99),
(4, "Cotton Crew-Neck T-Shirt", "Clothing", 19.99),
(5, "Slim Fit Chinos", "Clothing", 49.99),
(6, "Running Shoes Pro", "Clothing", 89.99),
(7, "The Pragmatic Programmer", "Books", 42.99),
(8, "Designing Data-Intensive Applications", "Books", 38.99),
(9, "Clean Code", "Books", 35.99),
(10, "Yoga Mat Premium", "Sports", 29.99),
(11, "Resistance Bands Set", "Sports", 24.99),
(12, "Stainless Steel Water Bottle", "Kitchen", 18.99),
(13, "French Press Coffee Maker", "Kitchen", 27.99),
(14, "LED Desk Lamp", "Home", 44.99),
(15, "Ergonomic Mouse Pad", "Electronics", 14.99),
]
cur.executemany(
"INSERT OR IGNORE INTO products (product_id, name, category, unit_price) VALUES (?, ?, ?, ?)",
products_data,
)
# -- Orders (spread across Jan-Apr 2024) --
orders_data = [
(1, 1, "2024-01-15", "completed"),
(2, 1, "2024-02-20", "completed"),
(3, 2, "2024-01-22", "completed"),
(4, 2, "2024-03-10", "completed"),
(5, 2, "2024-04-05", "shipped"),
(6, 3, "2024-02-14", "completed"),
(7, 3, "2024-03-28", "completed"),
(8, 4, "2024-01-30", "completed"),
(9, 5, "2024-02-18", "completed"),
(10, 5, "2024-03-15", "completed"),
(11, 5, "2024-04-01", "shipped"),
(12, 6, "2024-01-10", "completed"),
(13, 7, "2024-03-05", "completed"),
(14, 8, "2024-02-25", "returned"),
(15, 9, "2024-04-02", "completed"),
(16, 9, "2024-04-08", "shipped"),
(17, 10, "2024-01-18", "completed"),
(18, 11, "2024-02-28", "completed"),
(19, 11, "2024-03-20", "completed"),
(20, 12, "2024-03-12", "completed"),
(21, 13, "2024-01-25", "completed"),
(22, 14, "2024-04-10", "shipped"),
(23, 15, "2024-02-08", "completed"),
(24, 1, "2024-04-12", "shipped"),
(25, 3, "2024-04-15", "pending"),
]
cur.executemany(
"INSERT OR IGNORE INTO orders (order_id, user_id, created_at, status) VALUES (?, ?, ?, ?)",
orders_data,
)
# -- Line items --
order_items_data = [
(1, 1, 1, 1, 79.99),
(2, 1, 7, 2, 42.99),
(3, 2, 3, 1, 129.99),
(4, 2, 4, 3, 19.99),
(5, 3, 2, 2, 34.99),
(6, 3, 8, 1, 38.99),
(7, 4, 5, 1, 49.99),
(8, 4, 6, 1, 89.99),
(9, 5, 14, 1, 44.99),
(10, 5, 15, 2, 14.99),
(11, 6, 10, 1, 29.99),
(12, 6, 12, 2, 18.99),
(13, 7, 1, 1, 79.99),
(14, 7, 9, 1, 35.99),
(15, 8, 3, 1, 129.99),
(16, 9, 7, 1, 42.99),
(17, 9, 11, 2, 24.99),
(18, 10, 6, 1, 89.99),
(19, 10, 4, 2, 19.99),
(20, 11, 2, 1, 34.99),
(21, 11, 13, 1, 27.99),
(22, 12, 8, 1, 38.99),
(23, 13, 1, 2, 79.99),
(24, 14, 5, 1, 49.99),
(25, 15, 3, 1, 129.99),
(26, 15, 10, 1, 29.99),
(27, 16, 14, 2, 44.99),
(28, 16, 12, 1, 18.99),
(29, 17, 9, 1, 35.99),
(30, 18, 2, 1, 34.99),
(31, 18, 7, 1, 42.99),
(32, 19, 11, 1, 24.99),
(33, 19, 15, 3, 14.99),
(34, 20, 6, 1, 89.99),
(35, 20, 4, 1, 19.99),
(36, 21, 13, 2, 27.99),
(37, 21, 12, 1, 18.99),
(38, 22, 1, 1, 79.99),
(39, 23, 3, 1, 129.99),
(40, 23, 5, 2, 49.99),
(41, 24, 10, 1, 29.99),
(42, 24, 14, 1, 44.99),
(43, 25, 8, 1, 38.99),
(44, 25, 2, 1, 34.99),
]
cur.executemany(
"INSERT OR IGNORE INTO order_items (item_id, order_id, product_id, quantity, price) VALUES (?, ?, ?, ?, ?)",
order_items_data,
)
# ββ Indexes ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# These matter for Task 3 β without them, the optimizer can't use
# index lookups and everything becomes a full table scan.
cur.executescript(
"""
CREATE INDEX IF NOT EXISTS idx_orders_user_id ON orders(user_id);
CREATE INDEX IF NOT EXISTS idx_order_items_order_id ON order_items(order_id);
CREATE INDEX IF NOT EXISTS idx_employees_department ON employees(department);
CREATE INDEX IF NOT EXISTS idx_employees_salary ON employees(salary);
"""
)
conn.commit()
|