Skip to content

Commit

Permalink
Fix #481, Don't extract indices from another schema (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
orangain authored Jan 28, 2024
1 parent 62d6979 commit 5f3926e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/kinds/extractTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, expect } from "vitest";
import { test, testWith } from "../tests/useSchema";
import extractTable, {
ColumnReference,
Index,
TableCheck,
TableColumn,
TableDetails,
Expand Down Expand Up @@ -393,5 +394,26 @@ describe("extractTable", () => {
name: "fk_1",
});
});

test("should not extract indices from another schema", async ({
knex: [db],
}) => {
await db.raw("create table test.some_table (id integer)");
await db.raw("create index some_table_id_idx on test.some_table (id)");
await db.raw("create schema test2");
await db.raw("create table test2.some_table (id integer)");
await db.raw("create index some_table_id_idx2 on test2.some_table (id)");

const result = await extractTable(db, makePgType("some_table"));

const expected: Index[] = [
{
isPrimary: false,
name: "some_table_id_idx",
},
];

expect(result.columns[0].indices).toStrictEqual(expected);
});
});
});
5 changes: 4 additions & 1 deletion src/kinds/query-parts/indexMapQueryPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ const indexMapQueryPart = `
pg_class t,
pg_class i,
pg_index ix,
pg_attribute a
pg_attribute a,
pg_namespace n
WHERE
t.oid = ix.indrelid
AND i.oid = ix.indexrelid
AND a.attrelid = t.oid
AND n.oid = t.relnamespace
AND a.attnum = ANY (ix.indkey)
AND t.relkind = 'r'
AND t.relname = :table_name
AND n.nspname = :schema_name
GROUP BY
a.attname
`;
Expand Down

0 comments on commit 5f3926e

Please sign in to comment.