Skip to content

Commit cdcfd1d

Browse files
authored
feat: implement flag to suppress defaults warnings (#222)
* fix: remove warn logs for defaults * Revert "fix: remove warn logs for defaults" This reverts commit 901b09f. * chore: implement suppress warnings tests * feat: implement `suppressDefaultsWarning` flag
1 parent 641fd4f commit cdcfd1d

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

src/cli/drizzle-kit.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ export const getDefaultConfig = async ({
1212
drizzleKitConfigPath,
1313
tsProject,
1414
debug,
15+
suppressDefaultsWarning,
1516
}: {
1617
drizzleSchemaPath: string | undefined;
1718
drizzleKitConfigPath: string | undefined;
1819
tsProject: Project;
1920
debug?: boolean;
21+
suppressDefaultsWarning?: boolean;
2022
}) => {
2123
const {drizzleSchemaPath: resolvedDrizzleSchemaPath, casing: drizzleCasing} =
2224
await getFullDrizzleSchemaFilePath({
@@ -36,6 +38,7 @@ export const getDefaultConfig = async ({
3638
const zeroSchema = drizzleZeroConfig(drizzleSchema, {
3739
casing: drizzleCasing ?? undefined,
3840
debug: Boolean(debug),
41+
suppressDefaultsWarning: Boolean(suppressDefaultsWarning),
3942
});
4043

4144
ensureSourceFileInProject({

src/cli/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export interface GeneratorOptions {
6464
skipDeclare?: boolean;
6565
enableLegacyMutators?: boolean;
6666
enableLegacyQueries?: boolean;
67+
suppressDefaultsWarning?: boolean;
6768
}
6869

6970
async function main(opts: GeneratorOptions = {}) {
@@ -81,6 +82,7 @@ async function main(opts: GeneratorOptions = {}) {
8182
skipDeclare,
8283
enableLegacyMutators,
8384
enableLegacyQueries,
85+
suppressDefaultsWarning,
8486
} = {...opts};
8587

8688
const resolvedTsConfigPath = tsConfigPath ?? defaultTsConfigFile;
@@ -127,6 +129,7 @@ async function main(opts: GeneratorOptions = {}) {
127129
drizzleKitConfigPath,
128130
tsProject,
129131
debug: Boolean(debug),
132+
suppressDefaultsWarning: Boolean(suppressDefaultsWarning),
130133
});
131134

132135
if (!result?.zeroSchema) {
@@ -216,6 +219,11 @@ function cli() {
216219
'Enable legacy CRUD queries (sets enableLegacyQueries to true)',
217220
false,
218221
)
222+
.option(
223+
'--suppress-defaults-warning',
224+
'Hide warnings for columns with default values',
225+
false,
226+
)
219227
.action(async command => {
220228
console.log(`⚙️ drizzle-zero: Generating zero schema...`);
221229

@@ -233,6 +241,7 @@ function cli() {
233241
skipDeclare: command.skipDeclare,
234242
enableLegacyMutators: command.enableLegacyMutators,
235243
enableLegacyQueries: command.enableLegacyQueries,
244+
suppressDefaultsWarning: command.suppressDefaultsWarning,
236245
});
237246

238247
if (command.output) {

src/relations.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ const drizzleZeroConfig = <
321321
* ```
322322
*/
323323
readonly debug?: boolean;
324+
/**
325+
* Whether to hide warnings for columns with default values.
326+
*
327+
* @example
328+
* ```ts
329+
* { suppressDefaultsWarning: true }
330+
* ```
331+
* @see https://github.com/rocicorp/drizzle-zero/issues/197
332+
*/
333+
readonly suppressDefaultsWarning?: boolean;
324334
},
325335
): Flatten<DrizzleToZeroSchema<TDrizzleSchema, TColumnConfig>> => {
326336
let tables: any[] = [];
@@ -377,6 +387,7 @@ const drizzleZeroConfig = <
377387
tableConfig,
378388
config?.debug,
379389
config?.casing,
390+
config?.suppressDefaultsWarning,
380391
);
381392

382393
tables.push(tableSchema);

src/tables.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ const createZeroTableBuilder = <
214214
* The casing to use for the table name.
215215
*/
216216
casing?: TCasing,
217+
/**
218+
* Whether to hide warnings for columns with default values.
219+
*/
220+
suppressDefaultsWarning?: boolean,
217221
): ZeroTableBuilder<TTableName, TTable, TColumnConfig> => {
218222
const actualTableName = getTableName(table);
219223
const tableColumns = getTableColumns(table);
@@ -310,7 +314,7 @@ const createZeroTableBuilder = <
310314
const hasServerDefault =
311315
column.hasDefault || typeof column.defaultFn !== 'undefined';
312316

313-
if (hasServerDefault) {
317+
if (hasServerDefault && !suppressDefaultsWarning) {
314318
const warningKey = `${actualTableName}.${resolvedColumnName}`;
315319
if (!warnedServerDefaults.has(warningKey)) {
316320
warnedServerDefaults.add(warningKey);

tests/tables.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,59 @@ describe('tables', () => {
19571957
expectTableSchemaDeepEqual(result.build()).toEqual(expected.build());
19581958
});
19591959

1960+
describe('default value warnings', () => {
1961+
test('pg - should warn for columns with database defaults when suppressDefaultsWarning is not set', ({
1962+
expect,
1963+
}) => {
1964+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1965+
1966+
const testTable = pgTable('test_defaults_warn', {
1967+
id: text().primaryKey(),
1968+
name: text().notNull().default('unnamed'),
1969+
});
1970+
1971+
createZeroTableBuilder('test_defaults_warn', testTable, {
1972+
id: true,
1973+
name: true,
1974+
});
1975+
1976+
expect(consoleSpy).toHaveBeenCalledWith(
1977+
expect.stringContaining(
1978+
'⚠️ drizzle-zero: Column test_defaults_warn.name uses a database default',
1979+
),
1980+
);
1981+
1982+
consoleSpy.mockRestore();
1983+
});
1984+
1985+
test('pg - should not warn for columns with database defaults when suppressDefaultsWarning is true', ({
1986+
expect,
1987+
}) => {
1988+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
1989+
1990+
const testTable = pgTable('test_defaults_suppressed', {
1991+
id: text().primaryKey(),
1992+
name: text().notNull().default('unnamed'),
1993+
});
1994+
1995+
createZeroTableBuilder(
1996+
'test_defaults_suppressed',
1997+
testTable,
1998+
{
1999+
id: true,
2000+
name: true,
2001+
},
2002+
undefined, // debug
2003+
undefined, // casing
2004+
true, // suppressDefaultsWarning
2005+
);
2006+
2007+
expect(consoleSpy).not.toHaveBeenCalled();
2008+
2009+
consoleSpy.mockRestore();
2010+
});
2011+
});
2012+
19602013
test('pg - no primary key', ({expect}) => {
19612014
const testTable = pgTable('test', {
19622015
id: text(),

0 commit comments

Comments
 (0)