Skip to content

Commit f9980f3

Browse files
authored
Merge pull request #6 from nblumhardt/pgup
Apply -> Update
2 parents d40658b + 289f7ce commit f9980f3

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ Piggy is a simple command-line tool for managing schema and data changes to Post
1212

1313
Piggy is available for Windows, macOS and Linux from [the releases page](https://github.com/datalust/piggy/releases). Download the MSI or archive file for [your platform](https://docs.microsoft.com/en-us/dotnet/core/rid-catalog). If your platform of choice isn't listed, please [raise an issue here](https://github.com/datalust/piggy/issues) so that we can add it.
1414

15-
### Organizing change scripts
15+
### Workflow
1616

17-
Your `.sql` files should be named so that they will sort lexicographically in the order they need to be executed:
17+
To manage database updates with Piggy, write SQL scripts to make changes, rather than applying changes directly.
18+
19+
Organize change scripts on the file system under a _script root_ directory. Name files so that they sort lexicographically in the order in which they need to be executed:
1820

1921
```
2022
001-create-schema.sql
2123
002-create-users-table.sql
2224
003-...
2325
```
2426

25-
If the scripts are arranged in subfolders, these can be ordered as well:
27+
If the scripts are arranged in subdirectories, these must be ordered by name as well:
2628

2729
```
2830
v1/
@@ -33,21 +35,21 @@ v2/
3335
001-rename-users-table.sql
3436
```
3537

36-
Each script is just regular SQL with any DDL or DML statements required to make a change to the database. Over time, as your application grows, create new scripts to move the database forwards.
37-
38-
### Applying scripts
39-
40-
Scripts must be applied starting from a _script root_. Piggy searches for `.sql` files and generates script names like `/v1/001-create-schema.sql` using each script's filename relative to the script root. These relative filenames are checked against the change log table to determine which of them need to be run.
38+
Piggy enumerates `.sql` files and generates names like `/v1/001-create-schema.sql` using each script's filename relative to the script root. These relative names are checked against the change log table to determine which of them need to be run.
4139

42-
The script root is specified with `-s`. Other parameters identify the database server host, the database name, username and password:
40+
To bring a database up to date, run `piggy up`, providing the script root, host, database name, username and password:
4341

4442
```
45-
piggy apply -s <scripts> -h <host> -d <database> -u <username> -p <password>
43+
piggy up -s <scripts> -h <host> -d <database> -u <username> -p <password>
4644
```
4745

4846
If the database does not exist, Piggy will create it using sensible defaults. To opt out of this behavior, add the `--no-create` flag.
4947

50-
For more detailed usage information, run `piggy help apply`; to see all available commands run `piggy help`.
48+
Over time, as your application grows, create new scripts to move the database forwards - don't edit the existing ones, since they've already been applied and will be ignored by Piggy.
49+
50+
Piggy can be used to update from any previous schema version to the current one: scripts that have already been run on a database are ignored, so only necessary scripts are applied.
51+
52+
For more detailed usage information, run `piggy help up`; to see all available commands run `piggy help`.
5153

5254
### Transactions
5355

@@ -73,9 +75,9 @@ Values are inserted using pure text substitution: no escaping or other processin
7375
Variables are supplied on the command-line with the `-v` flag:
7476

7577
```
76-
piggy apply ... -v schema=myapp -v admin=myuser
78+
piggy up ... -v schema=myapp -v admin=myuser
7779
```
7880

7981
### Change log
8082

81-
The change log is stored in the target database in `piggy.changes`. The `piggy log` command can be used to view applied change scripts.
83+
The change log is stored in the target database in `piggy.changes`. The `piggy log` command is used to view applied change scripts.

src/Datalust.Piggy/Cli/Commands/ApplyCommand.cs renamed to src/Datalust.Piggy/Cli/Commands/UpdateCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
namespace Datalust.Piggy.Cli.Commands
77
{
8-
[Command("apply", "Apply a set of database change scripts")]
9-
class ApplyCommand : Command
8+
[Command("up", "Bring a database up to date")]
9+
class UpdateCommand : Command
1010
{
1111
readonly DefineVariablesFeature _defineVariablesFeature;
1212
readonly UsernamePasswordFeature _usernamePasswordFeature;
@@ -16,7 +16,7 @@ class ApplyCommand : Command
1616
string _scriptRoot;
1717
bool _createIfMissing = true;
1818

19-
public ApplyCommand()
19+
public UpdateCommand()
2020
{
2121
_databaseFeature = Enable<DatabaseFeature>();
2222
_usernamePasswordFeature = Enable<UsernamePasswordFeature>();
@@ -44,7 +44,7 @@ protected override int Run()
4444

4545
try
4646
{
47-
ApplySession.ApplyChangeScripts(
47+
UpdateSession.ApplyChangeScripts(
4848
_databaseFeature.Host, _databaseFeature.Database, _usernamePasswordFeature.Username, _usernamePasswordFeature.Password,
4949
_createIfMissing, _scriptRoot, _defineVariablesFeature.Variables);
5050

File renamed without changes.

src/Datalust.Piggy/Apply/ChangeScriptFileEnumerator.cs renamed to src/Datalust.Piggy/Update/ChangeScriptFileEnumerator.cs

File renamed without changes.

src/Datalust.Piggy/Apply/ApplySession.cs renamed to src/Datalust.Piggy/Update/UpdateSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Datalust.Piggy.Apply
1212
{
13-
static class ApplySession
13+
static class UpdateSession
1414
{
1515
public static void ApplyChangeScripts(string host, string database, string username, string password,
1616
bool createIfMissing, string scriptRoot, IReadOnlyDictionary<string, string> variables)

0 commit comments

Comments
 (0)