-
-
Notifications
You must be signed in to change notification settings - Fork 4
Sl/feat/db optimization #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,14 +12,29 @@ dotenv.config(); | |||||||||||||||||||||||||||||||||||||||
| type AppDatabase = PgDatabase<PgQueryResultHKT, typeof schema>; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const APP_ENV = process.env.APP_ENV?.trim().toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (!APP_ENV) { | ||||||||||||||||||||||||||||||||||||||||
| throw new Error( | ||||||||||||||||||||||||||||||||||||||||
| '[db] APP_ENV is required. Set APP_ENV=local in .env for development, or APP_ENV=develop/production for deployment' | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const IS_LOCAL_ENV = APP_ENV === 'local'; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
14
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate APP_ENV against an allowlist, not only presence. A typoed value still passes and is treated as non-local. That undermines the fail-fast safety goal. ✅ Suggested fix const APP_ENV = process.env.APP_ENV?.trim().toLowerCase();
+const ALLOWED_APP_ENVS = new Set(['local', 'develop', 'production']);
-if (!APP_ENV) {
+if (!APP_ENV || !ALLOWED_APP_ENVS.has(APP_ENV)) {
throw new Error(
- '[db] APP_ENV is required. Set APP_ENV=local in .env for development, or APP_ENV=develop/production for deployment'
+ '[db] APP_ENV must be one of: local, develop, production'
);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
| if (process.env.NODE_ENV !== 'test') { | ||||||||||||||||||||||||||||||||||||||||
| console.log('[db] runtime env check', { | ||||||||||||||||||||||||||||||||||||||||
| has_DATABASE_URL: Boolean(process.env.DATABASE_URL?.trim()), | ||||||||||||||||||||||||||||||||||||||||
| has_DATABASE_URL_LOCAL: Boolean(process.env.DATABASE_URL_LOCAL?.trim()), | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const STRICT_LOCAL_DB_GUARD = process.env.SHOP_STRICT_LOCAL_DB === '1'; | ||||||||||||||||||||||||||||||||||||||||
| const REQUIRED_LOCAL_DB_URL = process.env.SHOP_REQUIRED_DATABASE_URL_LOCAL; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (STRICT_LOCAL_DB_GUARD) { | ||||||||||||||||||||||||||||||||||||||||
| if (!IS_LOCAL_ENV) { | ||||||||||||||||||||||||||||||||||||||||
| throw new Error( | ||||||||||||||||||||||||||||||||||||||||
| `[db] SHOP_STRICT_LOCAL_DB=1 requires APP_ENV=local (got "${APP_ENV ?? 'undefined'}")` | ||||||||||||||||||||||||||||||||||||||||
| `[db] SHOP_STRICT_LOCAL_DB=1 requires APP_ENV=local (got "${APP_ENV}")` | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
@@ -68,7 +83,7 @@ if (IS_LOCAL_ENV) { | |||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (!url) { | ||||||||||||||||||||||||||||||||||||||||
| throw new Error( | ||||||||||||||||||||||||||||||||||||||||
| `[db] APP_ENV=${APP_ENV ?? 'undefined'} requires DATABASE_URL to be set` | ||||||||||||||||||||||||||||||||||||||||
| `[db] APP_ENV=${APP_ENV} requires DATABASE_URL to be set` | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,13 +20,17 @@ export async function getAdminCategoryList(): Promise<AdminCategoryItem[]> { | |||||||||||||||||||||
| title: categoryTranslations.title, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| .from(categories) | ||||||||||||||||||||||
| .innerJoin( | ||||||||||||||||||||||
| .leftJoin( | ||||||||||||||||||||||
| categoryTranslations, | ||||||||||||||||||||||
| sql`${categoryTranslations.categoryId} = ${categories.id} AND ${categoryTranslations.locale} = ${ADMIN_LOCALE}` | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| .orderBy(categories.displayOrder); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return rows; | ||||||||||||||||||||||
| return rows.map(row => ({ | ||||||||||||||||||||||
| id: row.id, | ||||||||||||||||||||||
| slug: row.slug, | ||||||||||||||||||||||
| title: row.title ?? row.slug, | ||||||||||||||||||||||
| })); | ||||||||||||||||||||||
|
Comment on lines
+29
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a non-empty fallback for category title. Line 32 uses 💡 Suggested fix return rows.map(row => ({
id: row.id,
slug: row.slug,
- title: row.title ?? row.slug,
+ title: row.title?.trim() || row.slug,
}));📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export async function getMaxQuizDisplayOrder( | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 50383
🏁 Script executed:
# Check the actual db/index.ts to confirm import-time behavior head -30 frontend/db/index.tsRepository: DevLoversTeam/devlovers.net
Length of output: 1002
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 301
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 240
🏁 Script executed:
# Check what vitest.setup.ts does cat frontend/vitest.setup.tsRepository: DevLoversTeam/devlovers.net
Length of output: 113
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1786
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 141
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1017
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 92
🏁 Script executed:
# Check one of the test files that imports db to see the exact import sequence head -50 frontend/lib/tests/shop/admin-orders-query-filters.test.tsRepository: DevLoversTeam/devlovers.net
Length of output: 1414
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 219
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 607
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 94
🏁 Script executed:
# Check if there's a setup that pre-loads environment before tests cat frontend/vitest.config.tsRepository: DevLoversTeam/devlovers.net
Length of output: 903
🏁 Script executed:
# Check the full content of lib/tests/shop/setup.ts to see if APP_ENV is set at module level cat frontend/lib/tests/shop/setup.tsRepository: DevLoversTeam/devlovers.net
Length of output: 1393
🏁 Script executed:
# Also check vitest.shop.config.ts in full cat frontend/vitest.shop.config.tsRepository: DevLoversTeam/devlovers.net
Length of output: 603
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 100
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 946
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 124
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 100
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 139
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1139
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 252
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 105
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 91
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 113
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 74
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 74
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 2713
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 212
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 360
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 100
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 468
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 228
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1533
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 53
Import-time APP_ENV hard-fail breaks test bootstrap for shop tests.
frontend/db/index.ts:16-20throws during module evaluation ifAPP_ENVis undefined. Shop test files (149 of 163) importdbat the top level, triggering this check beforebeforeAllhooks inlib/tests/shop/setup.ts:38-42can set required environment variables. Since Vitest executessetupFilesbefore importing test modules, the validation hooks never run.Consider deferring the APP_ENV check to lazy initialization (e.g., on first db operation) or setting APP_ENV in
vitest.setup.tsat module level before any test files are loaded.🤖 Prompt for AI Agents