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

Commit

Permalink
feat: add push command
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlettau committed Feb 4, 2018
1 parent 69cb4c3 commit a54bf6f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 22 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ Example output:
...
```

## Push
Execute all local scripts against the requested database.

```bash
ssc push prod
```

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

## Cat
Concatenate all SQL files into a single file. Outputs to `./_sql-database/cat.sql`.

Expand Down
29 changes: 8 additions & 21 deletions src/commands/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,16 @@ export function cat(): void {
let output: string = '';

// order is important
const directories: string[] = [
config.output.schemas,
config.output.tables,
config.output.views,
config.output['scalar-valued'],
config.output['table-valued'],
config.output.views,
config.output.procs,
config.output.triggers
];
const files: string[] = util.getFilesOrdered(config);

for (const dir of directories) {
const files: string[] = glob.sync(`${config.output.root}/${dir}/**/*.sql`);
for (const file of files) {
const content: string = fs.readFileSync(file).toString();
const end: string = content.substr(-2).toLowerCase();

for (const file of files) {
const content: string = fs.readFileSync(file).toString();
const end: string = content.substr(-2).toLowerCase();

output += content;
output += EOL;
output += (end !== 'go' ? 'go' : '');
output += EOL + EOL;
}
output += content;
output += EOL;
output += (end !== 'go' ? 'go' : '');
output += EOL + EOL;
}

fs.outputFileSync(`${config.output.root}/cat.sql`, output);
Expand Down
45 changes: 45 additions & 0 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import chalk from 'chalk';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as sql from 'mssql';
import { EOL } from 'os';

import { Config } from '../common/config';
import { Connection } from '../common/connection';
import * as util from '../common/utility';

/**
* Execute all scripts against the requested database.
*
* @param name Connection name to use.
*/
export function push(name?: string): void {
const start: [number, number] = process.hrtime();
const config: Config = util.getConfig();
const conn: Connection = util.getConn(config, name);

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

const files: string[] = util.getFilesOrdered(config);
let promise: Promise<sql.ConnectionPool> = new sql.ConnectionPool(conn).connect();

for (const file of files) {
console.log(`Executing ${chalk.cyan(file)} ...`);

const content: string = fs.readFileSync(file, 'utf8');
const statements: string[] = content.split('go' + EOL);

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

promise
.then(() => {
const time: [number, number] = process.hrtime(start);
console.log(chalk.green(`Finished after ${time[0]}s!`));
})
.catch(err => console.error(err));
}
26 changes: 26 additions & 0 deletions src/common/utility.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk';
import * as deepmerge from 'deepmerge';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as path from 'path';
import { isString } from 'ts-util-is';
import * as xml2js from 'xml2js';
Expand Down Expand Up @@ -177,6 +178,31 @@ export function getWebConfigConns(file?: string): Connection[] {
return (conns.length ? conns : undefined);
}

/**
* Get all SQL files in correct execution order.
*
* @param config Config object used to search for connection.
*/
export function getFilesOrdered(config: Config): string[] {
const output: string[] = [];
const directories: string[] = [
config.output.schemas,
config.output.tables,
config.output.views,
config.output['scalar-valued'],
config.output['table-valued'],
config.output.procs,
config.output.triggers
];

for (const dir of directories) {
const files: string[] = glob.sync(`${config.output.root}/${dir}/**/*.sql`);
output.push(...files);
}

return output;
}

/**
* Parse connection string into object.
*
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { cat } from './commands/cat';
import { conns } from './commands/conns';
import { init } from './commands/init';
import { pull } from './commands/pull';
import { push } from './commands/push';

// check for updates
updateNotifier({ pkg }).notify();
Expand All @@ -28,6 +29,11 @@ program
.description('Generate SQL files for all tables, stored procedures, functions, etc.')
.action(pull);

program
.command('push [name]')
.description('Execute all scripts against the requested database.')
.action(push);

program
.command('cat')
.description('Concatenate all SQL files into a single file.')
Expand Down
2 changes: 1 addition & 1 deletion src/sql/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function schema(item: SchemaRecordSet): string {
output += `if not exists (select * from sys.schemas where name = '${item.name}')`;
output += EOL;

output += `create schema ${item.name}`;
output += `exec('create schema ${item.name}')`;
return output;
}

Expand Down

0 comments on commit a54bf6f

Please sign in to comment.