Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 4 additions & 3 deletions .kilo/command/migrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
- To create a new migration follow these steps:
1. First alter the schema in `packages/db/src/schema.ts` as appropriate
2. Then run `pnpm drizzle generate` which will create a new migration file with a descriptive name
3. Read the generated migration file in `packages/db/src/migrations/`
3. Read the generated timestamped migration folder in `packages/db/src/migrations/`
4. Improve the migration to ensure it is as NON-DESTRUCTIVE as possible and that Drizzle didn't change more than necessary
5. If needed, you can run `pnpm drizzle migrate` to apply migrations to the database
6. When you're done, run `pnpm format` to autoformat
5. Run `pnpm drizzle check` to verify migration history consistency
6. If needed, you can run `pnpm drizzle migrate` to apply migrations to the database
7. When you're done, run `pnpm format` to autoformat
- Prefer `timestamp({ withTimezone: true })` over regular timestamp columns for better timezone handling.
2 changes: 1 addition & 1 deletion apps/web/src/app/admin/api/users/add-credit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function POST(
return warnAndCreateErrorResponse('Invalid email parameter');
}

const user = await db.query.kilocode_users.findFirst({
const user = await db._query.kilocode_users.findFirst({
where: eq(kilocode_users.google_user_email, email),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function GET(

if (!kilo_user_id) throw new Error('kilo_user_id is required');

const transactions = await db.query.credit_transactions.findMany({
const transactions = await db._query.credit_transactions.findMany({
where: and(
eq(credit_transactions.kilo_user_id, kilo_user_id),
isNull(credit_transactions.organization_id)
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/admin/api/users/payment-methods/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function GET(

if (!kilo_user_id) throw new Error('kilo_user_id is required');

const methods = await db.query.payment_methods.findMany({
const methods = await db._query.payment_methods.findMany({
where: eq(payment_methods.user_id, kilo_user_id),
orderBy: desc(payment_methods.created_at),
});
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/admin/api/users/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export async function GET(
const orderFunction = sortOrder === 'asc' ? asc : desc;
const orderCondition = orderFunction(kilocode_users[sortField]);

const users = await db.query.kilocode_users.findMany({
const users = await db._query.kilocode_users.findMany({
where: whereCondition,
orderBy: orderCondition,
limit: limit,
Expand All @@ -196,7 +196,7 @@ export async function GET(
const notes =
userIds.length <= 0
? []
: await db.query.user_admin_notes.findMany({
: await db._query.user_admin_notes.findMany({
where: inArray(user_admin_notes.kilo_user_id, userIds),
orderBy: desc(user_admin_notes.created_at),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function AdminOrganizationWebhooksPage({
const { id } = await params;
const organizationId = decodeURIComponent(id);

const organization = await db.query.organizations.findFirst({
const organization = await db._query.organizations.findFirst({
columns: {
id: true,
name: true,
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/app/admin/users/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function getUserData(userId: string): Promise<UserDetailProps | null> {
return null;
}

const notes = await db.query.user_admin_notes.findMany({
const notes = await db._query.user_admin_notes.findMany({
where: eq(user_admin_notes.kilo_user_id, userId),
orderBy: desc(user_admin_notes.created_at),
});
Expand All @@ -36,7 +36,7 @@ async function getUserData(userId: string): Promise<UserDetailProps | null> {

const admins =
adminIds.length > 0
? await db.query.kilocode_users.findMany({
? await db._query.kilocode_users.findMany({
where: inArray(kilocode_users.id, adminIds),
limit: 50,
})
Expand Down Expand Up @@ -68,7 +68,7 @@ async function getUserData(userId: string): Promise<UserDetailProps | null> {

// Fetch auto-top-up config
const autoTopUpConfig =
(await db.query.auto_top_up_configs.findFirst({
(await db._query.auto_top_up_configs.findFirst({
where: eq(auto_top_up_configs.owned_by_user_id, userId),
})) ?? null;

Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/admin/users/[id]/webhooks/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function AdminUserWebhooksPage({
const { id } = await params;
const userId = decodeURIComponent(id);

const user = await db.query.kilocode_users.findFirst({
const user = await db._query.kilocode_users.findFirst({
columns: {
id: true,
google_user_email: true,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/api/cron/deployment-threat-scan/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function GET(request: Request) {

// Get deployments pending scan, oldest first (by last_deployed_at)
// Uses partial index idx_deployments_threat_status_pending for efficient queries
const pendingDeployments = await db.query.deployments.findMany({
const pendingDeployments = await db._query.deployments.findMany({
where: eq(deployments.threat_status, 'pending_scan'),
orderBy: asc(deployments.last_deployed_at),
limit: 25,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/api/dev/create-kilocode-org/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function POST(_request: NextRequest): Promise<NextResponse> {
console.log('[DEV CREATE KILOCODE ORG] Stripe customer ID created');

// Verify membership was created
const newMembership = await db.query.organization_memberships.findFirst({
const newMembership = await db._query.organization_memberships.findFirst({
where: and(
eq(organization_memberships.organization_id, DEV_ORG_ID),
eq(organization_memberships.kilo_user_id, user.id)
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/api/up/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function GET(): Promise<
recentAIActivity: false,
};
try {
const db_usage_shows_recent_llm_usage = await db.query.microdollar_usage.findFirst({
const db_usage_shows_recent_llm_usage = await db._query.microdollar_usage.findFirst({
columns: { id: true },
where: and(
gte(microdollar_usage.created_at, sql`NOW() - INTERVAL '5 minutes'`),
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/api/user-deployments/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async function resolveRecipientEmails(deployment: {
}

async function sendDeploymentFailureNotification(deploymentId: string) {
const deployment = await db.query.deployments.findFirst({
const deployment = await db._query.deployments.findFirst({
where: eq(deployments.id, deploymentId),
});

Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/payments/topup/success/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function fetchCreditTransactionIdForStripeSession(sessionId: string
}

// Note: This is a bit ugly, because for some reason we sometimes store py_ ids (charges), and sometimes pi_ ids (payment intents)
const creditTransaction = await db.query.credit_transactions.findFirst({
const creditTransaction = await db._query.credit_transactions.findFirst({
where: inArray(credit_transactions.stripe_payment_id, [
paymentIntent.id,
paymentIntent.latest_charge as string,
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/lib/admin-utils-serverside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export const describePaymentMethods = (
};

export async function getPaymentStatusByUserIds(userIds: string[]) {
const paymentMethodsData = await db.query.payment_methods.findMany({
const paymentMethodsData = await db._query.payment_methods.findMany({
where: inArray(payment_methods.user_id, userIds),
});

return Object.groupBy(paymentMethodsData, pm => pm.user_id);
}

export async function hasPaymentMethod(userId: string): Promise<boolean> {
const result = await db.query.payment_methods.findFirst({
const result = await db._query.payment_methods.findFirst({
columns: { id: true },
where: and(eq(payment_methods.user_id, userId), isNull(payment_methods.deleted_at)),
});
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/lib/affiliate-attribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function recordAffiliateAttribution(params: {
}

export async function getAffiliateAttribution(userId: string, provider: AffiliateProvider) {
return await db.query.user_affiliate_attributions.findFirst({
return await db._query.user_affiliate_attributions.findFirst({
where: and(
eq(user_affiliate_attributions.user_id, userId),
eq(user_affiliate_attributions.provider, provider)
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/lib/affiliate-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ async function getEventByDedupeKey(
database: DatabaseClient,
dedupeKey: string
): Promise<AffiliateEventRow> {
const event = await database.query.user_affiliate_events.findFirst({
const event = await database._query.user_affiliate_events.findFirst({
where: eq(user_affiliate_events.dedupe_key, dedupeKey),
});

Expand Down Expand Up @@ -700,7 +700,7 @@ async function getAffiliateEventById(
database: DatabaseClient,
eventId: string
): Promise<AffiliateEventRow | null> {
const event = await database.query.user_affiliate_events.findFirst({
const event = await database._query.user_affiliate_events.findFirst({
where: eq(user_affiliate_events.id, eventId),
});

Expand Down Expand Up @@ -1022,7 +1022,7 @@ async function persistPendingSaleReversal(
async function reconcilePendingSaleReversals(
database: DatabaseClient
): Promise<{ materialized: number }> {
const pendingRows = await database.query.pending_impact_sale_reversals.findMany();
const pendingRows = await database._query.pending_impact_sale_reversals.findMany();
let materialized = 0;

for (const pending of pendingRows) {
Expand Down
16 changes: 8 additions & 8 deletions apps/web/src/lib/ai-gateway/processUsage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ describe('logMicrodollarUsage', () => {
const updatedUser = await findUserById('test-log-user-1');
expect(updatedUser?.microdollars_used).toBe(1500); // 1000 + 500

const metadataRecord = await db.query.microdollar_usage_metadata.findFirst({
const metadataRecord = await db._query.microdollar_usage_metadata.findFirst({
where: eq(microdollar_usage_metadata.message_id, 'test-msg-123'),
});
expect(metadataRecord).toBeTruthy();

const usageRecord = await db.query.microdollar_usage.findFirst({
const usageRecord = await db._query.microdollar_usage.findFirst({
where: eq(microdollar_usage.id, metadataRecord!.id),
});
expect(usageRecord).toBeTruthy();
Expand Down Expand Up @@ -424,7 +424,7 @@ describe('logMicrodollarUsage', () => {

await logMicrodollarUsage(usageStats, usageContext);

const metadataRecord = await db.query.microdollar_usage_metadata.findFirst({
const metadataRecord = await db._query.microdollar_usage_metadata.findFirst({
where: eq(microdollar_usage_metadata.message_id, 'test-msg-session'),
});
expect(metadataRecord).toBeTruthy();
Expand Down Expand Up @@ -464,12 +464,12 @@ describe('logMicrodollarUsage', () => {
const updatedUser = await findUserById('test-log-user-2');
expect(updatedUser?.microdollars_used).toBe(2000); // unchanged

const metadataRecord = await db.query.microdollar_usage_metadata.findFirst({
const metadataRecord = await db._query.microdollar_usage_metadata.findFirst({
where: eq(microdollar_usage_metadata.message_id, 'test-msg-456'),
});
expect(metadataRecord).toBeTruthy();

const usageRecord = await db.query.microdollar_usage.findFirst({
const usageRecord = await db._query.microdollar_usage.findFirst({
where: eq(microdollar_usage.id, metadataRecord!.id),
});
expect(usageRecord).toBeTruthy();
Expand Down Expand Up @@ -554,7 +554,7 @@ describe('logMicrodollarUsage', () => {
expect(updatedUser?.microdollars_used).toBe(4200); // 3000 + 300 + 400 + 500

// Verify all 3 usage records exist
const usageRecords = await db.query.microdollar_usage.findMany({
const usageRecords = await db._query.microdollar_usage.findMany({
where: eq(microdollar_usage.kilo_user_id, 'test-dedup-user'),
});
expect(usageRecords).toHaveLength(3);
Expand Down Expand Up @@ -626,13 +626,13 @@ describe('logMicrodollarUsage', () => {

await logMicrodollarUsage(usageStats, usageContext);

const metadataRecord = await db.query.microdollar_usage_metadata.findFirst({
const metadataRecord = await db._query.microdollar_usage_metadata.findFirst({
where: eq(microdollar_usage_metadata.message_id, 'test-org-msg-123'),
});

expect(metadataRecord).toBeTruthy();

const usageRecord = await db.query.microdollar_usage.findFirst({
const usageRecord = await db._query.microdollar_usage.findFirst({
where: eq(microdollar_usage.id, metadataRecord!.id),
});

Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/lib/ai-gateway/processUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ async function isFirstUsage(
): Promise<boolean> {
if (prior_microdollar_usage || usage.organization_id) return false;
//perf: we only pay the costs for querying prior microdollar usage for non-org users that have incurred zero cost so far.
return !(await db.query.microdollar_usage.findFirst({
return !(await db._query.microdollar_usage.findFirst({
where: eq(microdollar_usage.kilo_user_id, usage.kilo_user_id),
columns: { created_at: true },
}));
Expand Down
Loading
Loading