https://github.com/blackglory/pg-migrations
🌿
https://github.com/blackglory/pg-migrations
library nodejs npm-package typescript
Last synced: 3 months ago
JSON representation
🌿
- Host: GitHub
- URL: https://github.com/blackglory/pg-migrations
- Owner: BlackGlory
- License: mit
- Created: 2021-02-20T12:23:10.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-12-12T12:01:30.000Z (over 2 years ago)
- Last Synced: 2024-04-14T09:58:18.446Z (about 2 years ago)
- Topics: library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@blackglory/pg-migrations
- Size: 231 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# pg-migrations
A utility for database migrations with [pg].
The module create a simple migrations table to record the schema version.
[pg]: https://www.npmjs.com/package/pg
## Install
```sh
npm install --save @blackglory/pg-migrations
# or
yarn add @blackglory/pg-migrations
```
## API
```ts
interface IMigration {
// An integer starting from 1
version: number
up: string | ((client: Client) => PromiseLike)
down: string | ((client: Client) => PromiseLike)
}
```
You may need [migration-files].
[migration-files]: https://github.com/BlackGlory/migration-files
### migrate
```ts
function migrate(
client: Client
, migrations: IMigration[]
, options?: {
targetVersion?: number
throwOnNewerVersion?: boolean = false
migrationsTable?: string = 'migrations'
advisoryLockKey?: bigint = BigInt('-9223372036854775808') // The smallest bigint for postgres
}
): Promise
```
If `options.targetVersion` is `undefined`,
the maximum version of the `migrations` is used.
When the maximum known migration version is less than the database schema version,
it means the current instance is outdated.
- When `options.throwOnNewerVersion` is `false` (default),
it will skip the migration,
so your outdated instance continues to run.
- When `options.throwOnNewerVersion` is `true`,
it will throw an error,
so your outdated instance fails immediately.
#### Can multiple instances migrate in parallel?
Yes, it uses advisory lock to ensure that only one instance is migrating at a time.
#### What if my migration requires more than one connection?
You can get all connection configurations through properties to create a new `pg.Client`.
It is important to note that the custom client you create is not part of the migration transaction.