()
| 116 | } |
| 117 | |
| 118 | async function main() { |
| 119 | console.log(`Seeding ${TABLE_NAME} table for workspace ${WORKSPACE_ID}...`) |
| 120 | |
| 121 | // Get user ID for created_by |
| 122 | const userResult = (await db.execute(`SELECT id FROM "user" LIMIT 1`)) as { id: string }[] |
| 123 | const userId = Array.isArray(userResult) && userResult[0] ? userResult[0].id : 'system' |
| 124 | console.log(`Using user ID: ${userId}`) |
| 125 | |
| 126 | // Check if table already exists |
| 127 | const existingTable = await db |
| 128 | .select() |
| 129 | .from(userTableDefinitions) |
| 130 | .where(eq(userTableDefinitions.workspaceId, WORKSPACE_ID)) |
| 131 | .limit(1) |
| 132 | |
| 133 | let tableId: string |
| 134 | |
| 135 | if (existingTable.length > 0 && existingTable[0].name === TABLE_NAME) { |
| 136 | tableId = existingTable[0].id |
| 137 | console.log(`Table ${TABLE_NAME} already exists (${tableId}), clearing existing rows...`) |
| 138 | |
| 139 | // Delete existing rows |
| 140 | await db.delete(userTableRows).where(eq(userTableRows.tableId, tableId)) |
| 141 | |
| 142 | // Reset row count (trigger will update it as we insert) |
| 143 | await db |
| 144 | .update(userTableDefinitions) |
| 145 | .set({ rowCount: 0, updatedAt: new Date() }) |
| 146 | .where(eq(userTableDefinitions.id, tableId)) |
| 147 | } else { |
| 148 | // Create table |
| 149 | tableId = `tbl_${generateId().replace(/-/g, '')}` |
| 150 | const now = new Date() |
| 151 | |
| 152 | const tableSchema = { |
| 153 | columns: [ |
| 154 | { name: 'name', type: 'string', required: true }, |
| 155 | { name: 'email', type: 'string', required: true, unique: true }, |
| 156 | { name: 'age', type: 'number', required: true }, |
| 157 | { name: 'department', type: 'string', required: true }, |
| 158 | { name: 'salary', type: 'number', required: true }, |
| 159 | { name: 'active', type: 'boolean', required: true }, |
| 160 | { name: 'hire_date', type: 'string', required: true }, |
| 161 | { name: 'country', type: 'string', required: true }, |
| 162 | ], |
| 163 | } |
| 164 | |
| 165 | await db.insert(userTableDefinitions).values({ |
| 166 | id: tableId, |
| 167 | workspaceId: WORKSPACE_ID, |
| 168 | name: TABLE_NAME, |
| 169 | description: 'Stress test table with sample user data', |
| 170 | schema: tableSchema, |
| 171 | maxRows: 10000, |
| 172 | createdBy: userId, |
| 173 | createdAt: now, |
| 174 | updatedAt: now, |
| 175 | }) |
no test coverage detected