Skip to content

Commit 9de7a00

Browse files
improvement(code-structure): move db into separate package (#1364)
* improvement(code-structure): move db into separate package * make db separate package * remake bun lock * update imports to not maintain two separate ones * fix CI for tests by adding dummy url * vercel build fix attempt * update bun lock * regenerate bun lock * fix mocks * remove db commands from apps/sim package json
1 parent 325a666 commit 9de7a00

File tree

400 files changed

+1054
-966
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

400 files changed

+1054
-966
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ jobs:
3232
env:
3333
NODE_OPTIONS: '--no-warnings'
3434
NEXT_PUBLIC_APP_URL: 'https://www.sim.ai'
35+
DATABASE_URL: 'postgresql://postgres:postgres@localhost:5432/simstudio'
3536
ENCRYPTION_KEY: '7cf672e460e430c1fba707575c2b0e2ad5a99dddf9b7b7e3b5646e630861db1c' # dummy key for CI only
3637
run: bun run test
3738

3839
- name: Build application
3940
env:
4041
NODE_OPTIONS: '--no-warnings'
4142
NEXT_PUBLIC_APP_URL: 'https://www.sim.ai'
43+
DATABASE_URL: 'postgresql://postgres:postgres@localhost:5432/simstudio'
4244
STRIPE_SECRET_KEY: 'dummy_key_for_ci_only'
4345
STRIPE_WEBHOOK_SECRET: 'dummy_secret_for_ci_only'
4446
RESEND_API_KEY: 'dummy_key_for_ci_only'
@@ -71,7 +73,7 @@ jobs:
7173
run: bun install
7274

7375
- name: Apply migrations
74-
working-directory: ./apps/sim
76+
working-directory: ./packages/db
7577
env:
7678
DATABASE_URL: ${{ github.ref == 'refs/heads/main' && secrets.DATABASE_URL || secrets.STAGING_DATABASE_URL }}
77-
run: bunx drizzle-kit migrate
79+
run: bunx drizzle-kit migrate --config=./drizzle.config.ts

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ Update your `.env` file with the database URL:
125125
DATABASE_URL="postgresql://postgres:your_password@localhost:5432/simstudio"
126126
```
127127

128-
4. Set up the database:
128+
4. Set up the database (from packages/db):
129129

130130
```bash
131-
bunx drizzle-kit migrate
131+
cd packages/db
132+
bunx drizzle-kit migrate --config=./drizzle.config.ts
132133
```
133134

134135
5. Start the development servers:

apps/sim/app/api/__test-utils__/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ export function mockExecutionDependencies() {
349349
})),
350350
}))
351351

352-
vi.mock('@/db', () => ({
352+
vi.mock('@sim/db', () => ({
353353
db: mockDb,
354354
}))
355355
}
@@ -395,7 +395,7 @@ export async function getMockedDependencies() {
395395
const workflowUtilsModule = await import('@/lib/workflows/utils')
396396
const executorModule = await import('@/executor')
397397
const serializerModule = await import('@/serializer')
398-
const dbModule = await import('@/db')
398+
const dbModule = await import('@sim/db')
399399

400400
return {
401401
decryptSecret: utilsModule.decryptSecret,
@@ -428,7 +428,7 @@ export function mockScheduleStatusDb({
428428
schedule?: any[]
429429
workflow?: any[]
430430
} = {}) {
431-
vi.doMock('@/db', () => {
431+
vi.doMock('@sim/db', () => {
432432
let callCount = 0
433433

434434
const select = vi.fn().mockImplementation(() => ({
@@ -469,7 +469,7 @@ export function mockScheduleExecuteDb({
469469
workflowRecord?: any
470470
envRecord?: any
471471
}): void {
472-
vi.doMock('@/db', () => {
472+
vi.doMock('@sim/db', () => {
473473
const select = vi.fn().mockImplementation(() => ({
474474
from: vi.fn().mockImplementation((table: any) => {
475475
const tbl = String(table)
@@ -544,7 +544,7 @@ export function mockAuth(user: MockUser = mockUser): MockAuthResult {
544544
* Mock common schema patterns
545545
*/
546546
export function mockCommonSchemas() {
547-
vi.doMock('@/db/schema', () => ({
547+
vi.doMock('@sim/db/schema', () => ({
548548
workflowFolder: {
549549
id: 'id',
550550
userId: 'userId',
@@ -597,7 +597,7 @@ export function mockDrizzleOrm() {
597597
* Mock knowledge-related database schemas
598598
*/
599599
export function mockKnowledgeSchemas() {
600-
vi.doMock('@/db/schema', () => ({
600+
vi.doMock('@sim/db/schema', () => ({
601601
knowledgeBase: {
602602
id: 'kb_id',
603603
userId: 'user_id',
@@ -1091,7 +1091,7 @@ export function createMockDatabase(options: MockDatabaseOptions = {}) {
10911091
transaction: createTransactionMock(),
10921092
}
10931093

1094-
vi.doMock('@/db', () => ({ db: mockDb }))
1094+
vi.doMock('@sim/db', () => ({ db: mockDb }))
10951095

10961096
return {
10971097
mockDb,

apps/sim/app/api/auth/oauth/connections/route.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ describe('OAuth Connections API Route', () => {
3434
getSession: mockGetSession,
3535
}))
3636

37-
vi.doMock('@/db', () => ({
37+
vi.doMock('@sim/db', () => ({
3838
db: mockDb,
39-
}))
40-
41-
vi.doMock('@/db/schema', () => ({
4239
account: { userId: 'userId', providerId: 'providerId' },
4340
user: { email: 'email', id: 'id' },
41+
eq: vi.fn((field, value) => ({ field, value, type: 'eq' })),
4442
}))
4543

4644
vi.doMock('drizzle-orm', () => ({

apps/sim/app/api/auth/oauth/connections/route.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import { account, db, user } from '@sim/db'
12
import { eq } from 'drizzle-orm'
23
import { jwtDecode } from 'jwt-decode'
34
import { type NextRequest, NextResponse } from 'next/server'
45
import { getSession } from '@/lib/auth'
56
import { createLogger } from '@/lib/logs/console/logger'
67
import { generateRequestId } from '@/lib/utils'
7-
import { db } from '@/db'
8-
import { account, user } from '@/db/schema'
98

109
const logger = createLogger('OAuthConnectionsAPI')
1110

apps/sim/app/api/auth/oauth/credentials/route.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ describe('OAuth Credentials API Route', () => {
4545
parseProvider: mockParseProvider,
4646
}))
4747

48-
vi.doMock('@/db', () => ({
48+
vi.doMock('@sim/db', () => ({
4949
db: mockDb,
5050
}))
5151

52-
vi.doMock('@/db/schema', () => ({
52+
vi.doMock('@sim/db/schema', () => ({
5353
account: { userId: 'userId', providerId: 'providerId' },
5454
user: { email: 'email', id: 'id' },
5555
}))

apps/sim/app/api/auth/oauth/credentials/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { db } from '@sim/db'
2+
import { account, user, workflow } from '@sim/db/schema'
13
import { and, eq } from 'drizzle-orm'
24
import { jwtDecode } from 'jwt-decode'
35
import { type NextRequest, NextResponse } from 'next/server'
@@ -7,8 +9,6 @@ import type { OAuthService } from '@/lib/oauth/oauth'
79
import { parseProvider } from '@/lib/oauth/oauth'
810
import { getUserEntityPermissions } from '@/lib/permissions/utils'
911
import { generateRequestId } from '@/lib/utils'
10-
import { db } from '@/db'
11-
import { account, user, workflow } from '@/db/schema'
1212

1313
export const dynamic = 'force-dynamic'
1414

apps/sim/app/api/auth/oauth/disconnect/route.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ describe('OAuth Disconnect API Route', () => {
3232
getSession: mockGetSession,
3333
}))
3434

35-
vi.doMock('@/db', () => ({
35+
vi.doMock('@sim/db', () => ({
3636
db: mockDb,
3737
}))
3838

39-
vi.doMock('@/db/schema', () => ({
39+
vi.doMock('@sim/db/schema', () => ({
4040
account: { userId: 'userId', providerId: 'providerId' },
4141
}))
4242

apps/sim/app/api/auth/oauth/disconnect/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { db } from '@sim/db'
2+
import { account } from '@sim/db/schema'
13
import { and, eq, like, or } from 'drizzle-orm'
24
import { type NextRequest, NextResponse } from 'next/server'
35
import { getSession } from '@/lib/auth'
46
import { createLogger } from '@/lib/logs/console/logger'
57
import { generateRequestId } from '@/lib/utils'
6-
import { db } from '@/db'
7-
import { account } from '@/db/schema'
88

99
export const dynamic = 'force-dynamic'
1010

apps/sim/app/api/auth/oauth/microsoft/file/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { db } from '@sim/db'
2+
import { account } from '@sim/db/schema'
13
import { eq } from 'drizzle-orm'
24
import { type NextRequest, NextResponse } from 'next/server'
35
import { getSession } from '@/lib/auth'
46
import { createLogger } from '@/lib/logs/console/logger'
57
import { generateRequestId } from '@/lib/utils'
68
import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
7-
import { db } from '@/db'
8-
import { account } from '@/db/schema'
99

1010
export const dynamic = 'force-dynamic'
1111

0 commit comments

Comments
 (0)