A tool for managing migrations and versioning SQL scripts in PostgreSQL.
- Python 3.10+
- PostgreSQL with
psqlinstalled - Linux
git clone https://github.com/mk-samoilov/pg_sqlapply.git
cd pg_sqlapplyInit tool for create config template:
python3 -m sqlapply
Configure sqlapply.conf:
[DEFAULT]
logging_level = INFO
[my_database]
host = localhost
port = 5432
user = myuser
password = mypassword
dbname = mydbTCP/IP with password:
host = localhost
user = myuser
password = mypasswordUnix socket (peer auth, no password):
host = local
user = mk
password =changes/
└── <change_name>/
└── <db_section>/
├── 01_schema.sql
├── 02_data.sql
└── 03_indexes.sql
Scripts are executed in natural sort order (1, 2, 10 instead of 1, 10, 2).
Creates sqlapply schema for storing execution history:
python3 -m sqlapply --init --dbname my_database
python3 -m sqlapply my_release --initpython3 -m sqlapply my_release --showpython3 -m sqlapply my_release --checkpython3 -m sqlapply my_release
python3 -m sqlapply my_release --dbname my_database
python3 -m sqlapply my_release --pattern "01_*.sql"python3 -m sqlapply my_release --force ALL
python3 -m sqlapply my_release --force ERROR
python3 -m sqlapply my_release --force MD5DIFFpython3 -m sqlapply my_release --mode single-transaction
python3 -m sqlapply my_release --mode on-error-stoppython3 -m sqlapply my_release -C /path/to/custom.conf- General logs:
logs/log_YYYY-MM-DD.log - Scripts executions logs:
logs/execution_logs/<db>_<change>_<script>.log
mkdir -p changes/release_v1/production_db
echo "CREATE TABLE users (id SERIAL PRIMARY KEY);" > changes/release_v1/production_db/01_users.sql
echo "INSERT INTO users DEFAULT VALUES;" > changes/release_v1/production_db/02_seed.sql
python3 -m sqlapply release_v1 --init
python3 -m sqlapply release_v1 --check
python3 -m sqlapply release_v1