Skip to content

Commit

Permalink
fix: extract columns from sql statements within PG index definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
rorz committed Jun 5, 2024
1 parent 7704d97 commit c26ac68
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/__tests__/pg/_pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from 'drizzle-orm/pg-core';
import { compareContents } from '../utils';
import { pgGenerate } from '~/generators';
import { relations } from 'drizzle-orm';
import { relations, sql } from 'drizzle-orm';

const pathPrefix = './src/__tests__/pg/';

Expand Down Expand Up @@ -162,7 +162,8 @@ async function indexesTest() {
index1: index('key_4').on(tbl.f3.desc()),
index2: index('key_5').on(tbl.f3, tbl.f4),
index3: index('key_6').on(tbl.f1.nullsFirst().desc()),
index4: index().on(tbl.f4)
index4: index().on(tbl.f4),
index5: index('key_7').using('btree', tbl.f1.asc(), sql`lower(${tbl.f2})`)
})
);

Expand Down
1 change: 1 addition & 0 deletions src/__tests__/pg/indexes.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ table table {
(f3, f4) [name: 'key_5']
f1 [name: 'key_6']
f4
(f1, f2) [name: 'key_7']
}
}
9 changes: 7 additions & 2 deletions src/generators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getTableColumns,
is,
isTable,
Table
Table,
Column
} from 'drizzle-orm';
import {
AnyInlineForeignKeys,
Expand Down Expand Up @@ -173,8 +174,12 @@ export abstract class BaseGenerator<
dbml.tab(2);

if (is(index, PgIndex) || is(index, MySqlIndex) || is(index, SQLiteIndex)) {
const configColumns = index.config.columns.flatMap((entry) =>
is(entry, SQL) ? entry.queryChunks.filter((v) => is(v, Column)) : entry
);

const idxColumns = wrapColumns(
index.config.columns as AnyColumn[],
configColumns as AnyColumn[],
this.buildQueryConfig.escapeName
);
const idxProperties = index.config.name
Expand Down

0 comments on commit c26ac68

Please sign in to comment.