simple-postgres-migrations
v1.0.2
Published
A simple library to manage Postgres databases migrations using raw SQL files.
Downloads
4
Readme
Simple Postgres Migrations
A simple library to manage Postgres databases migrations using raw SQL files.
How to use
- Firstly, setup the needed environment variables :
PGHOST
: the host on which the Postgres service runsPGPORT
: the port on which the Postgres service runsPGUSER
: the database's userPGPASSWORD
: the user's passwordPGDATABASE
: the database's name
- Create an empty SQL migration file (also create a migration directory in your project root if it doesn't exists) : npx migration create filename. This creates a file named YYYMMdd-filename.sql.
- Fill in your sql commands.
- Then run the following command to apply the content of your migrations directory :
npx simple-postgres-migrations apply
.
If you just cloned a project using this library, you can just apply the previous migrations using npx simple-postgres-migrations apply
to setup your schema accordingly.
How do this works
Migrations are persisted in a table named "migrations" in your database. This table contains a single column which is the name of the file which had been applied.
This means that each run of the command npx simple-postgres-migrations apply
fills in a new entry in your database table.
Reset the migrations table
You can empty the migrations table to force a re-application of the migrations files with the following command : npx simple-postgres-migrations reset
.
Do note however that this doesn't undo the previously applied migrations, so you may encounter conflicts at some point when you run npx simple-postgres-migrations apply
.