Skip to content

Commit b64770b

Browse files
[cockroach]: fixed tests in kit
1 parent c286807 commit b64770b

File tree

11 files changed

+51
-25
lines changed

11 files changed

+51
-25
lines changed

drizzle-kit/src/dialects/cockroach/convertor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ const recreateEnumConvertor = convertor('recreate_enum', (st) => {
556556
for (const column of columns) {
557557
const key = column.schema !== 'public' ? `"${column.schema}"."${column.table}"` : `"${column.table}"`;
558558
statements.push(`ALTER TABLE ${key} ALTER COLUMN "${column.name}" SET DATA TYPE text;`);
559-
if (column.default) statements.push(`ALTER TABLE ${key} ALTER COLUMN "${column.name}" DROP DEFAULT;`);
559+
if (column.default.left) statements.push(`ALTER TABLE ${key} ALTER COLUMN "${column.name}" DROP DEFAULT;`);
560560
}
561561
statements.push(dropEnumConvertor.convert({ enum: to }) as string);
562562
statements.push(createEnumConvertor.convert({ enum: to }) as string);
@@ -568,8 +568,15 @@ const recreateEnumConvertor = convertor('recreate_enum', (st) => {
568568
statements.push(
569569
`ALTER TABLE ${key} ALTER COLUMN "${column.name}" SET DATA TYPE ${enumType} USING "${column.name}"::${enumType};`,
570570
);
571-
if (column.default) {
572-
statements.push(`ALTER TABLE ${key} ALTER COLUMN "${column.name}" SET DEFAULT ${defaultToSQL(column)};`);
571+
if (column.default.right) {
572+
statements.push(`ALTER TABLE ${key} ALTER COLUMN "${column.name}" SET DEFAULT ${
573+
defaultToSQL({
574+
default: column.default.right,
575+
dimensions: column.dimensions,
576+
type: column.type,
577+
typeSchema: column.typeSchema,
578+
})
579+
};`);
573580
}
574581
}
575582

drizzle-kit/src/dialects/cockroach/diff.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,12 @@ export const ddlDiff = async (
892892
// recreate enum
893893
const columns = ddl1.columns.list({ typeSchema: alter.schema, type: alter.name }).map((it) => {
894894
const c2 = ddl2.columns.one({ schema: it.schema, table: it.table, name: it.name })!;
895-
it.default = c2.default;
896-
return it;
895+
896+
const def = {
897+
right: c2.default,
898+
left: it.default,
899+
};
900+
return { ...it, default: def };
897901
});
898902
recreateEnums.push(prepareStatement('recreate_enum', { to: e.$right, columns, from: e.$left }));
899903
} else {

drizzle-kit/src/dialects/cockroach/statements.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ export interface JsonRenameEnum {
5959
export interface JsonRecreateEnum {
6060
type: 'recreate_enum';
6161
to: Enum;
62-
columns: Column[];
62+
columns: (Omit<Column, 'default'> & {
63+
default: {
64+
left: Column['default'];
65+
right: Column['default'];
66+
};
67+
})[];
6368
from: Enum;
6469
}
6570

drizzle-kit/src/dialects/cockroach/typescript.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ const createTableColumns = (
553553
if (fks) {
554554
const fksStatement = fks
555555
.map((it) => {
556-
const onDelete = it.onDelete && it.onDelete !== 'NO ACTION' ? it.onDelete : null;
557-
const onUpdate = it.onUpdate && it.onUpdate !== 'NO ACTION' ? it.onUpdate : null;
556+
const onDelete = it.onDelete && it.onDelete !== 'NO ACTION' ? it.onDelete.toLowerCase() : null;
557+
const onUpdate = it.onUpdate && it.onUpdate !== 'NO ACTION' ? it.onUpdate.toLowerCase() : null;
558558
const params = { onDelete, onUpdate };
559559

560560
const typeSuffix = isCyclic(it) ? ': AnyCockroachColumn' : '';
@@ -698,8 +698,8 @@ const createTableFKs = (fks: ForeignKey[], schemas: Record<string, string>, casi
698698
statement += it.nameExplicit ? `\t\tname: "${it.name}"\n` : '';
699699
statement += `\t})`;
700700

701-
statement += it.onUpdate && it.onUpdate !== 'NO ACTION' ? `.onUpdate("${it.onUpdate}")` : '';
702-
statement += it.onDelete && it.onDelete !== 'NO ACTION' ? `.onDelete("${it.onDelete}")` : '';
701+
statement += it.onUpdate && it.onUpdate !== 'NO ACTION' ? `.onUpdate("${it.onUpdate.toLowerCase()}")` : '';
702+
statement += it.onDelete && it.onDelete !== 'NO ACTION' ? `.onDelete("${it.onDelete.toLowerCase()}")` : '';
703703
statement += `,\n`;
704704
});
705705
return statement;

drizzle-kit/src/dialects/postgres/typescript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,8 @@ const createTableFKs = (fks: ForeignKey[], schemas: Record<string, string>, casi
10341034
statement += it.nameExplicit ? `\t\tname: "${it.name}"\n` : '';
10351035
statement += `\t})`;
10361036

1037-
statement += it.onUpdate && it.onUpdate !== 'NO ACTION' ? `.onUpdate("${it.onUpdate}")` : '';
1038-
statement += it.onDelete && it.onDelete !== 'NO ACTION' ? `.onDelete("${it.onDelete}")` : '';
1037+
statement += it.onUpdate && it.onUpdate !== 'NO ACTION' ? `.onUpdate("${it.onUpdate.toLowerCase()}")` : '';
1038+
statement += it.onDelete && it.onDelete !== 'NO ACTION' ? `.onDelete("${it.onDelete.toLowerCase()}")` : '';
10391039
statement += `,\n`;
10401040
});
10411041
return statement;

drizzle-kit/src/dialects/sqlite/convertor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ const createTable = convertor('create_table', (st) => {
9191
onUpdate,
9292
} = referenceData[i];
9393

94-
const onDeleteStatement = onDelete !== 'no action' ? ` ON DELETE ${onDelete}` : '';
95-
const onUpdateStatement = onUpdate !== 'no action' ? ` ON UPDATE ${onUpdate}` : '';
94+
const onDeleteStatement = (onDelete && onDelete !== 'NO ACTION') ? ` ON DELETE ${onDelete}` : '';
95+
const onUpdateStatement = (onUpdate && onUpdate !== 'NO ACTION') ? ` ON UPDATE ${onUpdate}` : '';
9696
const fromColumnsString = columns.map((it) => `\`${it}\``).join(',');
9797
const toColumnsString = columnsTo.map((it) => `\`${it}\``).join(',');
9898

drizzle-kit/src/dialects/sqlite/drizzle.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getTableName, is, SQL } from 'drizzle-orm';
22
import { Relations } from 'drizzle-orm/_relations';
3-
import type { AnySQLiteColumn, AnySQLiteTable } from 'drizzle-orm/sqlite-core';
3+
import type { AnySQLiteColumn, AnySQLiteTable, UpdateDeleteAction } from 'drizzle-orm/sqlite-core';
44
import {
55
getTableConfig,
66
getViewConfig,
@@ -10,6 +10,7 @@ import {
1010
SQLiteTimestamp,
1111
SQLiteView,
1212
} from 'drizzle-orm/sqlite-core';
13+
import { assertUnreachable } from 'src/utils';
1314
import { safeRegister } from 'src/utils/utils-node';
1415
import type { CasingType } from '../../cli/validations/common';
1516
import { getColumnCasing, sqlToStr } from '../drizzle';
@@ -27,6 +28,16 @@ import type {
2728
} from './ddl';
2829
import { Int, nameForForeignKey, nameForPk, nameForUnique, typeFor } from './grammar';
2930

31+
export const transformOnUpdateDelete = (on: UpdateDeleteAction): ForeignKey['onUpdate'] => {
32+
if (on === 'no action') return 'NO ACTION';
33+
if (on === 'cascade') return 'CASCADE';
34+
if (on === 'restrict') return 'RESTRICT';
35+
if (on === 'set default') return 'SET DEFAULT';
36+
if (on === 'set null') return 'SET NULL';
37+
38+
assertUnreachable(on);
39+
};
40+
3041
export const fromDrizzleSchema = (
3142
dTables: AnySQLiteTable[],
3243
dViews: SQLiteView[],
@@ -110,8 +121,8 @@ export const fromDrizzleSchema = (
110121
const fks = tableConfigs.map((it) => {
111122
return it.config.foreignKeys.map((fk) => {
112123
const tableFrom = it.config.name;
113-
const onDelete = fk.onDelete ?? 'no action';
114-
const onUpdate = fk.onUpdate ?? 'no action';
124+
const onDelete = fk.onDelete;
125+
const onUpdate = fk.onUpdate;
115126
const reference = fk.reference();
116127

117128
const referenceFT = reference.foreignTable;
@@ -131,8 +142,8 @@ export const fromDrizzleSchema = (
131142
tableTo,
132143
columns: columnsFrom,
133144
columnsTo,
134-
onDelete,
135-
onUpdate,
145+
onDelete: transformOnUpdateDelete(onDelete ?? 'no action'),
146+
onUpdate: transformOnUpdateDelete(onUpdate ?? 'no action'),
136147
nameExplicit: fk.isNameExplicit(),
137148
} satisfies ForeignKey;
138149
});

drizzle-kit/src/dialects/sqlite/introspect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ export const fromDatabase = async (
535535
columns: columnsFrom,
536536
columnsTo,
537537
nameExplicit: true,
538-
onDelete: fk.onDelete?.toLowerCase() ?? 'no action',
539-
onUpdate: fk.onUpdate?.toLowerCase() ?? 'no action',
538+
onDelete: fk.onDelete ?? 'NO ACTION',
539+
onUpdate: fk.onUpdate ?? 'NO ACTION',
540540
});
541541
}
542542

drizzle-kit/src/dialects/sqlite/typescript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ const createTableColumns = (
284284
withCasing(fk.columnsTo[0], casing)
285285
}`;
286286

287-
const onDelete = (fk.onDelete && fk.onDelete !== 'no action') ? fk.onDelete : null;
288-
const onUpdate = (fk.onUpdate && fk.onUpdate !== 'no action') ? fk.onUpdate : null;
287+
const onDelete = (fk.onDelete && fk.onDelete !== 'NO ACTION') ? fk.onDelete.toLowerCase() : null;
288+
const onUpdate = (fk.onUpdate && fk.onUpdate !== 'NO ACTION') ? fk.onUpdate.toLowerCase() : null;
289289
const params = { onDelete, onUpdate };
290290

291291
const paramsStr = objToStatement2(params);

drizzle-kit/tests/cockroach/entity-filter.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { sql } from 'drizzle-orm';
22
import { cockroachSchema, cockroachView, int4 as int } from 'drizzle-orm/cockroach-core';
33
import { afterAll, beforeAll, beforeEach, expect } from 'vitest';
44
import { push, test } from './mocks';
5-
3;
65

76
test('push schema #1', async ({ db }) => {
87
const to = { dev: cockroachSchema('dev') };

0 commit comments

Comments
 (0)