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

Commit

Permalink
feat: support both CLUSTERED and NONCLUSTERED INDEX, PERSISTED comput…
Browse files Browse the repository at this point in the history
…ed columns (#133)

Co-authored-by: cesrom <[email protected]>
  • Loading branch information
César Román and cesrom authored Sep 22, 2020
1 parent e109f7d commit d37ad0a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/generators/mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,12 @@ export default class MSSQLGenerator {

if (item.is_computed) {
output += ` AS ${item.formula}`;

if (item.is_persisted) {
output += ' PERSISTED';
output += item.is_nullable ? ' NULL' : ' NOT NULL';
}

return output;
}

Expand Down Expand Up @@ -666,7 +672,7 @@ export default class MSSQLGenerator {
output += ' UNIQUE';
}

output += ` NONCLUSTERED INDEX [${first.name}] ON ${objectId}`;
output += ` ${first.type} INDEX [${first.name}] ON ${objectId}`;
output += '(';

if (items.length > 1) {
Expand Down
2 changes: 2 additions & 0 deletions src/queries/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface SqlColumn {
seed_value: number;
increment_value: number;
formula: string;
is_persisted: boolean;
default_name: string;
}

Expand Down Expand Up @@ -109,6 +110,7 @@ export interface SqlIndex {
column: string;
schema: string;
table: string;
type: 'CLUSTERED' | 'NONCLUSTERED';
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/queries/mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const columnsRead = `
ic.seed_value,
ic.increment_value,
cc.definition AS [formula],
cc.is_persisted,
dc.name as default_name
FROM
sys.columns c
Expand Down Expand Up @@ -85,6 +86,10 @@ export const primaryKeysRead = `
ic.is_included_column = 0
AND ic.index_id = k.unique_index_id
AND k.type = 'PK'
ORDER BY
c.object_id,
k.name,
ic.key_ordinal
`;

/**
Expand Down Expand Up @@ -126,18 +131,20 @@ export const indexesRead = `
i.name,
c.name AS [column],
SCHEMA_NAME(ro.schema_id) AS [schema],
ro.name AS [table]
ro.name AS [table],
CASE i.type WHEN 1 THEN 'CLUSTERED' WHEN 2 THEN 'NONCLUSTERED' END AS [type]
FROM
sys.index_columns ic
JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
JOIN sys.indexes i ON i.object_id = c.object_id AND i.index_id = ic.index_id AND i.is_primary_key = 0 AND i.type = 2
JOIN sys.indexes i ON i.object_id = c.object_id AND i.index_id = ic.index_id AND i.is_primary_key = 0 AND i.type IN (1, 2)
INNER JOIN sys.objects ro ON ro.object_id = c.object_id
WHERE
ro.is_ms_shipped = 0
AND ic.is_included_column = 0
ORDER BY
ro.schema_id,
ro.name,
ic.key_ordinal,
c.object_id
`;

Expand Down

0 comments on commit d37ad0a

Please sign in to comment.