You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://ci.appveyor.com/project/datalust/piggy)[](https://gitter.im/datalust/piggy)
6
6
7
7
### What is Piggy?
8
8
9
9
Piggy is a simple command-line tool for managing schema and data changes to PostgreSQL databases. Piggy looks for `.sql` files in a directory and applies them to the database in order, using transactions and a change log table to ensure each script runs only once per database.
10
10
11
11
### Installation
12
12
13
-
Piggy is available as a Windows MSI installer from [the releases page](https://github.com/datalust/piggy/releases). Linux and macOS are supported, but currently require the `Datalust.Piggy` project in this repository to be built from source.
13
+
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.
14
14
15
-
### Organizing change scripts
15
+
### Workflow
16
16
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:
18
20
19
21
```
20
22
001-create-schema.sql
21
23
002-create-users-table.sql
22
24
003-...
23
25
```
24
26
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:
26
28
27
29
```
28
30
v1/
@@ -33,21 +35,21 @@ v2/
33
35
001-rename-users-table.sql
34
36
```
35
37
36
-
Each script is just regular SQL with any DDL or DML statements required to make a change to the database.
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.
41
39
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:
If the database does not exist, Piggy will create it using sensible defaults. To opt out of this behavior, add the `--no-create` flag.
49
47
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`.
51
53
52
54
### Transactions
53
55
@@ -64,7 +66,7 @@ as the first line of the script.
64
66
Piggy uses `$var$` syntax for replaced variables:
65
67
66
68
```sql
67
-
create table $schema$.users;
69
+
create table $schema$.users (name varchar(140) not null);
68
70
insert into users (name) values ('$admin$');
69
71
```
70
72
@@ -73,9 +75,9 @@ Values are inserted using pure text substitution: no escaping or other processin
73
75
Variables are supplied on the command-line with the `-v` flag:
74
76
75
77
```
76
-
piggy apply ... -v schema=myapp -v admin=myuser
78
+
piggy up ... -v schema=myapp -v admin=myuser
77
79
```
78
80
79
81
### Change log
80
82
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.
0 commit comments