Skip to content

Commit

Permalink
fix: unify drizlze schema and fix sqlite timestamps (#1931)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge authored Jul 4, 2024
1 parent 7d8f10f commit a1a4b87
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-meals-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": patch
---

fix: unify drizlze schema and fix sqlite timestamps
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const posts = createTable(
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt").onUpdateNow(),
updatedAt: timestamp("updated_at").onUpdateNow(),
},
(example) => ({
nameIndex: index("name_idx").on(example.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const posts = createTable(
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt").onUpdateNow(),
updatedAt: timestamp("updated_at").onUpdateNow(),
},
(example) => ({
nameIndex: index("name_idx").on(example.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export const posts = createTable(
createdAt: timestamp("created_at", { withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt", { withTimezone: true }),
updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate(
() => new Date()
),
},
(example) => ({
nameIndex: index("name_idx").on(example.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export const posts = createTable(
id: int("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
name: text("name", { length: 256 }),
createdAt: int("created_at", { mode: "timestamp" })
.default(sql`CURRENT_TIMESTAMP`)
.default(sql`(unixepoch())`)
.notNull(),
updatedAt: int("updatedAt", { mode: "timestamp" }),
updatedAt: int("updated_at", { mode: "timestamp" }).$onUpdate(
() => new Date()
),
},
(example) => ({
nameIndex: index("name_idx").on(example.name),
Expand Down
24 changes: 13 additions & 11 deletions cli/template/extras/src/server/db/schema-drizzle/with-auth-mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ export const posts = createTable(
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }),
createdById: varchar("createdById", { length: 255 })
createdById: varchar("created_by", { length: 255 })
.notNull()
.references(() => users.id),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt").onUpdateNow(),
updatedAt: timestamp("updated_at").onUpdateNow(),
},
(example) => ({
createdByIdIdx: index("createdById_idx").on(example.createdById),
createdByIdIdx: index("created_by_idx").on(example.createdById),
nameIndex: index("name_idx").on(example.name),
})
);
Expand All @@ -45,7 +45,7 @@ export const users = createTable("user", {
.$defaultFn(() => crypto.randomUUID()),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }).notNull(),
emailVerified: timestamp("emailVerified", {
emailVerified: timestamp("email_verified", {
mode: "date",
fsp: 3,
}).default(sql`CURRENT_TIMESTAMP(3)`),
Expand All @@ -60,14 +60,16 @@ export const usersRelations = relations(users, ({ many }) => ({
export const accounts = createTable(
"account",
{
userId: varchar("userId", { length: 255 })
userId: varchar("user_id", { length: 255 })
.notNull()
.references(() => users.id),
type: varchar("type", { length: 255 })
.$type<AdapterAccount["type"]>()
.notNull(),
provider: varchar("provider", { length: 255 }).notNull(),
providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
providerAccountId: varchar("provider_account_id", {
length: 255,
}).notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: int("expires_at"),
Expand All @@ -80,7 +82,7 @@ export const accounts = createTable(
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
userIdIdx: index("account_userId_idx").on(account.userId),
userIdIdx: index("account_user_id_idx").on(account.userId),
})
);

Expand All @@ -91,16 +93,16 @@ export const accountsRelations = relations(accounts, ({ one }) => ({
export const sessions = createTable(
"session",
{
sessionToken: varchar("sessionToken", { length: 255 })
sessionToken: varchar("session_token", { length: 255 })
.notNull()
.primaryKey(),
userId: varchar("userId", { length: 255 })
userId: varchar("user_id", { length: 255 })
.notNull()
.references(() => users.id),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
(session) => ({
userIdIdx: index("session_userId_idx").on(session.userId),
userIdIdx: index("session_user_id_idx").on(session.userId),
})
);

Expand All @@ -109,7 +111,7 @@ export const sessionsRelations = relations(sessions, ({ one }) => ({
}));

export const verificationTokens = createTable(
"verificationToken",
"verification_token",
{
identifier: varchar("identifier", { length: 255 }).notNull(),
token: varchar("token", { length: 255 }).notNull(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export const posts = createTable(
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }),
createdById: varchar("createdById", { length: 255 }).notNull(),
createdById: varchar("created_by", { length: 255 }).notNull(),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt").onUpdateNow(),
updatedAt: timestamp("updated_at").onUpdateNow(),
},
(example) => ({
createdByIdIdx: index("createdById_idx").on(example.createdById),
createdByIdIdx: index("created_by_idx").on(example.createdById),
nameIndex: index("name_idx").on(example.name),
})
);
Expand All @@ -43,7 +43,7 @@ export const users = createTable("user", {
.$defaultFn(() => crypto.randomUUID()),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }).notNull(),
emailVerified: timestamp("emailVerified", {
emailVerified: timestamp("email_verified", {
mode: "date",
fsp: 3,
}).default(sql`CURRENT_TIMESTAMP(3)`),
Expand All @@ -58,12 +58,14 @@ export const usersRelations = relations(users, ({ many }) => ({
export const accounts = createTable(
"account",
{
userId: varchar("userId", { length: 255 }).notNull(),
userId: varchar("user_id", { length: 255 }).notNull(),
type: varchar("type", { length: 255 })
.$type<AdapterAccount["type"]>()
.notNull(),
provider: varchar("provider", { length: 255 }).notNull(),
providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
providerAccountId: varchar("provider_account_id", {
length: 255,
}).notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: int("expires_at"),
Expand All @@ -76,7 +78,7 @@ export const accounts = createTable(
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
userIdIdx: index("accounts_userId_idx").on(account.userId),
userIdIdx: index("accounts_user_id_idx").on(account.userId),
})
);

Expand All @@ -87,14 +89,14 @@ export const accountsRelations = relations(accounts, ({ one }) => ({
export const sessions = createTable(
"session",
{
sessionToken: varchar("sessionToken", { length: 255 })
sessionToken: varchar("session_token", { length: 255 })
.notNull()
.primaryKey(),
userId: varchar("userId", { length: 255 }).notNull(),
userId: varchar("user_id", { length: 255 }).notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
(session) => ({
userIdIdx: index("session_userId_idx").on(session.userId),
userIdIdx: index("session_user_id_idx").on(session.userId),
})
);

Expand All @@ -103,7 +105,7 @@ export const sessionsRelations = relations(sessions, ({ one }) => ({
}));

export const verificationTokens = createTable(
"verificationToken",
"verification_token",
{
identifier: varchar("identifier", { length: 255 }).notNull(),
token: varchar("token", { length: 255 }).notNull(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ export const posts = createTable(
{
id: serial("id").primaryKey(),
name: varchar("name", { length: 256 }),
createdById: varchar("createdById", { length: 255 })
createdById: varchar("created_by", { length: 255 })
.notNull()
.references(() => users.id),
createdAt: timestamp("created_at", { withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt", { withTimezone: true }),
updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate(
() => new Date()
),
},
(example) => ({
createdByIdIdx: index("createdById_idx").on(example.createdById),
createdByIdIdx: index("created_by_idx").on(example.createdById),
nameIndex: index("name_idx").on(example.name),
})
);
Expand All @@ -45,7 +47,7 @@ export const users = createTable("user", {
.$defaultFn(() => crypto.randomUUID()),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }).notNull(),
emailVerified: timestamp("emailVerified", {
emailVerified: timestamp("email_verified", {
mode: "date",
withTimezone: true,
}).default(sql`CURRENT_TIMESTAMP`),
Expand All @@ -59,14 +61,16 @@ export const usersRelations = relations(users, ({ many }) => ({
export const accounts = createTable(
"account",
{
userId: varchar("userId", { length: 255 })
userId: varchar("user_id", { length: 255 })
.notNull()
.references(() => users.id),
type: varchar("type", { length: 255 })
.$type<AdapterAccount["type"]>()
.notNull(),
provider: varchar("provider", { length: 255 }).notNull(),
providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
providerAccountId: varchar("provider_account_id", {
length: 255,
}).notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: integer("expires_at"),
Expand All @@ -79,7 +83,7 @@ export const accounts = createTable(
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
userIdIdx: index("account_userId_idx").on(account.userId),
userIdIdx: index("account_user_id_idx").on(account.userId),
})
);

Expand All @@ -90,10 +94,10 @@ export const accountsRelations = relations(accounts, ({ one }) => ({
export const sessions = createTable(
"session",
{
sessionToken: varchar("sessionToken", { length: 255 })
sessionToken: varchar("session_token", { length: 255 })
.notNull()
.primaryKey(),
userId: varchar("userId", { length: 255 })
userId: varchar("user_id", { length: 255 })
.notNull()
.references(() => users.id),
expires: timestamp("expires", {
Expand All @@ -102,7 +106,7 @@ export const sessions = createTable(
}).notNull(),
},
(session) => ({
userIdIdx: index("session_userId_idx").on(session.userId),
userIdIdx: index("session_user_id_idx").on(session.userId),
})
);

Expand All @@ -111,7 +115,7 @@ export const sessionsRelations = relations(sessions, ({ one }) => ({
}));

export const verificationTokens = createTable(
"verificationToken",
"verification_token",
{
identifier: varchar("identifier", { length: 255 }).notNull(),
token: varchar("token", { length: 255 }).notNull(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ export const posts = createTable(
{
id: int("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
name: text("name", { length: 256 }),
createdById: text("createdById", { length: 255 })
createdById: text("created_by", { length: 255 })
.notNull()
.references(() => users.id),
createdAt: int("created_at", { mode: "timestamp" })
.default(sql`CURRENT_TIMESTAMP`)
.default(sql`(unixepoch())`)
.notNull(),
updatedAt: int("updatedAt", { mode: "timestamp" }),
updatedAt: int("updatedAt", { mode: "timestamp" }).$onUpdate(
() => new Date()
),
},
(example) => ({
createdByIdIdx: index("createdById_idx").on(example.createdById),
createdByIdIdx: index("created_by_idx").on(example.createdById),
nameIndex: index("name_idx").on(example.name),
})
);
Expand All @@ -42,9 +44,9 @@ export const users = createTable("user", {
.$defaultFn(() => crypto.randomUUID()),
name: text("name", { length: 255 }),
email: text("email", { length: 255 }).notNull(),
emailVerified: int("emailVerified", {
emailVerified: int("email_verified", {
mode: "timestamp",
}).default(sql`CURRENT_TIMESTAMP`),
}).default(sql`(unixepoch())`),
image: text("image", { length: 255 }),
});

Expand All @@ -55,14 +57,14 @@ export const usersRelations = relations(users, ({ many }) => ({
export const accounts = createTable(
"account",
{
userId: text("userId", { length: 255 })
userId: text("user_id", { length: 255 })
.notNull()
.references(() => users.id),
type: text("type", { length: 255 })
.$type<AdapterAccount["type"]>()
.notNull(),
provider: text("provider", { length: 255 }).notNull(),
providerAccountId: text("providerAccountId", { length: 255 }).notNull(),
providerAccountId: text("provider_account_id", { length: 255 }).notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: int("expires_at"),
Expand All @@ -75,7 +77,7 @@ export const accounts = createTable(
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
userIdIdx: index("account_userId_idx").on(account.userId),
userIdIdx: index("account_user_id_idx").on(account.userId),
})
);

Expand All @@ -86,7 +88,7 @@ export const accountsRelations = relations(accounts, ({ one }) => ({
export const sessions = createTable(
"session",
{
sessionToken: text("sessionToken", { length: 255 }).notNull().primaryKey(),
sessionToken: text("session_token", { length: 255 }).notNull().primaryKey(),
userId: text("userId", { length: 255 })
.notNull()
.references(() => users.id),
Expand All @@ -102,7 +104,7 @@ export const sessionsRelations = relations(sessions, ({ one }) => ({
}));

export const verificationTokens = createTable(
"verificationToken",
"verification_token",
{
identifier: text("identifier", { length: 255 }).notNull(),
token: text("token", { length: 255 }).notNull(),
Expand Down

0 comments on commit a1a4b87

Please sign in to comment.