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

Commit

Permalink
feat: add prompt to push command
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlettau committed Oct 6, 2018
1 parent 15cda32 commit 2573dc2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 34 deletions.
45 changes: 24 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,18 @@ Example output:
### `ssc push [conn]`
Execute all local scripts against the requested database.

**WARNING**: All scripts are directly executed against the requested connection. This can not be undone! Be sure
to backup your database before running the `push` command.

Arguments:

| Argument | Description | Default |
|----------|-----------------------------------------|-----------------------------------------|
| `conn` | Optional name of the connection to use. | First available connection from config. |

Options:

| Option | Alias | Type | Description | Default |
|----------|-------|-----------|---------------------------|---------|
| `--skip` | `-s` | `boolean` | Skip user warning prompt. | n/a |

# Configuration
Configuration options are stored in a `ssc.json` file. The following properties are supported:

Expand All @@ -137,30 +140,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` |
| 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` |
| `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` |
| 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` |
| `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
7 changes: 7 additions & 0 deletions src/commands/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export interface InitOptions {
export interface PullOptions {
config?: string;
}

/**
* CLI arguments for `push` command.
*/
export interface PushOptions {
skip?: boolean;
}
54 changes: 43 additions & 11 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,66 @@
import chalk from 'chalk';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as inquirer from 'inquirer';
import * as sql from 'mssql';
import { EOL } from 'os';

import Config from '../common/config';
import Connection from '../common/connection';
import { PushOptions } from './interfaces';

export default class Push {

/**
* Invoke actions.
*
* @param name Optional connection name to use.
* @param options CLI options.
*/
public invoke(name?: string): void {
public invoke(name: string, options: PushOptions): void {
const start: [number, number] = process.hrtime();
const config: Config = new Config();
const conn: Connection = config.getConnection(name);

console.log(`Pushing to ${chalk.magenta(conn.database)} on ${chalk.magenta(conn.server)} ...`);
inquirer.prompt<inquirer.Answers>([
{
name: 'continue',
message: [
'WARNING! All local SQL files will be executed against the requested database.',
'This can not be undone!',
'Make sure to backup your database first.',
EOL,
'Are you sure you want to continue?'
].join(' '),
type: 'confirm',
when: !options.skip
}
])
.then(answers => {
if (answers.continue === false) {
throw 'Command aborted!';
}
})
.then(() => this.batch(config, conn))
.then(() => {
const time: [number, number] = process.hrtime(start);
console.log(chalk.green(`Finished after ${time[0]}s!`));
})
.catch(err => console.error(err));
}

/**
* Execute all files against database.
*
* @param config Configuration used to execute commands.
* @param conn Connection used to execute commands.
*/
private batch(config: Config, conn: Connection): Promise<any> {
const files: string[] = this.getFilesOrdered(config);
let promise: Promise<sql.ConnectionPool> = new sql.ConnectionPool(conn).connect();

console.log(`Pushing to ${chalk.magenta(conn.database)} on ${chalk.magenta(conn.server)} ...`);

files.forEach(file => {
console.log(`Executing ${chalk.cyan(file)} ...`);

Expand All @@ -32,25 +69,20 @@ export default class Push {

statements.forEach(statement => {
promise = promise.then(pool => {
return pool.request().batch(statement).then(result => pool);
return pool.request().batch(statement).then(() => pool);
});
});
});

promise
.then(() => {
const time: [number, number] = process.hrtime(start);
console.log(chalk.green(`Finished after ${time[0]}s!`));
})
.catch(err => console.error(err));
return promise;
}

/**
* Get all SQL files in correct execution order.
*
* @param config Config object used to search for connection.
* @param config Configuration used to search for connection.
*/
public getFilesOrdered(config: Config): string[] {
private getFilesOrdered(config: Config): string[] {
const output: string[] = [];
const directories: string[] = [
config.output.schemas,
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ program
program
.command('push [name]')
.description('Execute all scripts against the requested database.')
.action(name => {
.option('-s, --skip', 'Skip user warning prompt.')
.action((name, options) => {
const action: Push = new Push();
action.invoke(name);
action.invoke(name, options);
});

program
Expand Down

0 comments on commit 2573dc2

Please sign in to comment.