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

Commit

Permalink
feat: consolidate output/idempotency options
Browse files Browse the repository at this point in the history
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"
}
```
  • Loading branch information
justinlettau committed Oct 3, 2018
1 parent ad14985 commit 81029fc
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 90 deletions.
61 changes: 28 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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"
}
}
Expand Down
23 changes: 10 additions & 13 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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';
Expand Down
34 changes: 16 additions & 18 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};

/**
Expand Down
34 changes: 16 additions & 18 deletions src/common/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 2 additions & 3 deletions src/common/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down
5 changes: 3 additions & 2 deletions src/sql/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = '';
Expand Down
7 changes: 4 additions & 3 deletions src/sql/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 81029fc

Please sign in to comment.