fix(cli): clean up unused pg and mysql import in drizzle schema generator (#3974)

* fix(cli): clean up unused pg import in schema generator

* sql clean up

* clean up
This commit is contained in:
KinfeMichael Tariku
2025-08-13 14:01:25 -07:00
committed by GitHub
parent fab97a064f
commit 68f00a118e
3 changed files with 38 additions and 9 deletions
+37 -1
View File
@@ -211,7 +211,43 @@ function generateImport({
);
imports.push(hasBigint ? (databaseType !== "sqlite" ? "bigint" : "") : "");
imports.push(databaseType !== "sqlite" ? "timestamp, boolean" : "");
imports.push(databaseType === "mysql" ? "int" : "integer");
if (databaseType === "mysql") {
// Only include int for MySQL if actually needed
const hasNonBigintNumber = Object.values(tables).some((table) =>
Object.values(table.fields).some(
(field) =>
(field.type === "number" || field.type === "number[]") &&
!field.bigint,
),
);
const needsInt = !!useNumberId || hasNonBigintNumber;
if (needsInt) {
imports.push("int");
}
} else if (databaseType === "pg") {
// Only include integer for PG if actually needed
const hasNonBigintNumber = Object.values(tables).some((table) =>
Object.values(table.fields).some(
(field) =>
(field.type === "number" || field.type === "number[]") &&
!field.bigint,
),
);
const hasFkToId = Object.values(tables).some((table) =>
Object.values(table.fields).some(
(field) => field.references?.field === "id",
),
);
// handles the references field with useNumberId
const needsInteger =
hasNonBigintNumber ||
(options.advanced?.database?.useNumberId && hasFkToId);
if (needsInteger) {
imports.push("integer");
}
} else {
imports.push("integer");
}
imports.push(useNumberId ? (databaseType === "pg" ? "serial" : "") : "");
return `import { ${imports
@@ -1,10 +1,4 @@
import {
pgTable,
text,
timestamp,
boolean,
integer,
} from "drizzle-orm/pg-core";
import { pgTable, text, timestamp, boolean } from "drizzle-orm/pg-core";
export const user = pgTable("user", {
id: text("id").primaryKey(),
-1
View File
@@ -166,7 +166,6 @@ describe("generate", async () => {
plugins: [twoFactor(), username()],
},
});
console.log(schema.code);
expect(schema.code).toMatchFileSnapshot("./__snapshots__/auth-schema.txt");
});