From 81029fc4dcaad180d579c1b07e67c929a0f63358 Mon Sep 17 00:00:00 2001 From: Justin Lettau Date: Tue, 2 Oct 2018 20:03:48 -0400 Subject: [PATCH] feat: consolidate output/idempotency options BREAKING CHANGE: Before: ``` "output": { "scalar-valued": "./functions/scalar-valued", "table-valued": "./functions/table-valued", "table-valued-parameters": "./user-defined-types/table-valued-parameters" } ``` ``` "idempotency": { "scalar-valued": "if-exists-drop", "table-valued": "if-exists-drop", "table-valued-parameters": "if-not-exists" } ``` After: ``` "output": { "functions": "./functions", "types": "./types" } ``` ``` "idempotency": { "functions": "if-exists-drop", "types": "if-not-exists" } ``` --- README.md | 61 ++++++++++++++++++---------------------- src/commands/pull.ts | 23 +++++++-------- src/common/config.ts | 34 +++++++++++----------- src/common/interfaces.ts | 34 +++++++++++----------- src/common/utility.ts | 5 ++-- src/sql/script.ts | 5 ++-- src/sql/sys.ts | 7 +++-- 7 files changed, 79 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index eff4338..8053c17 100644 --- a/README.md +++ b/README.md @@ -81,10 +81,8 @@ Example output: ./data dbo.easy-lookup.sql ... - ./functions/scalar-valued + ./functions dbo.complex-math.sql - ... - ./functions/table-valued dbo.awesome-table-function.sql ... ./schemas @@ -96,8 +94,8 @@ Example output: ./tables dbo.people.sql ... - ./user-defined-types/table-valued-parameters - dbo.insert-people-params.sql + ./types + dbo.people-type.sql ... ./views dbo.super-cool-view.sql @@ -139,32 +137,30 @@ includes none. **output** (`object`): Optional. Defines paths where files will be scripted during the `pull` command. The following properties are supported: -| Property | Type | Description | Default | -|---------------------------|----------|--------------------------------------------------------|------------------------------------------------| -| `root` | `string` | Directory for scripted files, relative to config file. | `./_sql-database` | -| `data` | `string` | Subdirectory for data files. | `./data` | -| `procs` | `string` | Subdirectory for stored procedure files. | `./stored-procedures` | -| `scalar-valued` | `string` | Subdirectory for scalar function files. | `./functions/scalar-valued` | -| `schemas` | `string` | Subdirectory for schema files. | `./schemas` | -| `table-valued-parameters` | `string` | Subdirectory for table valued parameter files. | `./user-defined-types/table-valued-parameters` | -| `table-valued` | `string` | Subdirectory for table functions. | `./functions/table-valued` | -| `tables` | `string` | Subdirectory for table files. | `./tables` | -| `triggers` | `string` | Subdirectory for trigger files. | `./triggers` | -| `views` | `string` | Subdirectory for view files. | `./views` | +| Property | Type | Description | Default | +|------------|----------|--------------------------------------------------------|-----------------------| +| `root` | `string` | Directory for scripted files, relative to config file. | `./_sql-database` | +| `data` | `string` | Subdirectory for data files. | `./data` | +| `functions` | `string` | Subdirectory for function files. | `./functions` | +| `procs` | `string` | Subdirectory for stored procedure files. | `./stored-procedures` | +| `schemas` | `string` | Subdirectory for schema files. | `./schemas` | +| `tables` | `string` | Subdirectory for table files. | `./tables` | +| `triggers` | `string` | Subdirectory for trigger files. | `./triggers` | +| `types` | `string` | Subdirectory for table valued parameter files. | `./types` | +| `views` | `string` | Subdirectory for view files. | `./views` | **idempotency** (`object`): Optional. Defines what type of idempotency will scripted during the `pull` command. The following properties are supported. Each property supports `if-exists-drop`, `if-not-exists`, or `false` as an option. -| Property | Type | Description | Default | -|---------------------------|--------------|-----------------------------------------------------|---------------------| -| `data` | `string` (2) | Idempotency for data files. | `truncate` | -| `procs` | `string` (1) | Idempotency for stored procedure files. | `if-exists-drop` | -| `scalar-valued` | `string` (1) | Idempotency for scalar function files. | `if-exists-drop` | -| `table-valued-parameters` | `string` (1) | Idempotency for user defined table parameter files. | `if-not-exists` | -| `table-valued` | `string` (1) | Idempotency for table-valued files. | `if-exists-drop` | -| `tables` | `string` (1) | Idempotency for table files. | `if-not-exists` | -| `triggers` | `string` (1) | Idempotency for triggers files. | `if-exists-drop` | -| `views` | `string` (1) | Idempotency for views files. | `if-exists-drop` | +| Property | Type | Description | Default | +|------------|--------------|-----------------------------------------------------|------------------| +| `data` | `string` (2) | Idempotency for data files. | `truncate` | +| `functions` | `string` (1) | Idempotency for function files. | `if-exists-drop` | +| `procs` | `string` (1) | Idempotency for stored procedure files. | `if-exists-drop` | +| `tables` | `string` (1) | Idempotency for table files. | `if-not-exists` | +| `triggers` | `string` (1) | Idempotency for triggers files. | `if-exists-drop` | +| `types` | `string` (1) | Idempotency for user defined table parameter files. | `if-not-exists` | +| `views` | `string` (1) | Idempotency for views files. | `if-exists-drop` | 1. `if-exists-drop`, `if-not-exists`, or `false`. 2. `delete-and-ressed`, `delete`, `truncate`, or `false`. @@ -264,22 +260,21 @@ Default configuration values. "output": { "root": "./_sql-database", "data": "./data", + "functions": "./functions", "procs": "./stored-procedures", - "scalar-valued": "./functions/scalar-valued", "schemas": "./schemas", - "table-valued": "./functions/table-valued", - "table-valued-parameters": "./user-defined-types/table-valued-parameters", "tables": "./tables", "triggers": "./triggers", + "types": "./types", "views": "./views" }, "idempotency": { + "data": "truncate", + "functions": "if-exists-drop", "procs": "if-exists-drop", - "scalar-valued": "if-exists-drop", - "table-valued": "if-exists-drop", - "table-valued-parameters": "if-not-exists", "tables": "if-not-exists", "triggers": "if-exists-drop", + "types": "if-not-exists", "views": "if-exists-drop" } } diff --git a/src/commands/pull.ts b/src/commands/pull.ts index 2f18824..44050dc 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -22,7 +22,7 @@ import { SqlTableValuedParameter } from '../sql/interfaces'; import * as script from '../sql/script'; -import { columnRead, foreignKeyRead, indexRead, objectRead, primaryKeyRead, tableRead, tvpRead } from '../sql/sys'; +import { columnRead, foreignKeyRead, indexRead, objectRead, primaryKeyRead, tableRead, typeRead } from '../sql/sys'; import { PullOptions } from './interfaces'; export default class Pull { @@ -51,7 +51,7 @@ export default class Pull { pool.request().query(primaryKeyRead), pool.request().query(foreignKeyRead), pool.request().query(indexRead), - pool.request().query(tvpRead), + pool.request().query(typeRead), ...config.data.map(table => { return pool.request() .query(`select * from ${table}`) @@ -86,7 +86,7 @@ export default class Pull { const primaryKeys: SqlPrimaryKey[] = results[3].recordset; const foreignKeys: SqlForeignKey[] = results[4].recordset; const indexes: SqlIndex[] = results[5].recordset; - const tvps: SqlTableValuedParameter[] = results[6].recordset; + const types: SqlTableValuedParameter[] = results[6].recordset; const data: SqlDataResult[] = results.slice(7); // get unique schema names @@ -133,15 +133,15 @@ export default class Pull { this.exclude(config, existing, dir); }); - // write files for user-defined table-valued parameter - tvps.forEach(item => { + // write files for types + types.forEach(item => { const file: string = Utility.safeFile(`${item.schema}.${item.name}.sql`); if (!this.include(config.files, file)) { return; } - const content: string = script.tvp(item, columns); + const content: string = script.type(item, columns); const dir: string = this.createFile(config, item, file, content); this.exclude(config, existing, dir); }); @@ -199,20 +199,17 @@ export default class Pull { break; case 'TF': case 'IF': - output = config.output['table-valued']; - type = config.idempotency['table-valued']; - break; case 'FN': - output = config.output['scalar-valued']; - type = config.idempotency['scalar-valued']; + output = config.output.functions; + type = config.idempotency.functions; break; case 'TR': output = config.output.triggers; type = config.idempotency.triggers; break; case 'TT': - output = config.output['table-valued-parameters']; - type = config.idempotency['table-valued-parameters']; + output = config.output.types; + type = config.idempotency.types; break; default: output = 'unknown'; diff --git a/src/common/config.ts b/src/common/config.ts index c41530a..69332b1 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -116,30 +116,28 @@ export default class Config implements IConfig { * Defines paths where files will be scripted during the `pull` command. */ public output: OutputConfig = { - 'root': './_sql-database', - 'data': './data', - 'procs': './stored-procedures', - 'scalar-valued': './functions/scalar-valued', - 'schemas': './schemas', - 'table-valued': './functions/table-valued', - 'table-valued-parameters': './user-defined-types/table-valued-parameters', - 'tables': './tables', - 'triggers': './triggers', - 'views': './views' + root: './_sql-database', + data: './data', + functions: './functions', + procs: './stored-procedures', + schemas: './schemas', + tables: './tables', + triggers: './triggers', + types: './types', + views: './views' }; /** * Defines what type of idempotency will scripted during the `pull` command. */ public idempotency: IdempotencyConfig = { - 'data': 'truncate', - 'procs': 'if-exists-drop', - 'scalar-valued': 'if-exists-drop', - 'table-valued': 'if-exists-drop', - 'table-valued-parameters': 'if-not-exists', - 'tables': 'if-not-exists', - 'triggers': 'if-exists-drop', - 'views': 'if-exists-drop' + data: 'truncate', + functions: 'if-exists-drop', + procs: 'if-exists-drop', + tables: 'if-not-exists', + triggers: 'if-exists-drop', + types: 'if-not-exists', + views: 'if-exists-drop' }; /** diff --git a/src/common/interfaces.ts b/src/common/interfaces.ts index 9d9676c..d9fb52b 100644 --- a/src/common/interfaces.ts +++ b/src/common/interfaces.ts @@ -29,28 +29,26 @@ export interface IConnection { * Supported idempotency configuration. */ export interface IdempotencyConfig { - 'data'?: IdempotencyData; - 'procs'?: IdempotencyObject; - 'scalar-valued'?: IdempotencyObject; - 'table-valued'?: IdempotencyObject; - 'table-valued-parameters'?: IdempotencyObject; - 'tables'?: IdempotencyObject; - 'triggers'?: IdempotencyObject; - 'views'?: IdempotencyObject; + data?: IdempotencyData; + functions?: IdempotencyObject; + procs?: IdempotencyObject; + tables?: IdempotencyObject; + triggers?: IdempotencyObject; + types?: IdempotencyObject; + views?: IdempotencyObject; } /** * Supported output configuration. */ export interface OutputConfig { - 'root'?: string; - 'data'?: string | false; - 'procs'?: string | false; - 'scalar-valued'?: string | false; - 'schemas'?: string | false; - 'table-valued'?: string | false; - 'table-valued-parameters'?: string | false; - 'tables'?: string | false; - 'triggers'?: string | false; - 'views'?: string | false; + root?: string; + data?: string | false; + functions?: string | false; + procs?: string | false; + schemas?: string | false; + tables?: string | false; + triggers?: string | false; + types?: string | false; + views?: string | false; } diff --git a/src/common/utility.ts b/src/common/utility.ts index 7f8d420..20b5a5f 100644 --- a/src/common/utility.ts +++ b/src/common/utility.ts @@ -27,12 +27,11 @@ export default class Utility { const directories: string[] = [ config.output.schemas, config.output.tables, + config.output.types, config.output.views, - config.output['scalar-valued'], - config.output['table-valued'], + config.output.functions, config.output.procs, config.output.triggers, - config.output['table-valued-parameters'], config.output.data ] as string[]; diff --git a/src/sql/script.ts b/src/sql/script.ts index 917a351..7966c85 100644 --- a/src/sql/script.ts +++ b/src/sql/script.ts @@ -175,12 +175,12 @@ export function table( } /** - * Get script for user-defined table-valued parameter's column. + * Get script for user defined types. * * @param item Row from `sys.columns` query. * @param columns Array of records from `sys.columns` query. */ -export function tvp( +export function type( item: SqlTable, columns: SqlColumn[] ): string { @@ -214,6 +214,7 @@ export function tvp( * Get script for for table data. * * @param item Results from data query. + * @param idempotency Idempotency option to use. */ export function data(item: SqlDataResult, idempotency: IdempotencyData): string { let output: string = ''; diff --git a/src/sql/sys.ts b/src/sql/sys.ts index 7966bd1..ebd1962 100644 --- a/src/sql/sys.ts +++ b/src/sql/sys.ts @@ -122,15 +122,16 @@ export const indexRead: string = ` `; /** - * Get SQL information for table-valued parameters. + * Get SQL information for user defined types. */ -export const tvpRead: string = ` +export const typeRead: string = ` select o.object_id, o.type, s.name as [schema], t.name - from sys.table_types t + from + sys.table_types t inner join sys.objects o on o.object_id = t.type_table_object_id join sys.schemas s on t.schema_id = s.schema_id where