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

Commit

Permalink
fix(pull): correct table-valued parameters not exists idempotency (#23)
Browse files Browse the repository at this point in the history
* chore(pull): fix the idempotency method for table-valued parameters

The idempotency method was not scripting out properly when the object type is
`TT` when the option is `if-not-exists`. This commit fixes that.

fixes #22

* chore(pull): do not script comma out after the tvp's last column

The comma after the last column is not allowed. It raises a syntax
error. Exception: `Incorrect syntax near ')'. `
  • Loading branch information
davidguilherme authored and justinlettau committed Apr 4, 2018
1 parent 2614354 commit 4c1df49
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/sql/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function idempotency(item: AbstractRecordSet, type: IdempotencyOption): s
// if not exists
if (item.type === 'TT') {
return [
'if exists (',
'if not exists (',
' select * from sys.table_types as t',
' join sys.schemas s on t.schema_id = s.schema_id',
` where t.name = '${item.name}' and s.name = '${item.schema}'`,
Expand Down Expand Up @@ -132,7 +132,7 @@ export function table(

// columns
for (const col of columns.filter(x => x.object_id === item.object_id)) {
output += ' ' + column(col);
output += ' ' + column(col) + ',';
output += EOL;
}

Expand Down Expand Up @@ -177,10 +177,18 @@ export function tvp(
output += EOL;

// columns
for (const col of columns.filter(x => x.object_id === item.object_id)) {
output += ' ' + column(col);
output += EOL;
}
columns
.filter(x => x.object_id === item.object_id)
.forEach((col, idx, array) => {
output += ' ' + column(col);

// if it is not the last column
if (idx !== array.length - 1) {
output += ',';
}

output += EOL;
});

output += ')';
output += EOL;
Expand Down Expand Up @@ -240,7 +248,6 @@ function column(item: ColumnRecordSet): string {
output += ` identity(${item.seed_value || 0}, ${item.increment_value || 1})`;
}

output += ',';
return output;
}

Expand Down

0 comments on commit 4c1df49

Please sign in to comment.