From 981458338d3638a6f45347559d6f086d9298489f Mon Sep 17 00:00:00 2001 From: zy1p Date: Mon, 29 Sep 2025 22:18:51 -0400 Subject: [PATCH] fix(cli): timestamp in schema for Drizzle with SQLite (#4622) --- packages/cli/src/generators/drizzle.ts | 4 +- .../auth-schema-sqlite-number-id.txt | 36 +++++++++--------- .../auth-schema-sqlite-passkey-number-id.txt | 38 +++++++++---------- .../auth-schema-sqlite-passkey.txt | 38 +++++++++---------- .../test/__snapshots__/auth-schema-sqlite.txt | 36 +++++++++--------- 5 files changed, 76 insertions(+), 76 deletions(-) diff --git a/packages/cli/src/generators/drizzle.ts b/packages/cli/src/generators/drizzle.ts index 428a1028..1c965d81 100644 --- a/packages/cli/src/generators/drizzle.ts +++ b/packages/cli/src/generators/drizzle.ts @@ -104,7 +104,7 @@ export const generateDrizzleSchema: SchemaGenerator = async ({ : `int('${name}')`, }, date: { - sqlite: `integer('${name}', { mode: 'timestamp' })`, + sqlite: `integer('${name}', { mode: 'timestamp_ms' })`, pg: `timestamp('${name}')`, mysql: `timestamp('${name}')`, }, @@ -170,7 +170,7 @@ export const generateDrizzleSchema: SchemaGenerator = async ({ attr.defaultValue.toString().includes("new Date()") ) { if (databaseType === "sqlite") { - type += `.default(sql\`(current_timestamp)\`)`; + type += `.default(sql\`(cast(unixepoch('subsecond') * 1000 as integer))\`)`; } else { type += `.defaultNow()`; } diff --git a/packages/cli/test/__snapshots__/auth-schema-sqlite-number-id.txt b/packages/cli/test/__snapshots__/auth-schema-sqlite-number-id.txt index 49e1d905..81570a04 100644 --- a/packages/cli/test/__snapshots__/auth-schema-sqlite-number-id.txt +++ b/packages/cli/test/__snapshots__/auth-schema-sqlite-number-id.txt @@ -9,11 +9,11 @@ export const custom_user = sqliteTable("custom_user", { .default(false) .notNull(), image: text("image"), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), twoFactorEnabled: integer("two_factor_enabled", { mode: "boolean" }).default( @@ -25,12 +25,12 @@ export const custom_user = sqliteTable("custom_user", { export const custom_session = sqliteTable("custom_session", { id: int("id").primaryKey(), - expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), + expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(), token: text("token").notNull().unique(), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), ipAddress: text("ip_address"), @@ -51,17 +51,17 @@ export const custom_account = sqliteTable("custom_account", { refreshToken: text("refresh_token"), idToken: text("id_token"), accessTokenExpiresAt: integer("access_token_expires_at", { - mode: "timestamp", + mode: "timestamp_ms", }), refreshTokenExpiresAt: integer("refresh_token_expires_at", { - mode: "timestamp", + mode: "timestamp_ms", }), scope: text("scope"), password: text("password"), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); @@ -70,12 +70,12 @@ export const custom_verification = sqliteTable("custom_verification", { id: int("id").primaryKey(), identifier: text("identifier").notNull(), value: text("value").notNull(), - expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(), + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); diff --git a/packages/cli/test/__snapshots__/auth-schema-sqlite-passkey-number-id.txt b/packages/cli/test/__snapshots__/auth-schema-sqlite-passkey-number-id.txt index edbebcf5..5d5d20be 100644 --- a/packages/cli/test/__snapshots__/auth-schema-sqlite-passkey-number-id.txt +++ b/packages/cli/test/__snapshots__/auth-schema-sqlite-passkey-number-id.txt @@ -9,23 +9,23 @@ export const custom_user = sqliteTable("custom_user", { .default(false) .notNull(), image: text("image"), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); export const custom_session = sqliteTable("custom_session", { id: int("id").primaryKey(), - expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), + expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(), token: text("token").notNull().unique(), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), ipAddress: text("ip_address"), @@ -46,17 +46,17 @@ export const custom_account = sqliteTable("custom_account", { refreshToken: text("refresh_token"), idToken: text("id_token"), accessTokenExpiresAt: integer("access_token_expires_at", { - mode: "timestamp", + mode: "timestamp_ms", }), refreshTokenExpiresAt: integer("refresh_token_expires_at", { - mode: "timestamp", + mode: "timestamp_ms", }), scope: text("scope"), password: text("password"), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); @@ -65,12 +65,12 @@ export const custom_verification = sqliteTable("custom_verification", { id: int("id").primaryKey(), identifier: text("identifier").notNull(), value: text("value").notNull(), - expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(), + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); @@ -87,6 +87,6 @@ export const passkey = sqliteTable("passkey", { deviceType: text("device_type").notNull(), backedUp: integer("backed_up", { mode: "boolean" }).notNull(), transports: text("transports"), - createdAt: integer("created_at", { mode: "timestamp" }), + createdAt: integer("created_at", { mode: "timestamp_ms" }), aaguid: text("aaguid"), }); diff --git a/packages/cli/test/__snapshots__/auth-schema-sqlite-passkey.txt b/packages/cli/test/__snapshots__/auth-schema-sqlite-passkey.txt index 779f13de..e3e1e01b 100644 --- a/packages/cli/test/__snapshots__/auth-schema-sqlite-passkey.txt +++ b/packages/cli/test/__snapshots__/auth-schema-sqlite-passkey.txt @@ -9,23 +9,23 @@ export const custom_user = sqliteTable("custom_user", { .default(false) .notNull(), image: text("image"), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); export const custom_session = sqliteTable("custom_session", { id: text("id").primaryKey(), - expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), + expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(), token: text("token").notNull().unique(), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), ipAddress: text("ip_address"), @@ -46,17 +46,17 @@ export const custom_account = sqliteTable("custom_account", { refreshToken: text("refresh_token"), idToken: text("id_token"), accessTokenExpiresAt: integer("access_token_expires_at", { - mode: "timestamp", + mode: "timestamp_ms", }), refreshTokenExpiresAt: integer("refresh_token_expires_at", { - mode: "timestamp", + mode: "timestamp_ms", }), scope: text("scope"), password: text("password"), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); @@ -65,12 +65,12 @@ export const custom_verification = sqliteTable("custom_verification", { id: text("id").primaryKey(), identifier: text("identifier").notNull(), value: text("value").notNull(), - expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(), + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); @@ -87,6 +87,6 @@ export const passkey = sqliteTable("passkey", { deviceType: text("device_type").notNull(), backedUp: integer("backed_up", { mode: "boolean" }).notNull(), transports: text("transports"), - createdAt: integer("created_at", { mode: "timestamp" }), + createdAt: integer("created_at", { mode: "timestamp_ms" }), aaguid: text("aaguid"), }); diff --git a/packages/cli/test/__snapshots__/auth-schema-sqlite.txt b/packages/cli/test/__snapshots__/auth-schema-sqlite.txt index 4b3b7bdc..2e5ecdf6 100644 --- a/packages/cli/test/__snapshots__/auth-schema-sqlite.txt +++ b/packages/cli/test/__snapshots__/auth-schema-sqlite.txt @@ -9,11 +9,11 @@ export const custom_user = sqliteTable("custom_user", { .default(false) .notNull(), image: text("image"), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), twoFactorEnabled: integer("two_factor_enabled", { mode: "boolean" }).default( @@ -25,12 +25,12 @@ export const custom_user = sqliteTable("custom_user", { export const custom_session = sqliteTable("custom_session", { id: text("id").primaryKey(), - expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), + expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(), token: text("token").notNull().unique(), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), ipAddress: text("ip_address"), @@ -51,17 +51,17 @@ export const custom_account = sqliteTable("custom_account", { refreshToken: text("refresh_token"), idToken: text("id_token"), accessTokenExpiresAt: integer("access_token_expires_at", { - mode: "timestamp", + mode: "timestamp_ms", }), refreshTokenExpiresAt: integer("refresh_token_expires_at", { - mode: "timestamp", + mode: "timestamp_ms", }), scope: text("scope"), password: text("password"), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }); @@ -70,12 +70,12 @@ export const custom_verification = sqliteTable("custom_verification", { id: text("id").primaryKey(), identifier: text("identifier").notNull(), value: text("value").notNull(), - expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), - createdAt: integer("created_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + expiresAt: integer("expires_at", { mode: "timestamp_ms" }).notNull(), + createdAt: integer("created_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .notNull(), - updatedAt: integer("updated_at", { mode: "timestamp" }) - .default(sql`(current_timestamp)`) + updatedAt: integer("updated_at", { mode: "timestamp_ms" }) + .default(sql`(cast(unixepoch('subsecond') * 1000 as integer))`) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), });