unique analysis

This commit is contained in:
Jan Prochazka 2021-08-26 13:00:38 +02:00
parent 3fe13f0443
commit dfe37496f2
3 changed files with 33 additions and 6 deletions

View File

@ -3,7 +3,7 @@ const { testWrapper } = require('../tools');
const t1Sql = 'CREATE TABLE t1 (id int not null primary key, val1 varchar(50) null)';
const ix1Sql = 'CREATE index ix1 ON t1(val1, id)';
const t2Sql = 'CREATE TABLE t2 (id int not null primary key, val2 varchar(50) null)';
const t2Sql = 'CREATE TABLE t2 (id int not null primary key, val2 varchar(50) null unique)';
const txMatch = (tname, vcolname, nextcol) =>
expect.objectContaining({
@ -125,4 +125,18 @@ describe('Table analyse', () => {
expect(t1.indexes[0].columns[1]).toEqual(expect.objectContaining({ columnName: 'id' }));
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Unique - full analysis - %s',
testWrapper(async (conn, driver, engine) => {
await driver.query(conn, t2Sql);
const structure = await driver.analyseFull(conn);
const t2 = structure.tables.find(x => x.pureName == 't2');
const indexesAndUniques = [...t2.uniques, ...t2.indexes];
expect(indexesAndUniques.length).toEqual(1);
expect(indexesAndUniques[0].columns.length).toEqual(1);
expect(indexesAndUniques[0].columns[0]).toEqual(expect.objectContaining({ columnName: 'val2' }));
})
);
});

View File

@ -120,6 +120,7 @@ class Analyser extends DatabaseAnalyser {
}))
),
})),
uniques: [],
};
}),
views: views.rows.map(view => ({

View File

@ -6,11 +6,12 @@ SELECT
m.name as tableName,
il.name as constraintName,
il."unique" as isUnique,
ii.name as columnName
ii.name as columnName,
il.origin
FROM sqlite_schema AS m,
pragma_index_list(m.name) AS il,
pragma_index_info(il.name) AS ii
WHERE m.type='table' AND il.name NOT LIKE 'sqlite_autoindex_%'
WHERE m.type='table' AND il.origin <> 'pk'
ORDER BY ii.seqno
`;
@ -82,7 +83,9 @@ class Analyser extends DatabaseAnalyser {
autoIncrement: tableSqls[tableName].toLowerCase().includes('autoincrement') && !!col.pk,
}));
const indexNames = _.uniq(indexcols.rows.filter((x) => x.tableName == tableName).map((x) => x.constraintName));
const indexNames = _.uniq(
indexcols.rows.filter((x) => x.tableName == tableName && x.origin == 'c').map((x) => x.constraintName)
);
tableObj.indexes = indexNames.map((idx) => ({
constraintName: idx,
@ -92,6 +95,17 @@ class Analyser extends DatabaseAnalyser {
.map(({ columnName }) => ({ columnName })),
}));
const uniqueNames = _.uniq(
indexcols.rows.filter((x) => x.tableName == tableName && x.origin == 'u').map((x) => x.constraintName)
);
tableObj.uniques = uniqueNames.map((idx) => ({
constraintName: idx,
columns: indexcols.rows
.filter((x) => x.tableName == tableName && x.constraintName == idx)
.map(({ columnName }) => ({ columnName })),
}));
const pkColumns = info.rows
.filter((x) => x.pk)
.map((col) => ({
@ -120,8 +134,6 @@ class Analyser extends DatabaseAnalyser {
};
return fk;
});
// console.log(info);
}
for (const viewName of this.getRequestedObjectPureNames(