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

Commit

Permalink
feat: add job generation to pull
Browse files Browse the repository at this point in the history
Closes #31
  • Loading branch information
justinlettau committed Jan 15, 2019
1 parent 1378a30 commit 14125e2
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 39 deletions.
64 changes: 51 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
[![NPM Version](https://badge.fury.io/js/sql-source-control.svg)](https://badge.fury.io/js/sql-source-control)

[![Build Status](https://travis-ci.org/justinlettau/sql-source-control.svg?branch=master)](https://travis-ci.org/justinlettau/sql-source-control)

[![Build status](https://ci.appveyor.com/api/projects/status/a92idr95kkly8lgt/branch/master?svg=true)](https://ci.appveyor.com/project/justinlettau/sql-source-control/branch/master)

[![Dependency Status](https://david-dm.org/justinlettau/sql-source-control.svg)](https://david-dm.org/justinlettau/sql-source-control)

[![Dev Dependency Status](https://david-dm.org/justinlettau/sql-source-control/dev-status.svg)](https://david-dm.org/justinlettau/sql-source-control?type=dev)

# SQL Source Control

CLI for scripting SQL objects into a flat file structure for use with source control systems.

# Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
Expand All @@ -22,16 +28,19 @@ CLI for scripting SQL objects into a flat file structure for use with source con
- [Development](#development)

# Features

- Works with **any source control system** like Git, SVN, Mercurial, etc.
- Supports all recent version of **Microsoft SQL Server**.
- **Free and open source**!

# Installation

```bash
npm install -g sql-source-control
```

# Usage

Commands are directory specific, so run all commands in the directory you want the scripts created in.

```bash
Expand All @@ -42,6 +51,7 @@ ssc --help
If TCP/IP is not enabled, you may receive a "failed to connect" error on commands.

### `ssc init`

This will ask you a bunch of questions, and then write a config file for you.

If the current directory contains a `Web.config` file with the `connectionStrings` property, the
Expand All @@ -51,21 +61,23 @@ specified with the `--webconfig` flag.
Options:

| Option | Alias | Type | Description | Default |
|---------------|-------|-----------|-------------------------------------------------|---------|
| ------------- | ----- | --------- | ----------------------------------------------- | ------- |
| `--force` | `-f` | `boolean` | Overwrite an existing config file, if present. | n/a |
| `--skip` | `-s` | `boolean` | Use defaults and not prompt you for any options | n/a |
| `--webconfig` | `-w` | `string` | Relative path to a `Web.config` file. | n/a |

### `ssc list`

List all available connections specified in the configuration file.

Options:

| Option | Alias | Type | Description | Default |
|------------|-------|----------|-------------------------------|------------|
| ---------- | ----- | -------- | ----------------------------- | ---------- |
| `--config` | `-c` | `string` | Relative path to config file. | `ssc.json` |

### `ssc pull [conn]`

Generate SQL files for all tables, stored procedures, functions, etc. All scripts will be put in
the `output.root` directory and SQL scripts will be organized into subdirectories (based on config
file).
Expand All @@ -80,13 +92,13 @@ only include static data tables, like lookup tables, in the `data` configuration
Arguments:

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

Options:

| Option | Alias | Type | Description | Default |
|------------|-------|----------|-------------------------------|------------|
| ---------- | ----- | -------- | ----------------------------- | ---------- |
| `--config` | `-c` | `string` | Relative path to config file. | `ssc.json` |

Example output (see [here](https://github.com/justinlettau/sql-source-control-example) for full example):
Expand All @@ -100,6 +112,9 @@ Example output (see [here](https://github.com/justinlettau/sql-source-control-ex
dbo.complex-math.sql
dbo.awesome-table-function.sql
...
./jobs
amazing-things.sql
...
./schemas
dbo.sql
...
Expand All @@ -118,29 +133,31 @@ Example output (see [here](https://github.com/justinlettau/sql-source-control-ex
```

### `ssc push [conn]`

Execute all local scripts against the requested database.

Arguments:

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

Options:

| Option | Alias | Type | Description | Default |
|------------|-------|-----------|-------------------------------|------------|
| ---------- | ----- | --------- | ----------------------------- | ---------- |
| `--config` | `-c` | `string` | Relative path to config file. | `ssc.json` |
| `--skip` | `-s` | `boolean` | Skip user warning prompt. | `false` |

# Configuration

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

**connections** (`object[]`, `string`): Relative path to a `Web.config` file with `connectionStrings`, a
`ssc-connections.json` file with an array of connections, or an array of connections with the following properties:

| Property | Type | Description | Default |
|------------|----------|------------------|---------|
| ---------- | -------- | ---------------- | ------- |
| `name` | `string` | Connection name. | n/a |
| `server` | `string` | Server name. | n/a |
| `database` | `string` | Database name. | n/a |
Expand All @@ -157,10 +174,11 @@ includes none.
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` |
| `functions` | `string` | Subdirectory for function files. | `./functions` |
| `jobs` | `string` | Subdirectory for jobs files. | `./jobs` |
| `procs` | `string` | Subdirectory for stored procedure files. | `./stored-procedures` |
| `schemas` | `string` | Subdirectory for schema files. | `./schemas` |
| `tables` | `string` | Subdirectory for table files. | `./tables` |
Expand All @@ -172,22 +190,25 @@ properties are supported:
following properties are supported.

| Property | Type | Description | Default |
|-------------|--------------|-----------------------------------------------------|------------------|
| ----------- | ------------ | --------------------------------------------------- | ---------------- |
| `data` | `string` (2) | Idempotency for data files. | `truncate` |
| `functions` | `string` (1) | Idempotency for function files. | `if-exists-drop` |
| `jobs` | `string` (1) | Idempotency for jobs 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` |
| `triggers` | `string` (1) | Idempotency for trigger 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` |
| `views` | `string` (1) | Idempotency for view files. | `if-exists-drop` |

1. `if-exists-drop`, `if-not-exists`, or `false`.
2. `delete-and-ressed`, `delete`, `truncate`, or `false`.
2. `delete-and-reseed`, `delete`, `truncate`, or `false`.

# Examples

### Connections

Basic connections.

```json
{
"connections": [
Expand All @@ -204,22 +225,26 @@ Basic connections.
```

Connections stored in `Web.config` file.

```json
{
"connections": "./Web.config"
}
```

Connections stored in separate JSON file. Storing connections in a separate JSON can be used in conjunction with a
`.gitignore` entry to prevent user connections or sensitive data from being commited.
`.gitignore` entry to prevent user connections or sensitive data from being committed.

```json
{
"connections": "./ssc-connections.json"
}
```

### Files

Only include certain files.

```js
{
// ...
Expand All @@ -228,6 +253,7 @@ Only include certain files.
```

Exclude certain files.

```js
{
// ...
Expand All @@ -236,7 +262,9 @@ Exclude certain files.
```

### Data

Only include certain tales.

```js
{
// ...
Expand All @@ -245,6 +273,7 @@ Only include certain tales.
```

Exclude certain tables.

```js
{
// ...
Expand All @@ -253,7 +282,9 @@ Exclude certain tables.
```

### Output

Override default options.

```js
{
// ...
Expand All @@ -266,7 +297,9 @@ Override default options.
```

### Idempotency

Override default options.

```js
{
// ...
Expand All @@ -278,7 +311,9 @@ Override default options.
```

### Defaults

Default configuration values.

```json
{
"connections": [],
Expand All @@ -288,6 +323,7 @@ Default configuration values.
"root": "./_sql-database",
"data": "./data",
"functions": "./functions",
"jobs": "./jobs",
"procs": "./stored-procedures",
"schemas": "./schemas",
"tables": "./tables",
Expand All @@ -298,6 +334,7 @@ Default configuration values.
"idempotency": {
"data": "truncate",
"functions": "if-exists-drop",
"jobs": "if-exists-drop",
"procs": "if-exists-drop",
"tables": "if-not-exists",
"triggers": "if-exists-drop",
Expand All @@ -308,6 +345,7 @@ Default configuration values.
```

# Development

Clone the repo and run the following commands in the `sql-source-control` directory:

```bash
Expand Down
29 changes: 25 additions & 4 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import * as multimatch from 'multimatch';
import * as ora from 'ora';

import Config from '../common/config';
import Connection from '../common/connection';
import FileUtility from '../common/file-utility';
import MSSQLGenerator from '../generators/mssql';
import {
SqlColumn,
SqlDataResult,
SqlForeignKey,
SqlIndex,
SqlJob,
SqlJobSchedule,
SqlJobStep,
SqlObject,
SqlPrimaryKey,
SqlTable,
Expand All @@ -21,6 +23,9 @@ import {
columnsRead,
foreignKeysRead,
indexesRead,
jobSchedulesRead,
jobsRead,
jobStepsRead,
objectsRead,
primaryKeysRead,
tablesRead,
Expand All @@ -39,7 +44,7 @@ export default class Pull {
/**
* Invoke action.
*/
invoke(): void {
invoke() {
const config = new Config(this.options.config);
const conn = config.getConnection(this.name);

Expand All @@ -56,7 +61,10 @@ export default class Pull {
pool.request().query(primaryKeysRead),
pool.request().query(foreignKeysRead),
pool.request().query(indexesRead),
pool.request().query(typesRead)
pool.request().query(typesRead),
pool.request().query(jobsRead(conn.database)),
pool.request().query(jobStepsRead(conn.database)),
pool.request().query(jobSchedulesRead(conn.database))
])
.then(results => {
const tables: sql.IRecordSet<SqlTable> = results[1].recordset;
Expand Down Expand Up @@ -107,7 +115,10 @@ export default class Pull {
const foreignKeys: SqlForeignKey[] = results[4].recordset;
const indexes: SqlIndex[] = results[5].recordset;
const types: SqlType[] = results[6].recordset;
const data: SqlDataResult[] = results.slice(7);
const jobs: SqlJob[] = results[7].recordset;
const jobSteps: SqlJobStep[] = results[8].recordset;
const jobSchedules: SqlJobSchedule[] = results[9].recordset;
const data: SqlDataResult[] = results.slice(10);

const generator = new MSSQLGenerator(config);
const file = new FileUtility(config);
Expand Down Expand Up @@ -200,6 +211,16 @@ export default class Pull {
file.write(config.output.data, name, content);
});

// jobs
jobs.forEach(item => {
const steps = jobSteps.filter(x => x.job_id === item.job_id);
const schedules = jobSchedules.filter(x => x.job_id === item.job_id);
const name = `${item.name}.sql`;
const content = generator.job(item, steps, schedules);

file.write(config.output.jobs, name, content);
});

const msg = file.finalize();
this.spinner.succeed(msg);
}
Expand Down
Loading

0 comments on commit 14125e2

Please sign in to comment.