3.0.0 • Published 3 years ago

sql-source-control v3.0.0

Weekly downloads
16
License
MIT
Repository
github
Last release
3 years ago

NPM Version CI 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
    ...
  ./jobs
    amazing-things.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
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
jobsstringSubdirectory for jobs files../jobs
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
jobsstring (1)Idempotency for job 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 trigger files.if-exists-drop
typesstring (1)Idempotency for user defined table parameter files.if-not-exists
viewsstring (1)Idempotency for view files.if-exists-drop
  1. if-exists-drop, if-not-exists, or false.
  2. delete-and-reseed, delete, truncate, or false.

includeConstraintName (boolean): Optional. Indicates if constraint names should be scripted. Default is false.

eol (string: Optional. Line ending character (auto, crlf, or lf). Default is auto.

Note: See Git documentation for information about how Git handles line endings.

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. The Web.config should be an XML .NET config file.

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

Connection strings can follow any of the following formats:

<connectionStrings>
  <add name="Example1" connectionString="server=MySqlServer;database=MySqlDb;uid=MyUsername;password=MyPassword;" />
  <add name="Example2" connectionString="server=MySqlServer;database=MySqlDb;uid=MyUsername;pwd=MyPassword;" />
  <add name="Example3" connectionString="server=MySqlServer,1433;database=MySqlDb;uid=MyUsername;pwd=MyPassword;" />
</connectionStrings>

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 committed.

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

Files

Only include certain files.

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

Exclude certain files.

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

Data

Only include certain tables.

{
  // ...
  "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",
    "jobs": "./jobs",
    "procs": "./stored-procedures",
    "schemas": "./schemas",
    "tables": "./tables",
    "triggers": "./triggers",
    "types": "./types",
    "views": "./views"
  },
  "idempotency": {
    "data": "truncate",
    "functions": "if-exists-drop",
    "jobs": "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

To use local docker container:

npm run docker:up
./docker/restore.ps1

ssc pull -c ./docker/config.json
3.0.0

3 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.9.1

6 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago