Skip to content

Commit

Permalink
fix: skip non unique indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
wsporto committed Oct 18, 2024
1 parent 96ba8c5 commit 5f44a25
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/sqlite-query-analyzer/query-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ export function getIndexInfo(db: DatabaseType | LibSqlDatabase, schema: string,
.prepare(`PRAGMA ${schema}.index_list('${table}')`)
.all() as { name: string; unique: number }[];
for (const index of indexList) {
const indexedColumns = db
//@ts-ignore
.prepare(`PRAGMA ${schema}.index_info('${index.name}')`)
.all()
.map((res: any) => res.name) as string[];
for (const column of indexedColumns) {
map.set(column, true);
if (index.unique === 1) {
const indexedColumns = db
//@ts-ignore
.prepare(`PRAGMA ${schema}.index_info('${index.name}')`)
.all()
.map((res: any) => res.name) as string[];
for (const column of indexedColumns) {
map.set(column, true);
}
}
}
return map;
Expand Down
38 changes: 38 additions & 0 deletions tests/sqlite/sqlite-query-executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,44 @@ describe('sqlite-query-executor', () => {
assert.deepStrictEqual(actual, expected);
});

it('loadDbSchema - albums with non unique index', () => {
const db = new Database('./mydb.db');

const dbSchema = loadDbSchema(db);
if (isLeft(dbSchema)) {
assert.fail(`Shouldn't return an error`);
}

const actual = dbSchema.right.filter(col => col.table === 'albums');
const expected: ColumnSchema[] = [
{
column: "AlbumId",
column_type: "INTEGER",
columnKey: "PRI",
notNull: true,
schema: "main",
table: "albums",
},
{
column: "Title",
column_type: "TEXT",
columnKey: "",
notNull: true,
schema: "main",
table: "albums",
},
{
column: "ArtistId",
column_type: "INTEGER",
columnKey: "",
notNull: true,
schema: "main",
table: "albums",
},
]
assert.deepStrictEqual(actual, expected);
});

it('loadCreateTableStmtWithCheckConstraint', () => {
const db = new Database('./mydb.db');

Expand Down

0 comments on commit 5f44a25

Please sign in to comment.