# @mmckelvy/apply-migrations

> Simple, idempotent database migrations for node-pg

Latest version **1.0.4** (published 2020-09-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mmckelvy/apply-migrations
pnpm add @mmckelvy/apply-migrations
yarn add @mmckelvy/apply-migrations
bun add @mmckelvy/apply-migrations
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-09-27 |
| First published | 2020-07-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Mark McKelvy |
| Maintainers | mmckelvy |

## Links

- npm: https://www.npmjs.com/package/@mmckelvy/apply-migrations
- Repository: https://github.com/mmckelvy/apply-migrations
- Homepage: https://github.com/mmckelvy/apply-migrations#readme
- Issues: https://github.com/mmckelvy/apply-migrations/issues
- npm.io page: https://npm.io/package/@mmckelvy/apply-migrations

## Dependencies (2)

- [@mmckelvy/pg-query](https://npm.io/package/@mmckelvy/pg-query.md) ^1.2.0
- [@mmckelvy/diff-arrays](https://npm.io/package/@mmckelvy/diff-arrays.md) ^1.0.1

## Recent versions

- 1.0.4 (latest) — 2020-09-27
- 1.0.3 — 2020-09-26
- 1.0.2 — 2020-07-22
- 1.0.1 — 2020-07-22
- 1.0.0 — 2020-07-22

## README

# Apply migrations
Simple, idempotent migrations for [node-pg](https://node-postgres.com/).

## Install
```
npm install @mmckelvy/apply-migrations --save-dev
```

## Suggested usage
For a full example, see the `examples` folder.

Create a directory called `migrations` and add a file called `index.js`.

Create a child directory called `src` and add your migrations to `src` as `.sql` files.  The naming convention for your files is as follows:

```
<order number>[separator]<migration name>.sql
```

Examples of valid filenames:

```
1_base-schema.sql <-- Best practice

2-adjust-column.sql
v003_drop-constraint.sql
4add-3-columns.sql
```

**The only thing that matters is the first number that appears in the filename.**  That will be used to sort the migration files in ascending order.  Using a separator such as `_` is optional, but encouraged for your own readability.

Include the following migration table at the top of your first migration file:

```
create table migration (
  migration_id integer primary key generated by default as identity,
  filename text not null,
  created_at timestamp with time zone default current_timestamp
);
```

The `migration` table will be used to check if a particular migration has already been applied.

Once you have your `src` directory with your migration files set up, import the `applyMigrations` function in `index.js` and apply your migrations:

```
const { Pool } = require('pg');
const applyMigrations = require('@mmckelvy/apply-migrations');

// an instance of a typical node-pg pool.
(async () => {
  const pool = new Pool({
    host: 'localhost',
    database: db,
    port: 5432,
    user: process.env.USER,
    password: null,
    max: 10
  });

  await applyMigrations({
    pool,
    src: `${__dirname}/src` // assumes your migration directory is called 'src'
  });

  await pool.end();
})();

```

## API
### applyMigrations({ pool, src })
Returns `void`.

Executes a sequence of `.sql` files (migrations) in ascending order.  Upon successful execution of each migration, records the migration in a `migration` table.

Notes:
* You will be responsible for instantiating and ending the pool, as well as creating the `migration` table (see suggested usage above).
* If a migration fails, `applyMigrations` will abort immediately.  No further migrations will be applied.

----
#### pool
Type: `object`

A node-pg [pool](https://node-postgres.com/features/pooling) instance.  Generally you'll want to use the same pool parameters as those from your app (i.e. same host, database, port, etc.).

Note that after the migrations run, you should `end` the pool by calling `pool.end()` (see suggested usage above).

#### src
Type: `string`

A path to the directory with your migrations.  It's generally easiest to make this directory a sibling to the script where you call `applyMigrations`.


## Test
```
npm test
```

---
_Source: https://npm.io/package/@mmckelvy/apply-migrations · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
