|
| 1 | +/** |
| 2 | + * Seed script to populate the stress_test_users table. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * cd packages/db && bun run scripts/seed-stress-test-users.ts |
| 6 | + */ |
| 7 | + |
| 8 | +import { eq } from 'drizzle-orm' |
| 9 | +import { db, userTableDefinitions, userTableRows } from '../index' |
| 10 | + |
| 11 | +const WORKSPACE_ID = '098d71e1-6a36-47e3-874d-818faee0bfe8' |
| 12 | +const TABLE_NAME = 'stress_test_users' |
| 13 | +const NUM_ROWS = 100000 |
| 14 | + |
| 15 | +interface UserRow { |
| 16 | + name: string |
| 17 | + email: string |
| 18 | + age: number |
| 19 | + department: string |
| 20 | + salary: number |
| 21 | + active: boolean |
| 22 | + hire_date: string |
| 23 | + country: string |
| 24 | +} |
| 25 | + |
| 26 | +const departments = [ |
| 27 | + 'Engineering', |
| 28 | + 'Sales', |
| 29 | + 'Marketing', |
| 30 | + 'HR', |
| 31 | + 'Finance', |
| 32 | + 'Operations', |
| 33 | + 'Legal', |
| 34 | + 'Product', |
| 35 | +] |
| 36 | +const countries = [ |
| 37 | + 'USA', |
| 38 | + 'UK', |
| 39 | + 'Germany', |
| 40 | + 'France', |
| 41 | + 'Canada', |
| 42 | + 'Australia', |
| 43 | + 'Japan', |
| 44 | + 'India', |
| 45 | + 'Brazil', |
| 46 | + 'Singapore', |
| 47 | +] |
| 48 | +const firstNames = [ |
| 49 | + 'James', |
| 50 | + 'Mary', |
| 51 | + 'John', |
| 52 | + 'Patricia', |
| 53 | + 'Robert', |
| 54 | + 'Jennifer', |
| 55 | + 'Michael', |
| 56 | + 'Linda', |
| 57 | + 'William', |
| 58 | + 'Elizabeth', |
| 59 | + 'David', |
| 60 | + 'Barbara', |
| 61 | + 'Richard', |
| 62 | + 'Susan', |
| 63 | + 'Joseph', |
| 64 | + 'Jessica', |
| 65 | + 'Thomas', |
| 66 | + 'Sarah', |
| 67 | + 'Charles', |
| 68 | + 'Karen', |
| 69 | +] |
| 70 | +const lastNames = [ |
| 71 | + 'Smith', |
| 72 | + 'Johnson', |
| 73 | + 'Williams', |
| 74 | + 'Brown', |
| 75 | + 'Jones', |
| 76 | + 'Garcia', |
| 77 | + 'Miller', |
| 78 | + 'Davis', |
| 79 | + 'Rodriguez', |
| 80 | + 'Martinez', |
| 81 | + 'Hernandez', |
| 82 | + 'Lopez', |
| 83 | + 'Gonzalez', |
| 84 | + 'Wilson', |
| 85 | + 'Anderson', |
| 86 | + 'Thomas', |
| 87 | + 'Taylor', |
| 88 | + 'Moore', |
| 89 | + 'Jackson', |
| 90 | + 'Martin', |
| 91 | +] |
| 92 | + |
| 93 | +function randomItem<T>(arr: T[]): T { |
| 94 | + return arr[Math.floor(Math.random() * arr.length)] |
| 95 | +} |
| 96 | + |
| 97 | +function randomInt(min: number, max: number): number { |
| 98 | + return Math.floor(Math.random() * (max - min + 1)) + min |
| 99 | +} |
| 100 | + |
| 101 | +function randomDate(start: Date, end: Date): string { |
| 102 | + const date = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())) |
| 103 | + return date.toISOString().split('T')[0] |
| 104 | +} |
| 105 | + |
| 106 | +function generateUserRow(index: number): UserRow { |
| 107 | + const firstName = randomItem(firstNames) |
| 108 | + const lastName = randomItem(lastNames) |
| 109 | + const domain = randomItem(['gmail.com', 'yahoo.com', 'outlook.com', 'company.com', 'work.org']) |
| 110 | + |
| 111 | + return { |
| 112 | + name: `${firstName} ${lastName}`, |
| 113 | + email: `${firstName.toLowerCase()}.${lastName.toLowerCase()}${index}@${domain}`, |
| 114 | + age: randomInt(22, 65), |
| 115 | + department: randomItem(departments), |
| 116 | + salary: randomInt(40000, 200000), |
| 117 | + active: Math.random() > 0.1, // 90% active |
| 118 | + hire_date: randomDate(new Date('2015-01-01'), new Date('2024-12-31')), |
| 119 | + country: randomItem(countries), |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +async function main() { |
| 124 | + console.log(`Seeding ${TABLE_NAME} table for workspace ${WORKSPACE_ID}...`) |
| 125 | + |
| 126 | + // Get user ID for created_by |
| 127 | + const userResult = await db.execute<{ id: string }[]>(`SELECT id FROM "user" LIMIT 1`) |
| 128 | + const userId = Array.isArray(userResult) && userResult[0] ? userResult[0].id : 'system' |
| 129 | + console.log(`Using user ID: ${userId}`) |
| 130 | + |
| 131 | + // Check if table already exists |
| 132 | + const existingTable = await db |
| 133 | + .select() |
| 134 | + .from(userTableDefinitions) |
| 135 | + .where(eq(userTableDefinitions.workspaceId, WORKSPACE_ID)) |
| 136 | + .limit(1) |
| 137 | + |
| 138 | + let tableId: string |
| 139 | + |
| 140 | + if (existingTable.length > 0 && existingTable[0].name === TABLE_NAME) { |
| 141 | + tableId = existingTable[0].id |
| 142 | + console.log(`Table ${TABLE_NAME} already exists (${tableId}), clearing existing rows...`) |
| 143 | + |
| 144 | + // Delete existing rows |
| 145 | + await db.delete(userTableRows).where(eq(userTableRows.tableId, tableId)) |
| 146 | + |
| 147 | + // Reset row count (trigger will update it as we insert) |
| 148 | + await db |
| 149 | + .update(userTableDefinitions) |
| 150 | + .set({ rowCount: 0, updatedAt: new Date() }) |
| 151 | + .where(eq(userTableDefinitions.id, tableId)) |
| 152 | + } else { |
| 153 | + // Create table |
| 154 | + tableId = `tbl_${crypto.randomUUID().replace(/-/g, '')}` |
| 155 | + const now = new Date() |
| 156 | + |
| 157 | + const tableSchema = { |
| 158 | + columns: [ |
| 159 | + { name: 'name', type: 'string', required: true }, |
| 160 | + { name: 'email', type: 'string', required: true, unique: true }, |
| 161 | + { name: 'age', type: 'number', required: true }, |
| 162 | + { name: 'department', type: 'string', required: true }, |
| 163 | + { name: 'salary', type: 'number', required: true }, |
| 164 | + { name: 'active', type: 'boolean', required: true }, |
| 165 | + { name: 'hire_date', type: 'string', required: true }, |
| 166 | + { name: 'country', type: 'string', required: true }, |
| 167 | + ], |
| 168 | + } |
| 169 | + |
| 170 | + await db.insert(userTableDefinitions).values({ |
| 171 | + id: tableId, |
| 172 | + workspaceId: WORKSPACE_ID, |
| 173 | + name: TABLE_NAME, |
| 174 | + description: 'Stress test table with sample user data', |
| 175 | + schema: tableSchema, |
| 176 | + maxRows: 10000, |
| 177 | + createdBy: userId, |
| 178 | + createdAt: now, |
| 179 | + updatedAt: now, |
| 180 | + }) |
| 181 | + |
| 182 | + console.log(`Created table ${TABLE_NAME} (${tableId})`) |
| 183 | + } |
| 184 | + |
| 185 | + // Generate and insert rows in batches |
| 186 | + const batchSize = 1000 |
| 187 | + const now = new Date() |
| 188 | + |
| 189 | + console.log(`Inserting ${NUM_ROWS} rows in batches of ${batchSize}...`) |
| 190 | + |
| 191 | + for (let i = 0; i < NUM_ROWS; i += batchSize) { |
| 192 | + const batch = [] |
| 193 | + const endIdx = Math.min(i + batchSize, NUM_ROWS) |
| 194 | + |
| 195 | + for (let j = i; j < endIdx; j++) { |
| 196 | + batch.push({ |
| 197 | + id: `row_${crypto.randomUUID().replace(/-/g, '')}`, |
| 198 | + tableId, |
| 199 | + workspaceId: WORKSPACE_ID, |
| 200 | + data: generateUserRow(j), |
| 201 | + createdBy: userId, |
| 202 | + createdAt: now, |
| 203 | + updatedAt: now, |
| 204 | + }) |
| 205 | + } |
| 206 | + |
| 207 | + await db.insert(userTableRows).values(batch) |
| 208 | + console.log(` Inserted rows ${i + 1} to ${endIdx}`) |
| 209 | + } |
| 210 | + |
| 211 | + // Verify final row count |
| 212 | + const finalTable = await db |
| 213 | + .select({ rowCount: userTableDefinitions.rowCount }) |
| 214 | + .from(userTableDefinitions) |
| 215 | + .where(eq(userTableDefinitions.id, tableId)) |
| 216 | + .limit(1) |
| 217 | + |
| 218 | + console.log(`\nDone! Table ${TABLE_NAME} now has ${finalTable[0]?.rowCount ?? 0} rows.`) |
| 219 | + |
| 220 | + process.exit(0) |
| 221 | +} |
| 222 | + |
| 223 | +main().catch((err) => { |
| 224 | + console.error('Error seeding data:', err) |
| 225 | + process.exit(1) |
| 226 | +}) |
0 commit comments