Skip to content

Commit 7f424fe

Browse files
committed
Merge branch 'alternation-engine' of https://github.com/drizzle-team/drizzle-orm into alternation-engine
2 parents 510b681 + e7ffaf3 commit 7f424fe

File tree

6 files changed

+1216
-30
lines changed

6 files changed

+1216
-30
lines changed

drizzle-kit/src/dialects/drizzle.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const extractPostgresExisting = (
3434
schema: x.schema ?? 'public',
3535
name: x.name,
3636
}));
37-
3837
const existingMatViews = matViews.map((x) => pgMatViewConfig(x)).filter((x) => x.isExisting).map<Table>((
3938
x,
4039
) => ({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ export const fromDrizzleSchema = (
667667
});
668668

669669
for (const view of combinedViews) {
670-
if (view.isExisting) continue;
670+
if (view.isExisting || !filter({ type: 'table', schema: view.schema ?? 'public', name: view.name })) continue;
671671

672672
const {
673673
name: viewName,

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

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const fromDatabase = async (
146146
defaultsQuery,
147147
]);
148148

149-
const { other } = namespaces.reduce<{ system: Namespace[]; other: Namespace[] }>(
149+
const { other: filteredNamespaces } = namespaces.reduce<{ system: Namespace[]; other: Namespace[] }>(
150150
(acc, it) => {
151151
if (isSystemNamespace(it.name)) {
152152
acc.system.push(it);
@@ -158,7 +158,6 @@ export const fromDatabase = async (
158158
{ system: [], other: [] },
159159
);
160160

161-
const filteredNamespaces = other.filter((it) => filter({ type: 'schema', name: it.name }));
162161
const filteredNamespacesStringForSQL = filteredNamespaces.map((ns) => `'${ns.name}'`).join(',');
163162

164163
schemas.push(...filteredNamespaces.map<Schema>((it) => ({ entityType: 'schemas', name: it.name })));
@@ -209,19 +208,13 @@ export const fromDatabase = async (
209208
: [] as TableListItem[];
210209

211210
const viewsList = tablesList.filter((it) => {
212-
if ((it.kind === 'v' || it.kind === 'm')) {
213-
return filter({ type: 'table', schema: it.schema, name: it.name });
214-
}
215-
return false;
211+
it.schema = trimChar(it.schema, '"'); // when camel case name e.x. mySchema -> it gets wrapped to "mySchema"
212+
return it.kind === 'v' || it.kind === 'm';
216213
});
217214

218215
const filteredTables = tablesList.filter((it) => {
219216
it.schema = trimChar(it.schema, '"'); // when camel case name e.x. mySchema -> it gets wrapped to "mySchema"
220-
221-
if ((it.kind === 'r' || it.kind === 'p')) {
222-
return filter({ type: 'table', schema: it.schema, name: it.name });
223-
}
224-
return false;
217+
return it.kind === 'r' || it.kind === 'p';
225218
});
226219

227220
const filteredTableIds = filteredTables.map((it) => it.oid);
@@ -681,10 +674,7 @@ export const fromDatabase = async (
681674

682675
progressCallback('enums', Object.keys(groupedEnums).length, 'done');
683676

684-
// TODO: drizzle link
685-
const filteredRoles = rolesList.filter((x) => filter({ type: 'role', name: x.rolname }));
686-
687-
for (const dbRole of filteredRoles) {
677+
for (const dbRole of rolesList) {
688678
roles.push({
689679
entityType: 'roles',
690680
name: dbRole.rolname,
@@ -1205,22 +1195,39 @@ export const fromDatabase = async (
12051195
progressCallback('checks', checksCount, 'done');
12061196
progressCallback('views', viewsCount, 'done');
12071197

1198+
const resultSchemas = schemas.filter((x) => filter({ type: 'schema', name: x.name }));
1199+
const resultTables = tables.filter((x) => filter({ type: 'table', schema: x.schema, name: x.name }));
1200+
const resultEnums = enums.filter((x) => resultSchemas.some((s) => s.name === x.schema));
1201+
const resultColumns = columns.filter((x) => resultTables.some((t) => t.schema === x.schema && t.name === x.table));
1202+
const resultIndexes = indexes.filter((x) => resultTables.some((t) => t.schema === x.schema && t.name === x.table));
1203+
const resultPKs = pks.filter((x) => resultTables.some((t) => t.schema === x.schema && t.name === x.table));
1204+
const resultFKs = fks.filter((x) => resultTables.some((t) => t.schema === x.schema && t.name === x.table));
1205+
const resultUniques = uniques.filter((x) => resultTables.some((t) => t.schema === x.schema && t.name === x.table));
1206+
const resultChecks = checks.filter((x) => resultTables.some((t) => t.schema === x.schema && t.name === x.table));
1207+
const resultSequences = sequences.filter((x) => resultSchemas.some((t) => t.name === x.schema));
1208+
// TODO: drizzle link
1209+
const resultRoles = roles.filter((x) => filter({ type: 'role', name: x.name }));
1210+
const resultViews = views.filter((x) => filter({ type: 'table', schema: x.schema, name: x.name }));
1211+
const resultViewColumns = viewColumns.filter((x) =>
1212+
resultViews.some((v) => v.schema === x.schema && v.name === x.view)
1213+
);
1214+
12081215
return {
1209-
schemas,
1210-
tables,
1211-
enums,
1212-
columns,
1213-
indexes,
1214-
pks,
1215-
fks,
1216-
uniques,
1217-
checks,
1218-
sequences,
1219-
roles,
1216+
schemas: resultSchemas,
1217+
tables: resultTables,
1218+
enums: resultEnums,
1219+
columns: resultColumns,
1220+
indexes: resultIndexes,
1221+
pks: resultPKs,
1222+
fks: resultFKs,
1223+
uniques: resultUniques,
1224+
checks: resultChecks,
1225+
sequences: resultSequences,
1226+
roles: resultRoles,
12201227
privileges,
12211228
policies,
1222-
views,
1223-
viewColumns,
1229+
views: resultViews,
1230+
viewColumns: resultViewColumns,
12241231
} satisfies InterimSchema;
12251232
};
12261233

drizzle-kit/src/dialects/pull-utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import type { Dialect } from 'src/utils/schemaValidator';
66
export type Schema = { type: 'schema'; name: string };
77
export type Table = { type: 'table'; schema: string | false; name: string };
88
export type Role = { type: 'role'; name: string };
9+
10+
/*
11+
there's a double edge sword with having narrow list here
12+
on one hand we can filter other entities through these 3 types
13+
14+
on the other hand when debugged - you see schema/table filter invocation
15+
for all other types like enums, sequences, etc.
16+
17+
I will leave this as is and in introspect I will rely on introspected schemas and tables
18+
to filter list of dependent entities, that'd probably be the go to
19+
*/
920
export type KitEntity = Schema | Table | Role;
1021

1122
export type EntityFilter = (it: KitEntity) => boolean;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { pgSchema } from 'drizzle-orm/pg-core';
2+
import { afterAll, beforeAll, beforeEach, expect, test } from 'vitest';
3+
import { diff, prepareTestDatabase, push, TestDatabase } from './mocks';
4+
5+
// @vitest-environment-options {"max-concurrency":1}
6+
let _: TestDatabase;
7+
let db: TestDatabase['db'];
8+
9+
beforeAll(async () => {
10+
_ = await prepareTestDatabase();
11+
db = _.db;
12+
});
13+
14+
afterAll(async () => {
15+
await _.close();
16+
});
17+
18+
beforeEach(async () => {
19+
await _.clear();
20+
});
21+
22+
test('big schema #1', async () => {
23+
const schema = await import('./schemas/schema1');
24+
25+
await push({ db, to: schema });
26+
27+
const res1 = await push({ db, to: { ...schema, core: pgSchema('core').existing() } });
28+
expect(res1.sqlStatements).toStrictEqual([]);
29+
30+
const res2 = await push({ db, to: schema });
31+
expect(res2.sqlStatements).toStrictEqual([]);
32+
});

0 commit comments

Comments
 (0)