Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
fix: remove identity insert when no identiity
Browse files Browse the repository at this point in the history
Closes #53
  • Loading branch information
justinlettau committed Dec 24, 2018
1 parent 25c1e78 commit dbe7d33
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@ export default class Pull {
pool.request().query(typesRead)
])
.then(results => {
const tables: string[] = results[1].recordset
.map(item => `${item.schema}.${item.name}`);
const tables: sql.IRecordSet<SqlTable> = results[1].recordset;
const names: string[] = tables.map(item => `${item.schema}.${item.name}`);

const matched: string[] = multimatch(tables, config.data);
const matched: string[] = multimatch(names, config.data);

if (!matched.length) {
return results;
}

return Promise.all<any>(
matched.map(item => {
const match: SqlTable = tables.find(table => item === `${table.schema}.${table.name}`);

return pool.request()
.query(`SELECT * FROM ${item}`)
.then(result => ({ name: item, result }));
.then(result => ({ name: item, hasIdentity: match.identity_count > 0, result }));
})
)
.then(data => [...results, ...data]);
Expand Down
17 changes: 12 additions & 5 deletions src/generators/mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ export default class MSSQLGenerator {
}

output += EOL;
output += `SET IDENTITY_INSERT ${item.name} ON`;
output += EOL;
output += EOL;

if (item.hasIdentity) {
output += `SET IDENTITY_INSERT ${item.name} ON`;
output += EOL;
output += EOL;
}

item.result.recordset.forEach(row => {
const keys: string[] = Object.keys(row);
Expand All @@ -67,9 +70,13 @@ export default class MSSQLGenerator {
output += EOL;
});

output += EOL;
output += `SET IDENTITY_INSERT ${item.name} OFF`;
if (item.hasIdentity) {
output += EOL;
output += `SET IDENTITY_INSERT ${item.name} OFF`;
output += EOL;
}

output += EOL;
return output;
}

Expand Down
6 changes: 4 additions & 2 deletions src/queries/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ export interface SqlSchema {
*/
export interface SqlDataResult {
name: string;
hasIdentity: number;
result: sql.IResult<any>;
}

/**
* SQL table object.
*/
// tslint:disable-next-line:no-empty-interface
export interface SqlTable extends AbstractSqlObject { }
export interface SqlTable extends AbstractSqlObject {
identity_count: number;
}

/**
* SQL type.
Expand Down
12 changes: 11 additions & 1 deletion src/queries/mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ export const tablesRead: string = `
o.object_id,
o.type,
s.name AS [schema],
o.name
o.name,
isnull(c.identity_count, 0) AS [identity_count]
FROM
sys.objects o
JOIN sys.schemas s ON o.schema_id = s.schema_id
LEFT JOIN (
SELECT
i.object_id,
count(1) AS [identity_count]
FROM
sys.identity_columns i
GROUP BY
i.object_id
) c on c.object_id = o.object_id
where
o.type = 'U'
AND o.is_ms_shipped = 0
Expand Down

0 comments on commit dbe7d33

Please sign in to comment.