Skip to content

Commit 252e449

Browse files
committed
Merge branch 'alternation-engine' of https://github.com/drizzle-team/drizzle-orm into alternation-engine
2 parents eb962a0 + 9e74e8c commit 252e449

File tree

13 files changed

+135
-112
lines changed

13 files changed

+135
-112
lines changed

drizzle-kit/src/cli/commands/push-postgres.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { ddlDiff } from '../../dialects/postgres/diff';
2121
import { fromDrizzleSchema, prepareFromSchemaFiles } from '../../dialects/postgres/drizzle';
2222
import type { JsonStatement } from '../../dialects/postgres/statements';
2323
import type { DB } from '../../utils';
24-
import { mockResolver } from '../../utils/mocks';
2524
import { prepareFilenames } from '../../utils/utils-node';
2625
import { resolver } from '../prompts';
2726
import { Select } from '../selector-ui';
@@ -37,18 +36,36 @@ export const handle = async (
3736
strict: boolean,
3837
credentials: PostgresCredentials,
3938
tablesFilter: string[],
40-
schemasFilter: string[],
39+
allowedSchemas: string[],
4140
entities: Entities,
4241
force: boolean,
4342
casing: CasingType | undefined,
4443
) => {
4544
const { preparePostgresDB } = await import('../connections');
46-
const { introspect: pgPushIntrospect } = await import('./pull-postgres');
45+
const { introspect } = await import('./pull-postgres');
4746

4847
const db = await preparePostgresDB(credentials);
4948
const filenames = prepareFilenames(schemaPath);
5049
const res = await prepareFromSchemaFiles(filenames);
5150

51+
console.log(allowedSchemas);
52+
if (allowedSchemas.length > 0) {
53+
const toCheck = res.schemas.map((it) => it.schemaName).filter((it) => it !== 'public');
54+
const missing = toCheck.filter((it) => !allowedSchemas.includes(it));
55+
if (missing.length > 0) {
56+
const missingArr = missing.map((it) => chalk.underline(it)).join(', ');
57+
const allowedArr = allowedSchemas.map((it) => chalk.underline(it)).join(', ');
58+
render(
59+
`[${chalk.red('x')}] ${missingArr} schemas missing in drizzle config file "schemaFilter": [${allowedArr}]`,
60+
);
61+
// TODO: write a guide and link here
62+
process.exit(1);
63+
}
64+
} else {
65+
allowedSchemas.push(...res.schemas.map((it) => it.schemaName));
66+
}
67+
console.log('.', allowedSchemas);
68+
5269
const { schema: schemaTo, errors, warnings } = fromDrizzleSchema(res, casing);
5370

5471
if (warnings.length > 0) {
@@ -61,11 +78,11 @@ export const handle = async (
6178
}
6279

6380
const progress = new ProgressView('Pulling schema from database...', 'Pulling schema from database...');
64-
const { schema: schemaFrom } = await pgPushIntrospect(db, tablesFilter, schemasFilter, entities, progress);
81+
const { schema: schemaFrom } = await introspect(db, tablesFilter, allowedSchemas, entities, progress);
6582

6683
const { ddl: ddl1, errors: errors1 } = interimToDDL(schemaFrom);
6784
const { ddl: ddl2, errors: errors2 } = interimToDDL(schemaTo);
68-
// todo: handle errors?
85+
// TODO: handle errors?
6986

7087
if (errors1.length > 0) {
7188
console.log(errors.map((it) => postgresSchemaError(it)).join('\n'));

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,12 @@ export const fromDrizzleSchema = (
365365
const { dimensions, sqlType, typeSchema, baseColumn } = unwrapColumn(column);
366366

367367
const columnDefault = defaultFromColumn(baseColumn, column.default, dimensions, dialect);
368-
const isPartOfPk = drizzlePKs.find((it) => it.columns.map((it) => it.name).includes(column.name));
368+
369+
const isPk = column.primary
370+
|| config.primaryKeys.find((pk) =>
371+
pk.columns.some((col) => col.name ? col.name === column.name : col.keyAsName === column.keyAsName)
372+
) !== undefined;
373+
369374
return {
370375
entityType: 'columns',
371376
schema: schema,
@@ -376,7 +381,7 @@ export const fromDrizzleSchema = (
376381
dimensions: dimensions,
377382
pk: column.primary,
378383
pkName: null,
379-
notNull: notNull || Boolean(isPartOfPk),
384+
notNull: notNull || isPk,
380385
default: columnDefault,
381386
generated: generatedValue,
382387
unique: column.isUnique,

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,13 @@ export const fromDrizzleSchema = (
268268
};
269269

270270
res.schemas = schema.schemas
271+
.filter((it) => {
272+
return !it.isExisting && it.schemaName !== 'public';
273+
})
271274
.map<Schema>((it) => ({
272275
entityType: 'schemas',
273276
name: it.schemaName,
274-
}))
275-
.filter((it) => {
276-
if (schemaFilter) {
277-
return schemaFilter.includes(it.name) && it.name !== 'public';
278-
} else {
279-
return it.name !== 'public';
280-
}
281-
});
277+
}));
282278

283279
const tableConfigPairs = schema.tables.map((it) => {
284280
return { config: getTableConfig(it), table: it };

drizzle-kit/tests/cockroach/defaults-without-tx.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ test.concurrent('char + char arrays', async ({ db }) => {
77
// char is less than default
88
const res10 = await diffDefault(db, char({ length: 2 }).default('text'), `'text'`, { expectError: true });
99

10-
expect.soft(res1_0).toStrictEqual([`Insert default failed`]);
11-
expect.soft(res10).toStrictEqual([`Insert default failed`]);
10+
expect(res1_0).toStrictEqual([`Insert default failed`]);
11+
expect(res10).toStrictEqual([`Insert default failed`]);
1212
});
1313

1414
test.concurrent('varchar + varchar arrays', async ({ db }) => {
1515
// varchar length is less than default
1616
const res10 = await diffDefault(db, varchar({ length: 2 }).default('text'), `'text'`, { expectError: true });
1717

18-
expect.soft(res10).toStrictEqual([`Insert default failed`]);
18+
expect(res10).toStrictEqual([`Insert default failed`]);
1919
});
2020

2121
test.concurrent('string + string arrays', async ({ db }) => {
2222
// varchar length is less than default
2323
const res10 = await diffDefault(db, string({ length: 2 }).default('text'), `'text'`, { expectError: true });
2424

25-
expect.soft(res10).toStrictEqual([`Insert default failed`]);
25+
expect(res10).toStrictEqual([`Insert default failed`]);
2626
});

drizzle-kit/tests/cockroach/generated.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { cockroachTable, int4, text } from 'drizzle-orm/cockroach-core';
33
import { expect } from 'vitest';
44
import { diff, push, test } from './mocks';
55

6-
test('generated as callback: add column with generated constraint', async ({ db }) => {
6+
test.concurrent('generated as callback: add column with generated constraint', async ({ db }) => {
77
const from = {
88
users: cockroachTable('users', {
99
id: int4('id'),
@@ -37,7 +37,7 @@ test('generated as callback: add column with generated constraint', async ({ db
3737
expect(pst).toStrictEqual(st0);
3838
});
3939

40-
test('generated as callback: add generated constraint to an exisiting column', async ({ db }) => {
40+
test.concurrent('generated as callback: add generated constraint to an exisiting column', async ({ db }) => {
4141
const from = {
4242
users: cockroachTable('users', {
4343
id: int4('id'),
@@ -73,7 +73,7 @@ test('generated as callback: add generated constraint to an exisiting column', a
7373
expect(pst).toStrictEqual(st0);
7474
});
7575

76-
test('generated as callback: drop generated constraint', async ({ db }) => {
76+
test.concurrent('generated as callback: drop generated constraint', async ({ db }) => {
7777
const from = {
7878
users: cockroachTable('users', {
7979
id: int4('id'),
@@ -109,7 +109,7 @@ test('generated as callback: drop generated constraint', async ({ db }) => {
109109
expect(pst).toStrictEqual(st0);
110110
});
111111

112-
test('generated as callback: change generated constraint', async ({ db }) => {
112+
test.concurrent('generated as callback: change generated constraint', async ({ db }) => {
113113
const from = {
114114
users: cockroachTable('users', {
115115
id: int4('id'),
@@ -144,7 +144,7 @@ test('generated as callback: change generated constraint', async ({ db }) => {
144144
expect(pst).toStrictEqual([]); // we don't trigger generated column recreate if definition change within push
145145
});
146146

147-
test('generated as sql: add column with generated constraint', async ({ db }) => {
147+
test.concurrent('generated as sql: add column with generated constraint', async ({ db }) => {
148148
const from = {
149149
users: cockroachTable('users', {
150150
id: int4('id'),
@@ -178,7 +178,7 @@ test('generated as sql: add column with generated constraint', async ({ db }) =>
178178
expect(pst).toStrictEqual(st0);
179179
});
180180

181-
test('generated as sql: add generated constraint to an exisiting column', async ({ db }) => {
181+
test.concurrent('generated as sql: add generated constraint to an exisiting column', async ({ db }) => {
182182
const from = {
183183
users: cockroachTable('users', {
184184
id: int4('id'),
@@ -214,7 +214,7 @@ test('generated as sql: add generated constraint to an exisiting column', async
214214
expect(pst).toStrictEqual(st0);
215215
});
216216

217-
test('generated as sql: drop generated constraint', async ({ db }) => {
217+
test.concurrent('generated as sql: drop generated constraint', async ({ db }) => {
218218
const from = {
219219
users: cockroachTable('users', {
220220
id: int4('id'),
@@ -250,7 +250,7 @@ test('generated as sql: drop generated constraint', async ({ db }) => {
250250
expect(pst).toStrictEqual(st0);
251251
});
252252

253-
test('generated as sql: change generated constraint', async ({ db }) => {
253+
test.concurrent('generated as sql: change generated constraint', async ({ db }) => {
254254
const from = {
255255
users: cockroachTable('users', {
256256
id: int4('id'),
@@ -288,7 +288,7 @@ test('generated as sql: change generated constraint', async ({ db }) => {
288288
expect(pst).toStrictEqual([]); // we don't trigger generated column recreate if definition change within push
289289
});
290290

291-
test('generated as string: add column with generated constraint', async ({ db }) => {
291+
test.concurrent('generated as string: add column with generated constraint', async ({ db }) => {
292292
const from = {
293293
users: cockroachTable('users', {
294294
id: int4('id'),
@@ -322,7 +322,7 @@ test('generated as string: add column with generated constraint', async ({ db })
322322
expect(pst).toStrictEqual(st0);
323323
});
324324

325-
test('generated as string: add generated constraint to an exisiting column', async ({ db }) => {
325+
test.concurrent('generated as string: add generated constraint to an exisiting column', async ({ db }) => {
326326
const from = {
327327
users: cockroachTable('users', {
328328
id: int4('id'),
@@ -358,7 +358,7 @@ test('generated as string: add generated constraint to an exisiting column', asy
358358
expect(pst).toStrictEqual(st0);
359359
});
360360

361-
test('generated as string: drop generated constraint', async ({ db }) => {
361+
test.concurrent('generated as string: drop generated constraint', async ({ db }) => {
362362
const from = {
363363
users: cockroachTable('users', {
364364
id: int4('id'),
@@ -394,7 +394,7 @@ test('generated as string: drop generated constraint', async ({ db }) => {
394394
expect(pst).toStrictEqual(st0);
395395
});
396396

397-
test('generated as string: change generated constraint', async ({ db }) => {
397+
test.concurrent('generated as string: change generated constraint', async ({ db }) => {
398398
const from = {
399399
users: cockroachTable('users', {
400400
id: int4('id'),
@@ -432,7 +432,7 @@ test('generated as string: change generated constraint', async ({ db }) => {
432432
expect(pst).toStrictEqual([]); // we don't trigger generated column recreate if definition change within push
433433
});
434434

435-
test('alter generated constraint', async ({ db }) => {
435+
test.concurrent('alter generated constraint', async ({ db }) => {
436436
const schema1 = {
437437
users: cockroachTable('users', {
438438
id: int4('id'),

drizzle-kit/tests/cockroach/indexes-without-tx.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { cockroachTable, index, int4, vector } from 'drizzle-orm/cockroach-core'
22
import { expect } from 'vitest';
33
import { diff, push, test } from './mocks';
44

5-
test.concurrent('vector index', async ({ dbc: db }) => {
5+
test('vector index', async ({ db }) => {
66
const schema1 = {
77
users: cockroachTable('users', {
88
id: int4('id').primaryKey(),

drizzle-kit/tests/cockroach/mocks.ts

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -616,45 +616,41 @@ export const prepareTestDatabase = async (): Promise<TestDatabaseKit> => {
616616
await prepareClient(url, 'dbc4', true),
617617
];
618618

619-
const closureTxs = () => {
620-
return async () => {
621-
while (true) {
622-
const c = clientsTxs.shift();
623-
if (!c) {
624-
await sleep(50);
625-
continue;
626-
}
627-
return {
628-
db: c,
629-
release: () => {
630-
clientsTxs.push(c);
631-
},
632-
};
619+
const closureTxs = async () => {
620+
while (true) {
621+
const c = clientsTxs.shift();
622+
if (!c) {
623+
await sleep(50);
624+
continue;
633625
}
634-
};
626+
return {
627+
db: c,
628+
release: () => {
629+
clientsTxs.push(c);
630+
},
631+
};
632+
}
635633
};
636634

637-
const closure = () => {
638-
return async () => {
639-
while (true) {
640-
const c = clients.shift();
641-
if (!c) {
642-
await sleep(50);
643-
continue;
644-
}
645-
return {
646-
db: c,
647-
release: () => {
648-
clients.push(c);
649-
},
650-
};
635+
const closure = async () => {
636+
while (true) {
637+
const c = clients.shift();
638+
if (!c) {
639+
await sleep(50);
640+
continue;
651641
}
652-
};
642+
return {
643+
db: c,
644+
release: () => {
645+
clients.push(c);
646+
},
647+
};
648+
}
653649
};
654650

655651
return {
656-
acquire: closure(),
657-
acquireTx: closureTxs(),
652+
acquire: closure,
653+
acquireTx: closureTxs,
658654
close: async () => {
659655
for (const c of clients) {
660656
c.close();
@@ -680,12 +676,9 @@ export const test = base.extend<{ kit: TestDatabaseKit; db: TestDatabase; dbc: T
680676
db: [
681677
async ({ kit }, use) => {
682678
const { db, release } = await kit.acquire();
683-
try {
684-
await use(db);
685-
} finally {
686-
await db.clear();
687-
release();
688-
}
679+
await use(db);
680+
await db.clear();
681+
release();
689682
},
690683
{ scope: 'test' },
691684
],
@@ -694,12 +687,9 @@ export const test = base.extend<{ kit: TestDatabaseKit; db: TestDatabase; dbc: T
694687
dbc: [
695688
async ({ kit }, use) => {
696689
const { db, release } = await kit.acquireTx();
697-
try {
698-
await use(db);
699-
} finally {
700-
await db.clear();
701-
release();
702-
}
690+
await use(db);
691+
await db.clear();
692+
release();
703693
},
704694
{ scope: 'test' },
705695
],

0 commit comments

Comments
 (0)