Eddy Vinck
Eddy Vinck
DTDrizzle Team
Created by Eddy Vinck on 6/13/2024 in #help
How to do an UPDATE with a JOIN in Drizzle ORM?
Thanks
5 replies
DTDrizzle Team
Created by Eddy Vinck on 6/13/2024 in #help
How to do an UPDATE with a JOIN in Drizzle ORM?
Here is what I'm trying to do as a subquery
const payingUserIds = ctx.db
.select({
id: users.id,
})
.from(users)
.where(inArray(users.subscriptionId, paidPlans))
.getSQL();

const update = await ctx.db
.update(userUsages)
.set({
lastResetAt: sql`CURRENT_TIMESTAMP`,
audioRecordingsMetered: 0,
})
.where(
and(
inArray(userUsages.userId, payingUserIds),
sql`${userUsages.lastResetAt} <= (CURRENT_DATE - INTERVAL '1 month')`,
),
);
const payingUserIds = ctx.db
.select({
id: users.id,
})
.from(users)
.where(inArray(users.subscriptionId, paidPlans))
.getSQL();

const update = await ctx.db
.update(userUsages)
.set({
lastResetAt: sql`CURRENT_TIMESTAMP`,
audioRecordingsMetered: 0,
})
.where(
and(
inArray(userUsages.userId, payingUserIds),
sql`${userUsages.lastResetAt} <= (CURRENT_DATE - INTERVAL '1 month')`,
),
);
5 replies
DTDrizzle Team
Created by Eddy Vinck on 6/13/2024 in #help
How to do an UPDATE with a JOIN in Drizzle ORM?
Schema:
export const users = pgTable("users", {
id: uuid("id").defaultRandom().primaryKey().notNull(),
email: varchar("email", { length: 256 }),
stripeId: varchar("stripe_id", { length: 256 }),
subscriptionId: varchar("subscription_id", { length: 256 }).default("unsubscribed"),
paymentStatus: varchar("payment_status", { length: 256 }).default("none"),
hasUsedTrial: boolean("has_used_trial").default(false),
});
export type User = InferSelectModel<typeof users>;

export const userUsages = pgTable("user_usage", {
id: uuid("id").defaultRandom().primaryKey().notNull(),
userId: uuid("user_id"),
lastResetAt: timestamp("last_reset_at", { mode: "string" })
.defaultNow()
.notNull(),
audioRecordingsMetered: integer("audio_recordings_metered"),
audioRecordingsTotal: integer("audio_recordings_total"),
});

export const usersRelations = relations(users, ({ one }) => ({
userUsage: one(userUsages, {
fields: [users.id],
references: [userUsages.userId],
}),
plan: one(plans, {
fields: [users.subscriptionId],
references: [plans.stripeLookupKey],
}),
}));
export const users = pgTable("users", {
id: uuid("id").defaultRandom().primaryKey().notNull(),
email: varchar("email", { length: 256 }),
stripeId: varchar("stripe_id", { length: 256 }),
subscriptionId: varchar("subscription_id", { length: 256 }).default("unsubscribed"),
paymentStatus: varchar("payment_status", { length: 256 }).default("none"),
hasUsedTrial: boolean("has_used_trial").default(false),
});
export type User = InferSelectModel<typeof users>;

export const userUsages = pgTable("user_usage", {
id: uuid("id").defaultRandom().primaryKey().notNull(),
userId: uuid("user_id"),
lastResetAt: timestamp("last_reset_at", { mode: "string" })
.defaultNow()
.notNull(),
audioRecordingsMetered: integer("audio_recordings_metered"),
audioRecordingsTotal: integer("audio_recordings_total"),
});

export const usersRelations = relations(users, ({ one }) => ({
userUsage: one(userUsages, {
fields: [users.id],
references: [userUsages.userId],
}),
plan: one(plans, {
fields: [users.subscriptionId],
references: [plans.stripeLookupKey],
}),
}));
5 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
Thanks for all the help here Andrew! Appreciate it ❤️
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
Love that
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
Nice, I’m on my phone now but I’ll check it later when I’m on my computer
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
Maybe with more familiarity about managing migrations like this I wouldn’t be asking this stuff here 😂
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
FWIW I am not great at database management but I love using Drizzle. I’m comfortable with queries, joins all that etc (in raw SQL) but the whole migration management is pretty new to me.
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
I was thinking about writing an article too 👍
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
So only schema modifications in the migrations
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
I see
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
No issues no I suppose, thanks for clearing these things up for me
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
So in the future would there be a way to add a migration file manually for all the custom work I did in pg admin?
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
Yeah I did
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
I’ll be creating a new production database soon when I create that environment, so not an issue I think then
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
Agree lol
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
Yeah no worries
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
No description
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
[✓] 6 tables fetched
[✓] 72 columns fetched
[✓] 0 enums fetched
[✓] 1 indexes fetched
[✓] 0 foreign keys fetched

[i] No SQL generated, you already have migrations in project
[✓] You schema file is ready ➜ drizzle/generated/schema.ts 🚀
[✓] 6 tables fetched
[✓] 72 columns fetched
[✓] 0 enums fetched
[✓] 1 indexes fetched
[✓] 0 foreign keys fetched

[i] No SQL generated, you already have migrations in project
[✓] You schema file is ready ➜ drizzle/generated/schema.ts 🚀
50 replies
DTDrizzle Team
Created by Eddy Vinck on 4/18/2024 in #help
Drizzle schema vs SQL when migrating serial id to uuid?
Hey, I just did my migration in SQL and ran the introspection. Just wanted to let you know that the introspect command did not generate a commented SQL migration file. It did of course generate the schema in TypeScript. (in the generated folder) Maybe I misunderstood, but should it be generating an SQL file? I'm on drizzle-kit 0.20.14
50 replies