Not for production purposes.
Tool to migrate database from files.
$ pip install pgrate
pgrate -p directorie/to/migrations -d postgres://username:password@localhost:5432/database
OR
pgrate -p directorie/to/migrations -d postgresql://username:password@localhost:5432/database
NOTE Migration name must starts with a number + "_", where number is migration version and ends with *.up.sql example (001_migration_name.up.slq)
- Move into working directory and create default migrations folder
$ cd /path/to/project
$ mkdir migrations
- Creating a migration up and down
-- migrations/001_init.up.sql
CREATE TABLE IF NOT EXISTS users(
id serial PRIMARY KEY,
name VARCHAR(255)
);
-- migrations/001_init.down.sql
DROP TABLE IF EXISTS users;
- Applying migrations
$ pgrate -p ./migrations -d postgres://username:password@localhost:5432/database
- Results
Migration schema
current_version | is_dirt |
---|---|
1 | false |
- Creating a migration up and down with error
-- migrations/002_users.up.sql
CREATE TABLE users(
id serial PRIMARY KEY,
name VARCHAR(255)
);
-- migrations/002_users.down.sql
DROP TABLE IF EXISTS users;
- Applying migrations
$ pgrate -p ./migrations -d postgres://username:password@localhost:5432/database
- Results
Console log
asyncpg.exceptions.DuplicateTableError: relation "users" already exists
Migration schema
current_version | is_dirt |
---|---|
2 | true |
Command | Description |
---|---|
'-p' '--path' |
Path to migrations folder |
'-d' '--db-uri' |
Database connection URI |