- The Postgresql script tracking tool
Here you will see a set of scripts that demonstrate usage of Schema Guard [rdbm] by DBinvent.
The examples show most typical use cases and are written to run in a Docker container. You can also use them as a starting point to adopt Schema Guard into your workflow quickly and without touching Docker at all.
Before we move on to running tests, let's briefly describe the main Schema Guard features.
Schema Guard is a database schema migration tool for PostgreSQL that helps manage, control and deploy database schema changes across various environments and development stages.
It employs the automatic SQL generation and plain-SQL approach in one software. Scripts can be assigned with a version and a certain migration phase, which brings developers true flexibility to perform the necessary action at an exact migration stage.
- Creates database and user if needed
- Provides both: schema/table definition and script execution tracking
- Simple ETL: data extraction from snapshots AND data insert after schema migration
- Supports a separate DB connection for history table as a remote copy of a primary one
- Uses native psql client for script execution, built-in support for psql
--single-transaction
flag - No JVM nor Python required to run
- Lightweight, just about 10MB
- Comes with an apt repository to stay updated
- Transactional changes with rollback OR configurable non-transactional mode
- Dry run on existing databases, diff tool friendly output
- Free personal license
The use cases perform database schema migrations and make some dumps. Then the produced YAML and SQL files are compared with the expected result.
- Baseline - Validates the license and performs a self migration.
- Create DB - Creates user (role) and database on demand.
- Simple migration - Simple migration, schema in YAML then a few inserts in plain-SQL script.
- Versioned, schema in YAML files - Sample of declarative schema definition in YAML files, version 1 to version 2 migration.
- Versioned, schema in SQL scripts - Plain-SQL way to migrate database schema.
- Stored procure - Repeatable scripts execution on every migration, useful for stored procedures and functions.
- Mixing SQL with YAML - Mixing declarative schema definition in YAML files with plain-SQL scripts, triggers and before schema generation SQL script.
- Simple examples of Extract-Transform-Load operations: 1, 2, 3
- ETL: mixes SQL and CSV - Mixing data sourcing from SQL and CSV files.
- Tag and User - Tagging migration and run it under a specific user.
- Dry run - Lists the SQL that is generated for upcoming migration, without making actual changes.
- Remote history DB - History table in the external database.
- URL - Passing a connection string through --db_url= param rather than providing the configuration file.
- ENV - Using the environment variables to set the configuration params.
Versioned scripts, schema files and data files for ETL operations follow simple naming rules. You can find more details in the Script Versions and Types article.
[Type][Version]__[Script_name].[yaml|sql|csv]
1 2 3 4
- Type - Single Capital Letter, one of these:
P
,S
,V
,R
,D
orA
for 'after' - Version - one or more digits divided by _, which further will be replaced with dots. Ex: 1_2 will be 1.2.0
- Script name - any meaningful name of the script file
- File type: yaml, sql or csv
P
- The Pre-deploy SQL scripts are executed before the schema is changed from S and V files.S
- The next Schema version in YAML format. Schema Guard computes the changes required and generates SQL commands on the fly. You can mix S and V files. See also D type.D
- The Data files for ETL operations used in batch with the S-files. See ETL exampleV
- Transition to the next schema Version in plain-SQL. Usually, the most-used file types. You can mix V and S files.R
- The Repeatable and mutable scripts, applied every time the schema is changed. Useful for stored procedures and functions.A
- After migration scripts. Run after all the scripts of the same version or at the end of all migrations. Allowed names are after.sql and after_each.sql
To run the examples, you need the Docker installed.
Clone this repository:
git clone https://github.com/DBinvent/rdbm.git
Then cd to rdbm folder:
cd rdbm
Now you are ready to run examples, one-by-one or all at once. To get the list of all available test cases:
./test.sh
./test.sh baseline # or any other name from the list displayed by ./test.sh
If you're interested in examining the resulting schema or history table content, it can be useful to keep the psql prompt after the example is completed.
./test.sh baseline psql
You can play with preconfigured rdbm application in the launched Docker container.
./test.sh baseline bash