2.1.21 • Published 4 years ago

sql-source-control-follow v2.1.21

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

NPM Version Build Status Build status Dependency Status Dev Dependency Status

SQL Source Control

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

Table of Contents

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

npm install -g sql-source-control

Usage

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

ssc --help

Note: Make sure to enable TCP/IP in "SQL Server Network Configuration" settings (instructions). 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 first node will be used for default values. Alternatively, a path to a Web.config file can be specified with the --webconfig flag.

Options:

OptionAliasTypeDescriptionDefault
--force-fbooleanOverwrite an existing config file, if present.n/a
--skip-sbooleanUse defaults and not prompt you for any optionsn/a
--webconfig-wstringRelative path to a Web.config file.n/a

ssc list

List all available connections specified in the configuration file.

Options:

OptionAliasTypeDescriptionDefault
--config-cstringRelative 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).

Within the output.root directory, cache.json is automatically generated and is intended to be committed into source repositories. This file stores checksums of each file for comparison, to reduce disk I/O.

Data can be included in the via the data option in the configuration file. All tables included in the data option will result in a file that truncates the table and inserts all rows. Because a truncate is issued, it is recommended to only include static data tables, like lookup tables, in the data configuration.

Arguments:

ArgumentDescriptionDefault
connOptional name of the connection to use.First available connection from config.

Options:

OptionAliasTypeDescriptionDefault
--config-cstringRelative path to config file.ssc.json

Example output (see here for full example):

./_sql-database
  ./data
    dbo.easy-lookup.sql
    ...
  ./functions
    dbo.complex-math.sql
    dbo.awesome-table-function.sql
    ...
  ./schemas
    dbo.sql
    ...
  ./stored-procedures
    dbo.people-read.sql
    ...
  ./tables
    dbo.people.sql
    ...
  ./types
    dbo.people-type.sql
    ...
  ./views
    dbo.super-cool-view.sql
    ...

ssc push [conn]

Execute all local scripts against the requested database.

Arguments:

ArgumentDescriptionDefault
connOptional name of the connection to use.First available connection from config.

Options:

OptionAliasTypeDescriptionDefault
--config-cstringRelative path to config file.ssc.json
--skip-sbooleanSkip 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:

PropertyTypeDescriptionDefault
namestringConnection name.n/a
serverstringServer name.n/a
databasestringDatabase name.n/a
portnumberServer port.n/a
optionsobjectServer options{"encrypt": true}
userstringLogin username.n/a
passwordstringLogin password.n/a

files (string[]): Optional. Glob of files to include/exclude during the pull command. Default includes all files.

data (string[]): Optional. Glob of table names to include for data scripting during the pull command. Default includes none.

output (object): Optional. Defines paths where files will be scripted during the pull command. The following properties are supported:

PropertyTypeDescriptionDefault
rootstringDirectory for scripted files, relative to config file../_sql-database
datastringSubdirectory for data files../data
functionsstringSubdirectory for function files../functions
procsstringSubdirectory for stored procedure files../stored-procedures
schemasstringSubdirectory for schema files../schemas
tablesstringSubdirectory for table files../tables
triggersstringSubdirectory for trigger files../triggers
typesstringSubdirectory for table valued parameter files../types
viewsstringSubdirectory for view files../views

idempotency (object): Optional. Defines what type of idempotency will scripted during the pull command. The following properties are supported.

PropertyTypeDescriptionDefault
datastring (2)Idempotency for data files.truncate
functionsstring (1)Idempotency for function files.if-exists-drop
procsstring (1)Idempotency for stored procedure files.if-exists-drop
tablesstring (1)Idempotency for table files.if-not-exists
triggersstring (1)Idempotency for triggers files.if-exists-drop
typesstring (1)Idempotency for user defined table parameter files.if-not-exists
viewsstring (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.

Examples

Connections

Basic connections.

{
  "connections": [
    {
      "name": "dev",
      "server": "localhost\\development",
      "database": "awesome-db",
      "port": 1433,
      "user": "example",
      "password": "qwerty"
    }
  ]
}

Connections stored in Web.config file.

{
  "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.

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

Files

Only include certain files.

{
  // ...
  "files": ["dbo.*"]
}

Exclude certain files.

{
  // ...
  "files": ["*", "!dbo.*"]
}

Data

Only include certain tales.

{
  // ...
  "data": ["dbo.*"]
}

Exclude certain tables.

{
  // ...
  "data": ["*", "!dbo.*"]
}

Output

Override default options.

{
  // ...
  "output": {
    "root": "./my-database",
    "procs": "./my-procs",
    "triggers": false
  }
}

Idempotency

Override default options.

{
  // ...
  "idempotency": {
    "triggers": false,
    "views": "if-not-exists"
  }
}

Defaults

Default configuration values.

{
  "connections": [],
  "files": [],
  "data": [],
  "output": {
    "root": "./_sql-database",
    "data": "./data",
    "functions": "./functions",
    "procs": "./stored-procedures",
    "schemas": "./schemas",
    "tables": "./tables",
    "triggers": "./triggers",
    "types": "./types",
    "views": "./views"
  },
  "idempotency": {
    "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"
  }
}

Development

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

npm install
npm link
npm run build
2.1.21

4 years ago

2.0.18

4 years ago

2.1.20

4 years ago

2.0.16

5 years ago

2.0.15

5 years ago

2.0.14

5 years ago

2.0.13

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago