Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pg): introspect composite index with mixed types #3859

Open
wants to merge 3 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drizzle-kit/src/serializer/pgSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ WHERE
ON i.indrelid = a.attrelid AND k.attnum = a.attnum
JOIN pg_namespace c on c.oid = t.relnamespace
LEFT JOIN pg_am AS am ON ic.relam = am.oid
JOIN pg_opclass opc ON opc.oid = ANY(i.indclass)
JOIN pg_opclass opc ON opc.oid = i.indclass[k.i-1]
WHERE
c.nspname = '${tableSchema}' AND
t.relname = '${tableName}';`,
Expand Down
14 changes: 13 additions & 1 deletion drizzle-kit/tests/introspect/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
varchar,
} from 'drizzle-orm/pg-core';
import fs from 'fs';
import { introspectPgToFile } from 'tests/schemaDiffer';
import { introspectAfterSqlStatements, introspectPgToFile } from 'tests/schemaDiffer';
import { expect, test } from 'vitest';

if (!fs.existsSync('tests/introspect/postgres')) {
Expand Down Expand Up @@ -892,3 +892,15 @@ test('multiple policies with roles from schema', async () => {
expect(statements.length).toBe(0);
expect(sqlStatements.length).toBe(0);
});

test('composite index', async () => {
const client = new PGlite();
const indexExpression = `name text_ops, deleted bool_ops, age int4_ops`;
const statements = [
`CREATE TABLE "users" ("name" text, deleted boolean, age integer);\n`,
`CREATE INDEX "idx" ON "users" USING btree (${indexExpression})`,
];
const { tables } = await introspectAfterSqlStatements(client, statements);
const types = tables['public.users']?.indexes.idx?.columns.map((c) => `${c.expression} ${c.opclass}`).join(', ')
expect(types).toBe(indexExpression);
});
35 changes: 35 additions & 0 deletions drizzle-kit/tests/schemaDiffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,41 @@ export const diffTestSchemasLibSQL = async (
return { sqlStatements, statements };
};

export const introspectAfterSqlStatements = async (
client: PGlite,
sqlStatements: string[],
schemas: string[] = ['public'],
entities?: Entities,
) => {
for (const st of sqlStatements) {
await client.query(st);
}
// introspect to schema
const introspectedSchema = await fromDatabase(
{
query: async (query: string, values?: any[] | undefined) => {
const res = await client.query(query, values);
return res.rows as any[];
},
},
undefined,
schemas,
entities,
);

const { version: _initV, dialect: _initD, ...initRest } = introspectedSchema;

const initSch = {
version: '7',
dialect: 'postgresql',
id: '0',
prevId: '0',
...initRest,
} as const;

return pgSchema.parse(initSch);
};

// --- Introspect to file helpers ---

export const introspectPgToFile = async (
Expand Down